diff options
author | Yigit Boyar <yboyar@google.com> | 2015-06-12 13:13:17 -0700 |
---|---|---|
committer | Yigit Boyar <yboyar@google.com> | 2015-06-12 14:15:07 -0700 |
commit | b6887f1479c3ecec38a7989748ef33de1fbcd973 (patch) | |
tree | 8d8ff47f19e1ce3993d952bcb6e320b61cbe0399 /gradlePlugin/src/main/java | |
parent | 30b1bbbcf006dc6600f86a4e4d1bdf6d8a218351 (diff) | |
download | data-binding-b6887f1479c3ecec38a7989748ef33de1fbcd973.tar.gz |
Export generated class list from javac
Previously, gradle plugin would figure out generated class names
that should be excluded from the packaging. This CL changes that
behavior to export the list from java compiler so that it is
consistent going forward.
This CL also changes exclusion task to always exclude generated
binding info class to not to leak any information about user's
local.
Bug: 21668472
Change-Id: Ibeed24bd854781942b4185f618a5cd1eafe706d3
Diffstat (limited to 'gradlePlugin/src/main/java')
-rw-r--r-- | gradlePlugin/src/main/java/android/databinding/tool/DataBindingExcludeGeneratedTask.java | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/gradlePlugin/src/main/java/android/databinding/tool/DataBindingExcludeGeneratedTask.java b/gradlePlugin/src/main/java/android/databinding/tool/DataBindingExcludeGeneratedTask.java new file mode 100644 index 00000000..cfc7a98a --- /dev/null +++ b/gradlePlugin/src/main/java/android/databinding/tool/DataBindingExcludeGeneratedTask.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.databinding.tool; + +import com.google.common.base.Preconditions; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.gradle.api.DefaultTask; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.TaskAction; +import org.gradle.api.tasks.bundling.Jar; + +import android.databinding.tool.util.L; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; + +/** + * Task to exclude generated classes from the Jar task of a library project + */ +public class DataBindingExcludeGeneratedTask extends DefaultTask { + private String appPackage; + private String infoClassQualifiedName; + @Input + private File generatedClassListFile; + private boolean isLibrary; + + private org.gradle.api.tasks.bundling.Jar packageTask; + private final String EXCLUDE_PATTERN = "android/databinding/layouts/*.*"; + + public void setAppPackage(String appPackage) { + this.appPackage = appPackage; + } + + public void setInfoClassQualifiedName(String infoClassQualifiedName) { + this.infoClassQualifiedName = infoClassQualifiedName; + } + + public void setLibrary(boolean isLibrary) { + this.isLibrary = isLibrary; + } + + public void setPackageTask(Jar packageTask) { + this.packageTask = packageTask; + } + + public void setGeneratedClassListFile(File generatedClassListFile) { + this.generatedClassListFile = generatedClassListFile; + } + + public String getAppPackage() { + return appPackage; + } + + public String getInfoClassQualifiedName() { + return infoClassQualifiedName; + } + + public File getGeneratedClassListFile() { + return generatedClassListFile; + } + + @TaskAction + public void excludeGenerated() { + L.d("Excluding generated classes from jar. Is library ? %s", isLibrary); + String appPkgAsClass = appPackage.replace('.', '/'); + String infoClassAsClass = infoClassQualifiedName.replace('.', '/'); + exclude(infoClassAsClass + ".class"); + exclude(EXCLUDE_PATTERN); + if (isLibrary) { + exclude(appPkgAsClass + "/BR.*"); + List<String> generatedClasses = readGeneratedClasses(); + for (String klass : generatedClasses) { + exclude(klass.replace('.', '/') + ".class"); + } + } + L.d("Excluding generated classes from library jar is done."); + } + + private void exclude(String pattern) { + L.d("exclude %s", pattern); + packageTask.exclude(pattern); + } + + private List<String> readGeneratedClasses() { + Preconditions.checkNotNull(generatedClassListFile, "Data binding exclude generated task" + + " is not configured properly"); + Preconditions.checkArgument(generatedClassListFile.exists(), + "Generated class list does not exist %s", generatedClassListFile.getAbsolutePath()); + FileInputStream fis = null; + try { + fis = new FileInputStream(generatedClassListFile); + return IOUtils.readLines(fis); + } catch (FileNotFoundException e) { + L.e(e, "Unable to read generated class list from %s", + generatedClassListFile.getAbsoluteFile()); + } catch (IOException e) { + L.e(e, "Unexpected exception while reading %s", + generatedClassListFile.getAbsoluteFile()); + } finally { + IOUtils.closeQuietly(fis); + } + Preconditions.checkState(false, "Could not read data binding generated class list"); + return null; + } +} |