4

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.

progquester
  • 1,228
  • 14
  • 23

2 Answers2

6

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).

funkybro
  • 8,432
  • 6
  • 39
  • 52
  • Would you please give a detail description? How can find the "Build Variants" tool? Thank you very much. – progquester Jan 10 '21 at 02:47
  • @ProgQuester there's a Build Variants tab at the side of the window, or choose View > Tool Windows > Build Variants. – funkybro Jan 11 '21 at 07:17
1

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

Dellkan
  • 1,861
  • 11
  • 15
  • It seems too complex if I have to manually modify this file after each compilation, Is there any method to modify script to change the conent? Thank you very much! – progquester Sep 07 '20 at 14:17
  • Either use gradle itself to update `output-metadata.json` as part of building (using a custom task). Or use the `archivesBaseName` property to change the name. This method also updates the `output-metadata.json` appropriately. See updated answer above for more details. – Dellkan Sep 07 '20 at 14:36
  • thank you very much. I tried your solution, but it has no help. I found the key point is about outputDirectory (not about outputFileName/archivesBaseName). The outpuFile field in json file can be set to proper value by outputFileName, but once directory is chaned, debugger cannot find the apk. – progquester Sep 08 '20 at 06:56