Val
Val

Reputation: 4366

Renamed APK with the time of building does not run

I use this code in build.gradle file of my module to rename the output APK

android.applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def file = output.outputFile
                def formattedDate = new Date().format('yyyy_MM_dd_HH_mm')
                output.outputFile = new File(file.parent, file.name.replace(".apk",
                        "-" + formattedDate + ".apk"))
            }
        }

When I press "Run", I receive the APK here build/outputs/apk/app-debug-2016_01_11_13_23.apk and get an error in the Run console:

The APK file .../build/outputs/apk/app-debug-2016_01_11_13_21.apk does not exist on disk.

Every new time when I press "Run", I receive the new APK in the /build/outputs/apk/ folder but the error is the same. It looks like an Android Studio uses the old value of the app's name.

I use this Run configuration: enter image description here

Feel free to give any kind of suggestions.

Upvotes: 2

Views: 434

Answers (1)

Gomino
Gomino

Reputation: 12347

Looks like there is a bug in current version 1.5.1 of android studio. I had the same problem as you but I only wanted to rename the release apk.

So I ended up with this quick workaround to rename the apk only when the selected signinConfig is the release one:

android.applicationVariants.all { variant ->
    if (variant.buildType.signingConfig.getName() == android.signingConfigs.release.getName()) {
        variant.outputs.each { output ->
            def file = output.outputFile
            def formattedDate = new Date().format('yyyy_MM_dd_HH_mm')
            output.outputFile = new File(file.parent, file.name.replace(".apk",
                    "-" + formattedDate + ".apk"))
        }
    }
}

All the debug build will have the same name, so it doesn't matter if Android Studio has difficulties refreshing the apk name before uploading it to the device

Upvotes: 1

Related Questions