一、问题现象
在 Android Studio安装APK时,可能遇到:
Groovy
Error running 'app'
The application could not be installed: INSTALL_FAILED_TEST_ONLY Installation failed due to: 'Error code: 'INSTALL_FAILED_TEST_ONLY', message='INSTALL_FAILED_TEST_ONLY''
List of apks:[0] '/.../debug.apk'
或:
Groovy
Installation did not succeed.
The application could not be installed: INSTALL_FAILED_TEST_ONLY
List of apks:[0] '/.../debug.apk'Installation failed due to: 'null'
二、根本原因
从 Android Studio 3.0 + Gradle Plugin 开始:
Gradle 在构建 debug APK 时,会默认标记 testOnly 属性
最终在 APK 中体现为:android:testOnly="true"
系统认为: ⚠️这是"仅供测试使用的 APK",禁止普通方式安装
❗关键误区纠正
很多人会尝试:android:testOnly="false"
这是无效的,原因是:
- testOnly 在3.0后不是 Manifest 的静态配置
- 而是 构建系统在打包阶段动态注入的属性
- 最终以 Gradle / PackageManager 为准
3.0之前是静态配置,构建工具不会动态注入,默认false,可配置;3.0之后由构建工具强制注入true。
三、最快解决方案(推荐)
✔ 方案1:直接关闭 testOnly 注入
在 gradle.properties 添加:
Groovy
android.injected.testOnly=false
然后执行:
bash
./gradlew clean
四、推荐标准方案(官方思路)
其实 Google 更推荐的方式是:
bash
./gradlew installDebug
等价于:assembleDebug + adb install -t,优势:
- 保留 testOnly 限制
- 可安装apk到设备
- 不需要手动 adb install -t
五、传统 adb 方式(不推荐但有效)
bash
adb install -t app-debug.apk //通过 -t 标志为测试包进行安装
adb install -r -t app-debug.apk //覆盖安装
一句话总结
INSTALL_FAILED_TEST_ONLY 并不是错误,而是 Android 系统对"测试构建 APK"的保护机制,用于防止测试包被误当作正式应用安装。解决方式要么在构建层面移除 testOnly 标记,要么在安装时通过 adb 使用 -t 参数显式允许安装;而 Release 包默认不受此限制。