Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

©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

Slide 4

Slide 4 text

©2019 Wantedly, Inc. Robolectric

Slide 5

Slide 5 text

©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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

©2019 Wantedly, Inc. Binary Resources

Slide 9

Slide 9 text

©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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

©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

Slide 13

Slide 13 text

©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

Slide 14

Slide 14 text

©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

Slide 15

Slide 15 text

©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

Slide 16

Slide 16 text

©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

Slide 17

Slide 17 text

©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

Slide 18

Slide 18 text

©2019 Wantedly, Inc.

Slide 19

Slide 19 text

©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

Slide 20

Slide 20 text

©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

Slide 21

Slide 21 text

Thanks! ©2019 Wantedly, Inc.