简体   繁体   中英

Failure [INSTALL_FAILED_USER_RESTRICTED: Invalid apk] in android

I am using Android Studio 3.0.1.
When i am trying to run app

INSTALL_FAILED_USER_RESTRICTED: Invalid apk

error occurs.

I also disable Instant Run.

在此处输入图像描述

again i am run app but same error occurs.

04/04 10:59:08: Launching app
$ 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"
Failure [INSTALL_FAILED_USER_RESTRICTED: Invalid apk]

$ adb shell pm uninstall com.android.buyforfund
DELETE_FAILED_INTERNAL_ERROR
Error while Installing APK

I have the same problem, I sent a feedback to Google on my phone, would say it's a bug. In my case the best solution is to cancel that dialog and then re-run, it works always on second attempt after cancelling.

Depending on what phone you are using, I would assume the problem is on the phone (Google) side. Not sure yet if a general Google problem or specific hardware phones, I use "Xiaomi Redmi 5".

Disabling instant run actually worked in my case, but that's not the purpose of it, that's just a dirty workaround.

Edit: Make sure that you don't have something like

android:debuggable="false"

in your manifest.

None of the other answers worked for me using Xiaomis MIUI 10 on a Mi 9 phone.

Besides the usual (like enabling USB debugging and Install via USB in the developer options) answers from other questions suggested turning off MIUI optimization . While this did the trick, I wasn't happy with it. So I did some digging and came to the following conclusions:

The described error only occurred the second time you deploy your app and after that keeps occurring every other time when deploying the same app to that phone.

To solve this I could either hit Run / press Shift + F10 again or unplug and plug in that phone again. None of this seems viable. So I did some more digging and it turns out when you are increasing the versionCode in your build.gradle file every time you build your app, MIUI 10 will not complain and let you install your app just like you would expect. Even Android Studios Instant Run works. Though doing this manually is just as annoying.

So I took some ideas to auto-increment the versionCode from this question and modified build.gradle (the one for your module, NOT the one for your project). You can do the same following these easy steps:

Replace

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"
}

with

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!")
}

Now each time you build your app by hitting Run or Instant Run your versionCode / VERSION_BUILD increases. If you build a release your VERSION_PATCH increases as well changing your versionName from xyz to xyz+1 (ie 1.2.3 turns to 1.2.4 ). To change VERSION_MAJOR (the x ) and VERSION_MINOR (the y ) edit the version.properties file which you can find in your module folder. If you didn't change your modules name it's called app so this file is located at app/version.properties .

Make sure you have enabled the following options:

Settings > Additional Settings > Developer options

  1. USB Debugging
  2. Install via USB
  3. USB Debugging (Security settings)

For those who still might get this error if you have done everything from enabling to flutter clean and all. I solved this error by checking the manifest and adding proper value because i changed something in the manifest which was not supported and later forgot to change it. so if you have changed something in theme, drawable or any resource file check that out first.

If you are targeting android 'P' then your build.gradle file should look like this

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 must be 27 to run your app below android 'P', otherwise the app can only run on 'P' and above.

INSTALL_FAILED_USER_RESTRICTED: Invalid apk Steps for MIUI 9 and Above:

Settings -> Additional Settings -> Developer options -> step 1: scroll down side - Turn off "MIUI optimization" and Restart your device. step 2: Turn On "USB Debugging" step 3: Turn On "Install via USB" step 4: show push notification and just click USB - Set USB Configuration to Charging (MTP(Media Transfer Protocol) is the default mode. Works even in MTP in some cases). please try.

I ran into this same error while the underlying issue was different.

Mine was that I was trying to install my app on Android 12 device while the AndroidManifest.xml file didn't have all the android:exported properties explicitly set. This error is explained further here: https://developer.android.com/about/versions/12/behavior-changes-12#exported

If your app targets Android 12 or higher and contains activities, services, or broadcast receivers that use intent filters, you must explicitly declare the android:exported attribute for these app components .

Warning: If an activity, service, or broadcast receiver uses intent filters and doesn't have an explicitly-declared value for android:exported , your app can't be installed on a device that runs Android 12 or higher .

After I added the required android:exported properties into AndroidManifest.xml file, the error was resolved.

In your Androidmanifest.xml file at path

app/src/main/Androidmanifest.xml

add android:exported="true"` in activity tag.

Sample:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.example">
   <application
        android:label="example"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize"
            android:exported="true">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM