diff options
author | Yigit Boyar <yboyar@google.com> | 2022-07-06 16:24:36 -0700 |
---|---|---|
committer | Yigit Boyar <yboyar@google.com> | 2022-07-06 16:27:13 -0700 |
commit | 99ae0caa0f2995f0018df9d56dbdc7793e4ef13c (patch) | |
tree | 7bbbc0fc5599c44ad623cbba4dabc74d5614e172 | |
parent | 5998fb8449f0e19ae7f7db9462f26b44c33f7bd1 (diff) | |
download | data-binding-99ae0caa0f2995f0018df9d56dbdc7793e4ef13c.tar.gz |
Update transform api used in library builds
This CL updates the data binding build file to no longer use
the deprecated transform API that will be removed in 8.0.
Instead, it uses the new androidComponents API.
Fixes: 237633659
Test: existing tests and manual check on the generated aar files
Change-Id: I315bb377f0497220c1232cb82561273de1c1c1a9
-rw-r--r-- | extensions-support/library/build.gradle | 98 | ||||
-rw-r--r-- | extensions/library/build.gradle | 98 |
2 files changed, 68 insertions, 128 deletions
diff --git a/extensions-support/library/build.gradle b/extensions-support/library/build.gradle index 3160bebc..38abde26 100644 --- a/extensions-support/library/build.gradle +++ b/extensions-support/library/build.gradle @@ -13,19 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import java.nio.file.StandardCopyOption; -import java.nio.file.Files; - -import com.android.build.api.transform.Format; -import com.android.build.api.transform.QualifiedContent; -import com.android.build.api.transform.QualifiedContent.ContentType; -import com.android.build.api.transform.QualifiedContent.Scope; -import com.android.build.api.transform.Transform; -import com.android.build.api.transform.Context; -import com.android.build.api.transform.TransformInput; -import com.android.build.api.transform.TransformOutputProvider; -import com.android.build.api.transform.TransformException; -import com.android.build.gradle.internal.pipeline.TransformManager; +import com.android.build.api.artifact.MultipleArtifact; // Top-level build file where you can add dataBindingConfiguration options common to all sub-projects/modules. apply plugin: 'com.android.library' @@ -61,6 +49,17 @@ android { consumerProguardFiles 'proguard-consumer-rules.pro' } + androidComponents { + onVariants(selector().all(), { variant -> + TaskProvider<ExcludeShimsTask> taskProvider = project.tasks.register( + variant.getName() + "ExcludeShimsTask", ExcludeShimsTask.class + ) + variant.artifacts.use(taskProvider) + .wiredWith( { it.getAllClasses() }, { it.getOutput() }) + .toTransform(MultipleArtifact.ALL_CLASSES_DIRS.INSTANCE) + }) + } + publishing { singleVariant("release") } @@ -110,62 +109,33 @@ afterEvaluate { } } - -class ExcludeShimTransform extends Transform { - Project project; - public ExcludeShimTransform(Project project) { - this.project = project; - } - public Set<ContentType> getInputTypes() { - return TransformManager.CONTENT_CLASS; - } - - public Set<Scope> getScopes() { - def result = new HashSet<Scope>(); - result.add(Scope.PROJECT); - return result; - } - - public Set<Scope> getReferencedScopes() { - return TransformManager.SCOPE_FULL_LIBRARY_WITH_LOCAL_JARS; - } - - public boolean isIncremental() { - return false; - } - - public String getName() { - return "DataBindingExcludeShimTransform"; - } - - public void transform(Context context, Collection<TransformInput> inputs, - Collection<TransformInput> referencedInputs, - TransformOutputProvider outputProvider, - boolean isIncremental) throws IOException, TransformException, InterruptedException { - inputs.each { transformInput -> - transformInput.getDirectoryInputs().each { - File outputDir = outputProvider.getContentLocation("data-binding-filtered", - it.getContentTypes(), it.getScopes(), Format.DIRECTORY); - outputDir.delete(); - outputDir.mkdirs(); - FileTree tree = project.fileTree(dir: it.getFile()) - tree.include '**/*.class' - tree.exclude 'android/databinding/DataBindingComponent.*' - tree.exclude 'android/databinding/DataBinderMapperImpl.*' - java.nio.file.Path sourcePath = it.getFile().toPath() - java.nio.file.Path outputPath = outputDir.toPath() - tree.each { file -> - java.nio.file.Path fileOutputPath = outputPath.resolve(sourcePath.relativize(file.toPath())) - fileOutputPath.toFile().getParentFile().mkdirs() - Files.copy(file.toPath(), fileOutputPath , StandardCopyOption.REPLACE_EXISTING) - } +/** + * Remove shim classes that will be generated by the application annotation processor. + */ +abstract class ExcludeShimsTask extends DefaultTask { + @InputFiles + abstract ListProperty<Directory> getAllClasses(); + + @OutputDirectory + abstract DirectoryProperty getOutput(); + + @TaskAction + def excludeShimClasses() { + File outputDir = output.get().asFile + outputDir.delete(); + outputDir.mkdirs(); + + allClasses.get().forEach { directory -> + project.copy { + from directory + into output + exclude 'android/databinding/DataBindingComponent.*' + exclude 'android/databinding/DataBinderMapperImpl.*' } } } } -android.registerTransform(new ExcludeShimTransform(project)) - task prebuildAar(type : Copy) { dependsOn tasks.named("publish") from "$buildDir/outputs/aar/library-release.aar" diff --git a/extensions/library/build.gradle b/extensions/library/build.gradle index 04436147..13a50f66 100644 --- a/extensions/library/build.gradle +++ b/extensions/library/build.gradle @@ -13,20 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import java.nio.file.StandardCopyOption; -import java.nio.file.Files; - -import com.android.build.api.transform.Format; -import com.android.build.api.transform.QualifiedContent; -import com.android.build.api.transform.QualifiedContent.ContentType; -import com.android.build.api.transform.QualifiedContent.Scope; -import com.android.build.api.transform.Transform; -import com.android.build.api.transform.Context; -import com.android.build.api.transform.TransformInput; -import com.android.build.api.transform.TransformOutputProvider; -import com.android.build.api.transform.TransformException; -import com.android.build.gradle.internal.pipeline.TransformManager; -// Top-level build file where you can add dataBindingConfiguration options common to all sub-projects/modules. +import com.android.build.api.artifact.MultipleArtifact; apply plugin: 'com.android.library' @@ -61,6 +48,17 @@ android { consumerProguardFiles 'proguard-consumer-rules.pro' } + androidComponents { + onVariants(selector().all(), { variant -> + TaskProvider<ExcludeShimsTask> taskProvider = project.tasks.register( + variant.getName() + "ExcludeShimsTask", ExcludeShimsTask.class + ) + variant.artifacts.use(taskProvider) + .wiredWith( { it.getAllClasses() }, { it.getOutput() }) + .toTransform(MultipleArtifact.ALL_CLASSES_DIRS.INSTANCE) + }) + } + publishing { singleVariant("release") } @@ -112,61 +110,33 @@ afterEvaluate { } } -class ExcludeShimTransform extends Transform { - Project project; - public ExcludeShimTransform(Project project) { - this.project = project; - } - public Set<ContentType> getInputTypes() { - return TransformManager.CONTENT_CLASS; - } - - public Set<Scope> getScopes() { - def result = new HashSet<Scope>(); - result.add(Scope.PROJECT); - return result; - } - - public Set<Scope> getReferencedScopes() { - return TransformManager.SCOPE_FULL_LIBRARY_WITH_LOCAL_JARS; - } - - public boolean isIncremental() { - return false; - } - - public String getName() { - return "DataBindingExcludeShimTransform"; - } - - public void transform(Context context, Collection<TransformInput> inputs, - Collection<TransformInput> referencedInputs, - TransformOutputProvider outputProvider, - boolean isIncremental) throws IOException, TransformException, InterruptedException { - inputs.each { transformInput -> - transformInput.getDirectoryInputs().each { - File outputDir = outputProvider.getContentLocation("data-binding-filtered", - it.getContentTypes(), it.getScopes(), Format.DIRECTORY); - outputDir.delete(); - outputDir.mkdirs(); - FileTree tree = project.fileTree(dir: it.getFile()) - tree.include '**/*.class' - tree.exclude 'androidx/databinding/DataBindingComponent.*' - tree.exclude 'androidx/databinding/DataBinderMapperImpl.*' - java.nio.file.Path sourcePath = it.getFile().toPath() - java.nio.file.Path outputPath = outputDir.toPath() - tree.each { file -> - java.nio.file.Path fileOutputPath = outputPath.resolve(sourcePath.relativize(file.toPath())) - fileOutputPath.toFile().getParentFile().mkdirs() - Files.copy(file.toPath(), fileOutputPath , StandardCopyOption.REPLACE_EXISTING) - } +/** + * Remove shim classes that will be generated by the application annotation processor. + */ +abstract class ExcludeShimsTask extends DefaultTask { + @InputFiles + abstract ListProperty<Directory> getAllClasses(); + + @OutputDirectory + abstract DirectoryProperty getOutput(); + + @TaskAction + def excludeShimClasses() { + File outputDir = output.get().asFile + outputDir.delete(); + outputDir.mkdirs(); + + allClasses.get().forEach { directory -> + project.copy { + from directory + into output + exclude 'androidx/databinding/DataBindingComponent.*' + exclude 'androidx/databinding/DataBinderMapperImpl.*' } } } } -android.registerTransform(new ExcludeShimTransform(project)) - task prebuildAar(type : Copy) { dependsOn tasks.named("publish") from "$buildDir/outputs/aar/library-release.aar" |