Rename APK Android

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

/* SOURCE */
android.applicationVariants.configureEach {
    val variant = this
    variant.outputs.configureEach {
        if (outputFile != null &&".apk")) {
            val assembleTask = variant.assembleProvider
            val mustRunAfterTaskName = "create${}ApkListingFileRedirect"
            val newTargetName = "${variant.applicationId}-v${variant.versionName}-${}.apk"
            val copyTask = project.tasks.register("copyAndRenameOutputAPKFile${}") {
                doLast {
                    copy {
                        rename(, newTargetName)
                    println("Copied ${} 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

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

../build/outputs/apk/release ls
app-release-unsigned.apk output-metadata.json
/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

scorpion ~ locate scorpion.android731-v1.0.2023-12-12-debug.apk

scorpion ~

I don’t understand ?


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.

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 {
                rename { desiredName }
            println("Copied $path to $destination$desiredName")
        } else {
            println("Path not found")

Once again thank you,

1 Like