https://kotlinlang.org logo
#gradle
Title
# gradle
e

enighma

02/22/2024, 12:46 AM
I have a gradle KTS script wherein I rename the apk file after it has been built. It might not be the most elegant but it has worked until I turned on build cache. This leads me to believe there's a better way, so what is the right way to rename or even name artifacts in the context of Android?
c

Chris Lee

02/22/2024, 2:01 AM
Generally you wouldn’t rename artifacts; either generate them with the correct name, or copy them to a target location, renaming along the way. I don’t use Android hence don’t know the specific tasks but the general Gradle patterns apply.
e

enighma

02/22/2024, 2:46 AM
Why wouldn't I rename the artifact generally? I don't mind generating them with the correct name. It's just not obvious how I could make that dynamic. Right now it generates it on the following format:
<modulename>-<flavor>.apk
I just want the build date postfixed. I guess I could create a gradle task that runs on compile and does what you do. I just imagine there's somewhere to add a suffix.
g

gildor

02/22/2024, 3:13 AM
I think it uses older variant API, so for new AGP it should be something like androidComponent { onVariants { variant -> variant.outputs = ?? } }
I didn't work on it on new variants API to change output name, but more or less it should work something like this
Start look into it and it indeed more complicated than this, I think it requires transformation task
Okay:
It's not recommended to simply rename the APKs that are generated by the default task because Studio expects specific names.
Having an API to rename things based on date/time would break Studio deployment as Studio would not know where to get these files. Based on this the API exposed by AGP allows you to copy them only.
d

Daniel B Duval

02/25/2024, 2:55 PM
There’s the above plug in that uses workers, below is something I’ve used to relocate the APK in a CI tasks simply to avoid env variables due to multiple flavors and built types. This is Groovy as I have not had time to convert this particular build script to kts yet.
Copy code
androidComponents {
    onVariants(selector().all(), { variant ->
      def targetDirectory = "outputs/apk/"
      def copy = tasks.register("copy${variant.getName().capitalize()}Apks", Copy) {
        from { variant.artifacts.get(SingleArtifact.APK.INSTANCE)  }
        exclude "**/androidTest/**"
        include "*.apk"
        into project.layout.buildDirectory.file(targetDirectory)
      }

      afterEvaluate {
        if (tasks.findByName(("create${variant.getName().capitalize()}AndroidTestApkListingFileRedirect"))) {
          tasks.named("create${variant.getName().capitalize()}AndroidTestApkListingFileRedirect").configure({
            it.dependsOn(copy)
          })
        }
        tasks.named("create${variant.getName().capitalize()}ApkListingFileRedirect").configure({
          it.dependsOn(copy)
        })
        tasks.withType(PackageApplication.class).named("package${variant.getName().capitalize()}").configure {
          finalizedBy(copy)
          copy.get().dependsOn(it)
        }
      }
    })
  }
37 Views