Failure [INSTALL_FAILED_USER_RESTRICTED: Invalid apk] in android
Asked Answered
W

9

13

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

Whitney answered 4/4, 2018 at 5:43 Comment(3)
login with SU (user) then try to install.Unreconstructed
Means? I don't get it. @HimeshGoswamiWhitney
@unnati..Which device(make/model) you are using?Grivation
B
10

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.

Biathlon answered 11/7, 2022 at 1:21 Comment(0)
I
6

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">
Inconsequential answered 20/7, 2022 at 7:11 Comment(1)
I only updated targetSdkVersion from 29 to 31 and adding android:exported="true" in androidManifest.xml resolved my issue.Cilla
E
5

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.

Exarchate answered 20/6, 2019 at 6:56 Comment(1)
Thank you! I can confirm that this solution works perfect on MIUI 10Propolis
U
4

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.

Unwell answered 28/4, 2018 at 15:34 Comment(1)
Redmi 5 plus device here, same issue, canceling and installing again helps, happens every other installWeight
B
3

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)
Boyce answered 4/4, 2018 at 5:57 Comment(3)
all options are enabled.Whitney
Try this command: adb shell pm uninstall com.android.buyforfund.debug and see the result success or fail?Boyce
adb shell "su" than try to installUnreconstructed
G
3

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.

Gudrun answered 20/4, 2022 at 12:38 Comment(0)
T
1

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.

Trenchant answered 22/9, 2021 at 16:42 Comment(0)
P
1

For MIUI 9 and above:

  1. Navigate to Settings -> Additional Settings -> Developer Options.
  2. Disable "MIUI optimization" and restart your device. Enable "USB
  3. Debugging" within the Developer Options.
  4. Activate "Install via USB" (Note that MTP (Media Transfer Protocol) is the default mode, and it might work with MTP in some cases).
  5. Set the USB Configuration to "Charging" mode.
Panoply answered 30/3, 2023 at 12:42 Comment(0)
J
0

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.

Jessamyn answered 2/6, 2018 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.