6
votes

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

4
login with SU (user) then try to install.Himesh goswami
Means? I don't get it. @HimeshGoswamiUnnati Patadia
@unnati..Which device(make/model) you are using?Chandrakanth

4 Answers

4
votes

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.

3
votes

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)
3
votes

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.

0
votes

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.