Test multiple APKs 
with Robolectric

Test multiple APKs 
with Robolectric

2019/06/18 potatotips #62

90ec81078e4fd2b905f8ef0776887afa?s=128

Ryo Sakaguchi

June 18, 2019
Tweet

Transcript

  1. ©2019 Wantedly, Inc. QPUBUPUJQT !XBLXBL 3ZP4BLBHVDIJ 2019.6.18 - 5FTUNVMUJQMF"1,T
 XJUI3PCPMFDUSJD

  2. ©2019 Wantedly, Inc. Ryo Sakaguchi • Wantedly, Inc - Wantedly

    People • Android/CI/CD/UI • Kotlin/Sake/Beer/MtG/Guitar… • Twitter/GitHub: @wakwak3125
  3. ©2019 Wantedly, Inc. 1. Robolectric • What’s this? • Why

    use this? • Binary Resources 2. Testing the Multiple APKs w/Robolectric • Problem • Why? • Way to resolve 3. WrapUp Today’s topics What I talk today
  4. ©2019 Wantedly, Inc. Robolectric

  5. ©2019 Wantedly, Inc. is a framework that brings fast and

    
 reliable unit tests to Android. 
 Tests run inside the JVM on your 
 workstation in seconds. Robolectric robolectric.org
  6. ©2019 Wantedly, Inc. API is common to androidx.test

  7. ©2019 Wantedly, Inc. API is common to androidx.test

  8. ©2019 Wantedly, Inc. Binary Resources

  9. ©2019 Wantedly, Inc. Use resources processed using 
 the Android

    build toolchain.
 Loads and handles resources using 
 the same logic as on 
 an actual Android device. Binary Resources robolectric.org
  10. ©2019 Wantedly, Inc. Testing the Multiple APKs w/Robolectric

  11. ©2019 Wantedly, Inc. Problem Testing the Multiple APKs w/Robolectric Robolectric

    + Multiple APKs I got this error… > expected one element but was: <BuildOutput{apkData=DefaultApkData…
  12. ©2019 Wantedly, Inc. Problem Testing the Multiple APKs w/Robolectric Why

    this cause? Let's take a look source code a bit https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle- core/src/main/java/com/android/build/gradle/internal/tasks/PackageForUnitTest.java
  13. ©2019 Wantedly, Inc. Problem Testing the Multiple APKs w/Robolectric ArtifactType

    Target artifact type to get. In this case, PROCESSED_RES(APK includes only resources) https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle- core/src/main/java/com/android/build/gradle/internal/tasks/PackageForUnitTest.java
  14. ©2019 Wantedly, Inc. Problem Testing the Multiple APKs w/Robolectric BuildableArtifact

    Output Files https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle- core/src/main/java/com/android/build/gradle/internal/tasks/PackageForUnitTest.java
  15. ©2019 Wantedly, Inc. Problem Testing the Multiple APKs w/Robolectric When

    you split APK… This should become multiple https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle- core/src/main/java/com/android/build/gradle/internal/tasks/PackageForUnitTest.java
  16. ©2019 Wantedly, Inc. Problem Testing the Multiple APKs w/Robolectric BuildElements

    It returns BuildElements that match artifact type. https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle- core/src/main/java/com/android/build/gradle/internal/tasks/PackageForUnitTest.java
  17. ©2019 Wantedly, Inc. Problem Testing the Multiple APKs w/Robolectric Iterables.getOnlyElement

    Returns the single element contained in iterable.
 IllegalArgumentException - if the iterable contains multiple elements https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle- core/src/main/java/com/android/build/gradle/internal/tasks/PackageForUnitTest.java
  18. ©2019 Wantedly, Inc.

  19. ©2019 Wantedly, Inc. Way to resolve Testing the Multiple APKs

    w/Robolectric I found some ways to resolve this • Use Android App Bundle(Recommended) • Android App Bundle allows you to separate the publish settings and build settings.(On publish, bundleXXX. On develop, assembleXXX) • And also reduce your APK size! • Introduce custom Gradle properties and switch it by command line arguments. • It’a not bad idea, but when you run tests on Android Studio, you have to set the arguments every time you want. • Until yesterday, I have been used this. Please choice by yourself
  20. ©2019 Wantedly, Inc. Test multiple APKs with Robolectric WrapUp •

    Test w/Robolectric + Binary Resources + Multiple APK does not work correctly. • I recommend to use Android App Bundle • You can fine issue here https://github.com/robolectric/ robolectric/issues/4942
  21. Thanks! ©2019 Wantedly, Inc.