Solution 1 :
the reason it fails is because you copy unmodified the output-metadata.json file (which contains the path to the original file). So the install task uses the new json file (which points to the old APK !).
we actually have an example of copying APKs here:
https://github.com/android/gradle-recipes/tree/master/Kotlin/workerEnabledTransformation
it’s a bit more complicated than what you do because it uses Worker to copy the files in parallel, but you should be able to use it as is.
please also note that we are in the process of changing those incubating API and some of the APIs used in that example are likely to change by the time we reach 4.2-beta01.
Problem :
I’ve been experimenting with the new com.android.tools.build:gradle-api
as described in
https://medium.com/androiddevelopers/new-apis-in-the-android-gradle-plugin-f5325742e614.
Specifically I’m looking at the part that talks about transforming apk artifacts like so:
abstract class MyTask: DefaultTask() {
@get:InputFiles
abstract val inputApkFiles: DirectoryProperty
@get:OutputDirectory
abstract val outputApkDirectory: DirectoryProperty
@TaskAction
fun taskAction() {
println("MyTask running")
inputApkFiles.asFileTree.files.forEach(Consumer {
println("Copying ${it} to ${File(outputApkDirectory.asFile.get(), it.name)}")
it.copyTo(File(outputApkDirectory.asFile.get(), it.name), overwrite = true)
})
}
}
class ExamplePlugin: Plugin<Project> {
override fun apply(project: Project) {
project.extensions.getByType(CommonExtension::class.java).let { androidExtension ->
androidExtension.onVariantProperties {
val myTask = project.tasks.register("myTask${this.name.capitalize()}", MyTask::class.java) {
it.inputApkFiles.set(artifacts.get(ArtifactType.APK))
}
artifacts
.use(myTask)
.wiredWithDirectories(
MyTask::inputApkFiles,
MyTask::outputApkDirectory
)
.toTransform(ArtifactType.APK)
}
}
}
}
I’m trying to combine this new api with custom tasks in gradle plugin so that Android studio picks up these changes. In other words, if you hit the “play” icon in Android studio I want my custom gradle apk-transform task to run, and the new apk produced from this apk to be installed and run on device.
I’ve found that even when I overcome the issue as described in How to avoid dependencies on internal android tasks in new com.android.tools.build:gradle-api (by including my custom gradle task in the Before launch
in the run configuration in android studio for app
) it is still installing and running an apk from an older path. Essentially, it’s ignoring the output from the custom task that ran a transformation on the apk.
I’m getting:
Copying /app/build/outputs/apk/debug/output-metadata.json to /app/build/intermediates/apk/debug/myTaskDebug/output-metadata.json
Copying /app/build/outputs/apk/debug/app-debug.apk to /app/build/intermediates/apk/debug/myTaskDebug/app-debug.apk
Yet during installation I can see that it’s instead using
/app/build/outputs/apk/debug/app-debug.apk
to push/install to device
How can I convince Android studio, and whatever other apk-consumer tasks there might be in the project to use the new apk that I produced in my custom task?
Comments
Comment posted by Dellkan
Thanks for this response! I’ll study the linked sample more to see where I went wrong. I have to note though.. I’ve not missed the presence and contents of
Comment posted by Dellkan
Done some additional testing (including reproducing the sample that was linked in this answer) and can now confirm that using the gradle install task (