TEST_ONLY 导致apk无法安装问题解决

升级到7.0,android studio升级到3.0后经常出下如下异常,导致apk无法正常安装,放到手机sd卡中也无法完成安装,会提示安装失败:

adb命令行

通过如下命令可以完成安装:

1
adb install -t *.APK

但是导致问题的原因在哪里呢?

网上有说是manifest中配置了testOnly标签的:

网络上问题原因

然而实际上,真正的原因是什么呢?
看看官方的说明吧:
https://developer.android.com/studio/run/index.html

Note: The Run button builds an APK with testOnly=”true”, which means the APK can only be installed via adb (which Android Studio uses). If you want a debuggable APK that people can install without adb, select your debug variant and click Build > Build APK(s).

大致意思就是说,只要在AndroidStudio3.0内,点击运行Run,跑出来的apk,一定是TEST_ONLY的,如果需要可以安装的apk,直接使用build apks或者assembleRelease即可。


以下转载自 沈文昌同学的blog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
之前有个问题涉及到生成apk无法安装。当时搜索到的结果是和gradle版本有关,实际上不是这样的。

今天测试妹妹要一个apk,我直接run了一个给她,发现问题又出现了。

我首先怀疑是不是Instant Run的配置,但这个配置我在settings里是关掉的,而Run功能(绿三角)的Configurations里,Android App->app中,右侧最下方Before launch,配置了一个Instant App Provision。我将这个配置直接删掉,问题依旧。

看来不是Instant Run的锅。

然后,开始怀疑是否是gradle版本不可为最新,尝试下载了升级AS3.0默认配的4.0-milestone-1,以及4.1,以及4.1-milestone-1,花费了很多时间,最后发现也不是gradle版本问题。

最终,在一个曾经看过的帖子上,发现了另一个回答。

https://stackoverflow.com/questions/25274296/adb-install-fails-with-install-failed-test-only

往下翻,有个只有个位数支持率的回答,里面提到官方网站在beta7版本已经有了提示: https://developer.android.com/studio/run/index.html

这个网站我是打不开的……好吧,这位好心人直接把主要内容粘贴了:

Note: The Run button builds an APK with testOnly="true", which means the APK can only be installed via adb (which Android Studio uses). If you want a debuggable APK that people can install without adb, select your debug variant and click Build > Build APK(s).

也就是说,只要在AndroidStudio3.0内,点击绿三角运行Run,跑出来的apk,一定是TEST_ONLY的。

问题解决,以后给测试妹妹出apk,不能直接用Run出来的了,必须跑一下Build Apks。