Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test multiple APKs 
with Robolectric

Test multiple APKs 
with Robolectric

2019/06/18 potatotips #62

Ryo Sakaguchi

June 18, 2019
Tweet

More Decks by Ryo Sakaguchi

Other Decks in Programming

Transcript

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

    People • Android/CI/CD/UI • Kotlin/Sake/Beer/MtG/Guitar… • Twitter/GitHub: @wakwak3125
  2. ©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
  3. ©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
  4. ©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
  5. ©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…
  6. ©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
  7. ©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
  8. ©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
  9. ©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
  10. ©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
  11. ©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
  12. ©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
  13. ©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