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.
    QPUBUPUJQT
    !XBLXBL 3ZP4BLBHVDIJ

    2019.6.18 -
    5FTUNVMUJQMF"1,T

    XJUI3PCPMFDUSJD

    View Slide

  2. ©2019 Wantedly, Inc.
    Ryo Sakaguchi
    • Wantedly, Inc - Wantedly People
    • Android/CI/CD/UI
    • Kotlin/Sake/Beer/MtG/Guitar…
    • Twitter/GitHub: @wakwak3125

    View Slide

  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

    View Slide

  4. ©2019 Wantedly, Inc.
    Robolectric

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  8. ©2019 Wantedly, Inc.
    Binary Resources

    View Slide

  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

    View Slide

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

    View Slide

  11. ©2019 Wantedly, Inc.
    Problem Testing the Multiple APKs w/Robolectric
    Robolectric + Multiple APKs
    I got this error…
    > expected one element but was:

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide


  18. ©2019 Wantedly, Inc.

    View Slide

  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

    View Slide

  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

    View Slide

  21. Thanks!
    ©2019 Wantedly, Inc.

    View Slide