Rename APK Android

Good morning,
Apologies for my school English.
I execute this task in a build of an Android project:

/* SOURCE
    https://discuss.gradle.org/t/what-is-the-actual-right-way-to-use-tasks/44926 */
android.applicationVariants.configureEach {
    val variant = this
    variant.outputs.configureEach {
        if (outputFile != null && outputFile.name.endsWith(".apk")) {
            val assembleTask = variant.assembleProvider
            val mustRunAfterTaskName = "create${variant.name.capitalize()}ApkListingFileRedirect"
            val newTargetName = "${variant.applicationId}-v${variant.versionName}-${variant.name}.apk"
            val copyTask = project.tasks.register("copyAndRenameOutputAPKFile${assembleTask.name.capitalize()}") {
                mustRunAfter(mustRunAfterTaskName)
                inputs.files(assembleTask)
                outputs.file(rootProject.file(newTargetName))
                doLast {
                    copy {
                        from(assembleTask)
                        into(rootDir)
                        rename(outputFile.name, newTargetName)
                    }
                    println("Copied ${outputFile.name} to $newTargetName")
                }
            }
            assembleTask { finalizedBy(copyTask) }
        }
    }
}

RUN gives me the following output:

Executing tasks: [copyAndRenameOutputAPKFileAssembleRelease] ...
...
> Task :app:copyAndRenameOutputAPKFileAssembleRelease
Copied app-release-unsigned.apk to scorpion.android731-v1.0.2023-12-02-release.apk
BUILD SUCCESSFUL ...

But my apk in the build is not renamed ???

BEFORE RUN
../build/outputs/apk/release ls
app-release-unsigned.apk output-metadata.json
AFTER RUN
/build/outputs/apk/release ls
app-release-unsigned.apk output-metadata.json

Can you tell me where the error is, thank you.

Can you tell me where the error is

In your expectations.
You copy into(rootDir) but do not search for the file in the root directory but in build/outputs/apk/release.
Look in the root dir of your build and you should find the file.

Good evening,
Thanks for your response, but I confirm my APK is not found.

Executing tasks: [app:copyAndRenameOutputAPKFileAssembleDebug] in project

Task :app:copyAndRenameOutputAPKFileAssembleDebug
Copied app-debug.apk to scorpion.android731-v1.0.2023-12-12-debug.apk
BUILD SUCCESSFUL

[TERMINAL]
scorpion ~ locate scorpion.android731-v1.0.2023-12-12-debug.apk

scorpion ~

I don’t understand ?

Claude

locate uses a database, it does not immediately find files that were just created.
Use sudo update-db first, or just look into the root folder of the build.

Good morning,
I know, after the build I do (root) updatedb then (user) locate.
Claude

Then I really cannot say without debugging the build.
Maybe you execute something else that then deletes the file or something like that.
But as you get the println printed which is done after the copying is finished, at that point in time the file should be there.
Maybe set a breakpoint at that println line and check for the file then while the execution is suspended at that breakpoint.

Good evening,
Thank you for your advice, it seems that in the Android Studio logs the task poses a problem but all this is beyond MY SKILLS.

[ Exception in thread "DisconnectableInputStream source reader" org.gradle.api.UncheckedIOException: java.nio.channels.AsynchronousCloseException
...
Caused by: java.nio.channels.AsynchronousCloseException
... ]

As a result, I made a new task, much more simplistic, but which does its job.

...
task("RenameCopyAPK") {
    doLast {
        val path = file("build/outputs/apk/debug/app-debug.apk")
        val destination = "/home/scorpion/Android/Mes_APKs/Api34/"
        val desiredName = "${defaultConfig.applicationId}-v${defaultConfig.versionName}-debug.apk"
        if (path.exists()) {
            copy {
                from(path)
                into(destination)
                rename { desiredName }
            }
            println("Copied $path to $destination$desiredName")
        } else {
            println("Path not found")
        }
    }
}
...

Once again thank you,
Claude

1 Like