AS can not find the apk when starting debug app when outputDirectory or outputFileName changed

Viewed 1912

I am using Android Studio to debug my application. For some reasons, I have to change the default output apk directory and file name. So I changed the applicationVariants' outputDirectory and outputFileName in app's build.gradle script. This modification make the apk output to the expected place and name. But I found a problem is that when I start to debug the app just like before, AS said:

Installation did not succeed.
The application could not be installed.

List of apks:
[0] 'C:\Users\progquest\projects\Study\app\build\outputs\apk\free\debug\app-free-debug.apk'
Installation failed due to: 'Invalid File: C:\Users\progquest\projects\Study\app\build\outputs\apk\free\debug\app-free-debug.apk'

Note: 'free' is a product flavor of my application.

Obviously, the debugger still used the old default output directory and apk file name to find the source apk. How to change it?

Thank you.

2 Answers

This happened to me; it started working again after I unselected and then re-selected the correct "Active Build Variant" in the "Build Variants" tool (even though it was already selected).

There is a file called output-metadata.json in the same directory.

The contents of this file is on my system something like so:

{
  "version": 2,
  "artifactType": {
    "type": "APK",
    "kind": "Directory"
  },
  "applicationId": "com.example.sample",
  "variantName": "debug",
  "elements": [
    {
      "type": "SINGLE",
      "filters": [],
      "versionCode": 1,
      "versionName": "1.0",
      "outputFile": "app-debug.apk"
    }
  ]
}

From my testing it appears that changing the path in "outputFile" (i.e the field that currently states "app-debug.apk") makes Android-studio pick up the change.


Alternatively: Use the property to change the apk-name-

android {
    ...
    defaultConfig {
        ...
        setProperty("archivesBaseName", "MyNewAppNameGoesHere")
    }
}

This sets part of the apk name, and also updates output-metadata.json appropriately so Android studio picks it up.

see https://stackoverflow.com/a/39202249/2923245

Related