Last active
January 18, 2021 02:39
-
-
Save ghale/5787f925c50a34b9b64a8aa16894087c to your computer and use it in GitHub Desktop.
Inserting weaving task into Android pipeline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
android { | |
libraryVariants.all { | |
def kotlinCompileProvider = tasks.named("compile${name.capitalize()}Kotlin") | |
kotlinCompileProvider.configure { | |
// We do this because the way Kotlin wires itself into the Android build pipeline, | |
// it maps directly to the destinationDir property. This means if we change it, the | |
// Android configuration just follows it around. Instead, we let it generate the | |
// class files where it wants to, then we move them to where we want them, set this | |
// new location as an output directory (for caching/incrementality) and delete the | |
// configured output directory. | |
def preWeaveKotlinDir = layout.buildDir.dir("preWeave/${name}/kotlin") | |
outputs.dir preWeaveKotlinDir | |
doLast { | |
copy { | |
from destinationDir | |
into preWeaveKotlinDir | |
} | |
delete(destinationDir) | |
} | |
} | |
javaCompileProvider.configure { | |
// Add the preWeave Kotlin dir to the classpath, since we've removed the original dir | |
classpath += files(kotlinCompileProvider) | |
// Do the same for Java | |
def preWeaveJavaDir = layout.buildDir.dir("preWeave/${name}/java") | |
outputs.dir preWeaveJavaDir | |
doLast { | |
copy { | |
from destinationDirectory | |
into preWeaveJavaDir | |
} | |
delete(destinationDirectory) | |
} | |
} | |
def weaveProvider = tasks.register("weave${name.capitalize()}", Copy) { | |
from javaCompileProvider | |
from kotlinCompileProvider | |
into layout.buildDir.dir("postWeave/${name}") | |
} | |
// This registers the weave task in the Android pipeline as something that generates | |
// byte code. This is technically designed to _add_ things to the pipeline, but | |
// since we've removed the java/kotlin output, it effectively replaces the byte code | |
// that they generate. | |
registerPostJavacGeneratedBytecode(files(weaveProvider.map { it.outputs.files })) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment