VDT - Not able to run instrumentation tests on android library project

Thank you for the help in finding a solution to this problem. I wanted to add my research into this problem and leave it here for reference to help others.

Problem: When running instrumentation tests on Bitrise for an Android library project, Bitrise may throw an error where it could not find an app APK on the workflow step: Android build for UI testing.

The error you may see is:

Export APKs:
  Export [ libraryname-debug-androidTest.apk => $BITRISE_DEPLOY_DIR/libraryname-debug-androidTest.apk ]
Could not find the exported app APK

Bitrise found my test APK, but did not find my app APK. Even though it did run the gradle task to compile an app APK: /bitrise/src/gradlew ":libraryname:assembleDebug" ":libraryname:assembleDebugAndroidTest", an app APK was not created because this is an Android library project, not an app.

As stated earlier in this post, the solution to this problem is to create a fake app APK that Bitrise can find in your project and upload to Firebase test lab. The app APK does not need to be anything special. It will not even be used. It will only exist in order for Firebase test lab to satisfy it’s need for having an app APK.

To create a fake app APK, I created a brand new Android Studio project with no views and no dependencies and built an APK for 93kb (awesome!). I copied the built app APK file into my code’s git repo. I made a bash script that would run on my Bitrise workflow before my workflow step for “Android build for UI testing”. This bash script would copy the already built APK and copy it into libraryname/build/output/apk/debug to fake that it was built by gradle.

This, however, did not work. I got the same error about my app APK not being found even though my copy bash script ran successfully.

Looking at the source code for Bitrise Android build for UI testing step, I stumbled upon the code for finding APK files and found that the modified date on the APK file must be greater then the date when the Bitrise Android build for UI testing workflow step started. This means that by running my bash script before the Android build for UI testing step, it would not work. Even if I modified the modified date on the file.

So, I resorted to this very simple solution, but not my favorite. I added an extra argument to the gradle task for my Android build for UI testing workflow step. This extra argument is simply :app:assembleDebug which would make sure to build a debug APK for my example app in my Android library project.

I do not like this solution as much because building my app’s APK takes longer then simply coping a 93kb file into a directory. Either one is a hack so I suppose either one isn’t the best solution.

2 Likes