Android Studio 3.0 and FLAG_TEST_ONLY

Your AndroidManifest.xml file can have an android:testOnly attribute. This value then shows up in PackageManager and ApplicationInfo as FLAG_TEST_ONLY. This has been around since API Level 4, which is practically forever in Android terms. According to what little docs we have, it serve as:

Option to indicate this application is only for testing purposes. For example, it may expose functionality or data outside of itself that would cause a security hole, but is useful for testing. This kind of application can not be installed without the INSTALL_ALLOW_TEST flag, which means only through adb install.

On the whole, this may seem unremarkable, considering that few people have remarked on it since it was introduced in 2009. However, there are two things to note about it:

  1. You cannot install an app with android:testOnly="true" by conventional means, such as from an Android file manager or from a download off of a Web site

  2. Android Studio 3.0 sets android:testOnly="true" on APKs that are run from the IDE

For many developers, this is of little consequence. However, if you have ever taken an APK lying around your build/ directory and sent it to somebody, that will only work if you did something other than run the app in Android Studio in order to create that APK, such as:

  • Built the app using the “Build APK(s)” menu option

  • Built the app using the assembleDebug or assembleRelease Gradle tasks

  • Built the app using something else that might use those tasks, such as a CI server

If, instead, you ship the APK that Android Studio built just by you running the app, the APK cannot be installed normally. adb install, for example, will report:

Failed to install app-debug.apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]

adb install -t will install such a test-only app, however.

I don’t really have a problem with Android Studio 3.0 adding this flag. After all, the APK that you run from the IDE may bear little resemblance to the APK that your users will use (hello, Instant Run!). Using this flag to steer developers towards shipping APKs built by other means is fairly reasonable.

Unfortunately, this change is not covered in the Android Studio 3.0 release notes, or the Android Studio 3.0 migration document. It is mentioned in the “Build and Run Your App” page, though the docs there do not mention the need for the -t switch.

So, just make sure that when you send an APK to somebody, you built it by some means other than just running it from Android Studio.