Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

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

like image 468
Unnati Patadia Avatar asked Apr 04 '18 05:04

Unnati Patadia


1 Answers

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 x.y.z to x.y.z+1 (i.e. 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.

like image 145
Tobias Wicker Avatar answered Sep 28 '22 06:09

Tobias Wicker