我正在使用Android Studio 3.0.1 当我试图运行应用程序时
发生INSTALL_FAILED_USER_RESTRICTED:无效的apk
错误。
我也禁用了Instant Run。
我再次运行应用程序,但同样的错误发生。
04/04 10:59:08:启动应用程序
$ adb push G:\ Android \ Fundraiser \ BuyForFund \ app \ build \ outputs \ apk \ debug \ app-debug.apk /data/local/tmp/com.android.buyforfund
$ adb shell pm install -t -r" /data/local/tmp/com.android.buyforfund"
失败[INSTALL_FAILED_USER_RESTRICTED:无效的apk]$ adb shell pm uninstall com.android.buyforfund
DELETE_FAILED_INTERNAL_ERROR
安装APK时出错
答案 0 :(得分:3)
我遇到同样的问题,我在手机上向Google发送了反馈意见,会说这是一个错误。在我的情况下,最好的解决方案是取消该对话框然后重新运行,它在取消后总是在第二次尝试时起作用。
根据您使用的手机,我认为问题出在手机(谷歌)方面。目前还不确定是否存在Google常见问题或特定硬件手机,我使用"小米Redmi 5"。
禁用即时运行实际上适用于我的情况,但这不是它的目的,这只是一个肮脏的解决方法。
编辑: 确保你没有像
这样的东西android:debuggable="false"
在你的清单中
答案 1 :(得分:2)
确保您已启用以下选项:
设置>其他设置>开发者选项
答案 2 :(得分:0)
如果您的目标是Android'P',那么您的build.gradle文件应该如下所示
android {
compileSdkVersion 'android-P'
defaultConfig {
applicationId "xyz.com"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
targetSdkVersion必须为27才能在Android'P'下运行您的应用,否则该应用只能在'P'及以上版本上运行。
答案 3 :(得分:0)
在Mi 9手机上使用Xiaomis MIUI 10
时,其他答案都对我没有帮助。
除了在开发人员选项中启用USB debugging
和Install via USB
这样的常用选项外,还建议关闭其他MIUI optimization
问题。尽管这成功了,但我对此并不满意。因此,我做了一些实验,得出以下结论:
所描述的错误仅在将同一应用程序部署到该手机时每隔两次发生。
要解决此问题,我可以按Run
/再按Shift + F10
或拔下插头再重新插入该电话。这些似乎都不可行。因此,我做了一些进一步的挖掘工作,结果发现,每次构建应用程序时,当您在versionCode
文件中增加build.gradle
时,MIUI 10
不会抱怨,而是让您安装应用程序就像您期望的那样。甚至Android Studio Instant Run
都可以使用。尽管手动执行也很烦人。
因此,我想出了一些办法,从this question自动增加versionCode
并修改了build.gradle
(一个用于您的模块,而不是用于您的项目)。您可以按照以下简单步骤进行操作:
替换
defaultConfig {
applicationId "your.app.id" // leave it at the value you have in your file
minSdkVersion 23 // this as well
targetSdkVersion 28 // and this
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
与
def versionPropsFile = file('version.properties')
def value = 0
Properties versionProps = new Properties()
if (!versionPropsFile.exists()) {
versionProps['VERSION_MAJOR'] = "1"
versionProps['VERSION_MINOR'] = "0"
versionProps['VERSION_PATCH'] = "0"
versionProps['VERSION_BUILD'] = "0"
versionProps.store(versionPropsFile.newWriter(), null)
}
def runTasks = gradle.startParameter.taskNames
if ('assembleRelease' in runTasks) {
value = 1
}
if (versionPropsFile.canRead()) {
versionProps.load(new FileInputStream(versionPropsFile))
versionProps['VERSION_PATCH'] = (versionProps['VERSION_PATCH'].toInteger() + value).toString()
versionProps['VERSION_BUILD'] = (versionProps['VERSION_BUILD'].toInteger() + 1).toString()
versionProps.store(versionPropsFile.newWriter(), null)
// change major and minor version here
def mVersionName = "${versionProps['VERSION_MAJOR']}.${versionProps['VERSION_MINOR']}.${versionProps['VERSION_PATCH']}"
defaultConfig {
applicationId "your.app.id" // leave it at the value you have in your file
minSdkVersion 23 // this as well
targetSdkVersion 28 // and this
versionCode versionProps['VERSION_BUILD'].toInteger()
versionName "${mVersionName} Build: ${versionProps['VERSION_BUILD']}"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
else {
throw new GradleException("Could not read version.properties!")
}
现在,每次您通过点击Run
或Instant Run
来构建应用时,您的versionCode
/ VERSION_BUILD
都会增加。
如果您构建发行版,则VERSION_PATCH
也会增加,并且versionName
从x.y.z
更改为x.y.z+1
(即1.2.3
会变成1.2.4
)。要更改VERSION_MAJOR
(x
)和VERSION_MINOR
(y
),请编辑version.properties
文件,您可以在模块文件夹中找到该文件。如果您未更改模块名称,则称为app
,因此此文件位于app/version.properties
。