| import java.time.format.DateTimeFormatter |
| |
| // ATTENTION -- hash value of this file is checked in the corresponding |
| // integration test. Please make sure any changes you make here are |
| // backwards compatible. |
| |
| apply from: "../commonHeader.gradle" |
| buildscript { apply from: "../commonBuildScript.gradle" } |
| |
| apply plugin: 'com.android.application' |
| |
| android { |
| namespace "com.android.tests.basic" |
| compileSdkVersion libs.versions.latestCompileSdk.get().toInteger() |
| buildToolsVersion = libs.versions.buildToolsVersion.get() |
| } |
| |
| public abstract class GenerateCode extends DefaultTask { |
| @Input |
| abstract Property<String> getValue() |
| |
| @Input |
| abstract Property<String> getClassName() |
| |
| @OutputDirectory |
| abstract DirectoryProperty getOutputFolder() |
| |
| @Input |
| abstract Property<String> getOutputFile() |
| |
| @TaskAction |
| void taskAction() { |
| def outputFile = new File(getOutputFolder().get().asFile, getOutputFile().get()) |
| outputFile.parentFile.mkdirs() |
| outputFile.text = |
| "package com.custom;\n" + |
| "public class ${getClassName().get()} {\n" + |
| " public static String getBuildDate() { return \"${getValue().get()}\"; }\n" + |
| "}\n"; |
| } |
| } |
| |
| public abstract class GenerateRes extends DefaultTask { |
| @Input |
| abstract Property<String> getValue() |
| |
| @OutputDirectory |
| abstract DirectoryProperty getOutputFolder() |
| |
| @Input |
| abstract Property<String> getOutputFile() |
| |
| @TaskAction |
| void taskAction() { |
| def outputFile = new File(getOutputFolder().get().asFile, getOutputFile().get()) |
| outputFile.parentFile.mkdirs() |
| outputFile.text = '<?xml version="1.0" encoding="utf-8"?>\n' + value |
| .get() |
| } |
| } |
| |
| androidComponents { |
| |
| onVariants(selector().all(), { variant -> |
| |
| // create a task that generates a java class |
| def javaGenerationTask = tasks |
| .register("generateJavaFor${variant.name.capitalize()}", GenerateCode) { |
| value.set(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'").withZone( |
| ZoneId.from(ZoneOffset.UTC)).format(Instant.now())) |
| className.set("Foo") |
| outputFile.set("com/custom/Foo.java") |
| } |
| |
| variant.sources.java.addGeneratedSourceDirectory(javaGenerationTask, { it.getOutputFolder() }) |
| |
| // register another similar task, and wire it using the other registerJavaGeneratingTask API |
| def javaGenerationTask2 = tasks |
| .register("generateJavaFor${variant.name.capitalize()}2", GenerateCode) { |
| value.set(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'").withZone( |
| ZoneId.from(ZoneOffset.UTC)).format(Instant.now())) |
| className.set("Bar") |
| outputFile.set("com/custom/Bar.java") |
| } |
| |
| variant.sources.java.addGeneratedSourceDirectory(javaGenerationTask2, { it.getOutputFolder() }) |
| |
| |
| // create a task that generates an XML file class |
| def resGenerationTask = tasks |
| .register("generateResFor${variant.name.capitalize()}", GenerateRes) { |
| if (Boolean.parseBoolean( |
| project.property('inject_enable_generate_values_res') |
| )) { |
| value.set('<resources>\n' + |
| ' <string\n' + |
| ' name="generated_string">Hello World</string>\n' + |
| '</resources>') |
| outputFile.set("values/generated.xml") |
| |
| } else { |
| value.set('<xml>' + java.time.format.DateTimeFormatter.ofPattern( |
| "yyyy-MM-dd'T'HH:mm'Z'").withZone(ZoneId.from(ZoneOffset.UTC)).format( |
| Instant.now()) + '</xml>' |
| ) |
| outputFile.set("xml/generated.xml") |
| } |
| } |
| |
| variant.sources.res.addGeneratedSourceDirectory(resGenerationTask, { it.getOutputFolder() }) |
| }) |
| } |