LoginSignup
38
21

More than 5 years have passed since last update.

Android testOnly フラグ

Posted at

事象

Android のビルド後の APK を実機にインストールしようとすると、次のようなエラーが発生した。

$ adb install -d app-debug.apk
adb: failed to install app-debug.apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]

原因

これは testOnly フラグによるもの。
API level 4 の頃から testOnly フラグは、 AndroidManifest.xml の application 要素の android:testOnly 属性で設定できる。

testOnly フラグが立っているかどうかは、コード上は以下のように確かめられる。

val applicationInfo = packageManager.getPackageInfo("com.example.app", 0).applicationInfo
val isTestOnly = (applicationInfo.flags and ApplicationInfo.FLAG_TEST_ONLY) != 0

ところで、今回は AndroidManifest.xml で特に android:testOnly を設定したりはしていないのだが、このエラーが出てきた。

調べたところ、マニフェストファイルで指定する以外に、このフラグが立つ条件があるらしい。
公式の情報も間違っているんじゃないかな、と思う記載があったりして、正確な情報がいまいちわからないんだが、僕が調べた感じだと、以下の場合は testOnly な APK になるようだ。

  • Android Studio 3.0 以降の [Run] or [Debug] で直接インストールした際に生成された APK
  • compileSdkVersion が正式リリース版でない場合 (developer preview SDK、つまり数字ではなくてアルファベットで表される場合 : 今回の場合は 'android-P')

対応

このエラーを回避するには、2つの方法がある。

1つ目は adb install-t オプション (INSTALL_ALLOW_TEST フラグ) を付けて実行すること。

$ adb install -d -t app-debug.apk

2つ目は gradle.properties に testOnly フラグを無効化するオプションを付けること。

android.injected.testOnly=false

References

38
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
21