aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2013-10-03 11:19:26 -0700
committerTor Norbye <tnorbye@google.com>2013-10-03 14:07:35 -0700
commit14e30af3597c9f80b5e433c5b671e920c2f27a14 (patch)
tree5a7a4975cdaf860dbb1f3a80a19ea0272d8d7d43
parent859f4a403897a38acfbcdf57750c5885a03ac386 (diff)
downloadbuild-14e30af3597c9f80b5e433c5b671e920c2f27a14.tar.gz
Make lint task use the plugin's SDK home, not environment
This makes the lint task pick up the SDK configured in local.properties, rather than just relying on $ANDROID_HOME. Also cleans up the Lint task such that it only performs work when the lint task is actually *run*, not just constructed (which will happen at every startup, for every variant -- not just when you're runnign lint). This is also important since it used to actually perform output file validation such as deleting the previous contents, which it should only do when you're actually running lint. Change-Id: I46224e7f6e558e88653fcef711581d302c1adfc5
-rw-r--r--gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy1
-rw-r--r--gradle/src/main/groovy/com/android/build/gradle/internal/LintGradleClient.java17
-rw-r--r--gradle/src/main/groovy/com/android/build/gradle/tasks/Lint.groovy200
3 files changed, 143 insertions, 75 deletions
diff --git a/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy b/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy
index 7731366..ab1acb4 100644
--- a/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy
+++ b/gradle/src/main/groovy/com/android/build/gradle/BasePlugin.groovy
@@ -792,6 +792,7 @@ public abstract class BasePlugin {
Task lintCheck = project.tasks.create("lint" + variantName, Lint)
lintCheck.dependsOn baseVariantData.javaCompileTask, lintCompile
lint.dependsOn lintCheck
+ lintCheck.setPlugin(this)
String outputName = "$project.buildDir/lint/" + variantName
VariantConfiguration config = baseVariantData.variantConfiguration
diff --git a/gradle/src/main/groovy/com/android/build/gradle/internal/LintGradleClient.java b/gradle/src/main/groovy/com/android/build/gradle/internal/LintGradleClient.java
index e565bcc..6833ffc 100644
--- a/gradle/src/main/groovy/com/android/build/gradle/internal/LintGradleClient.java
+++ b/gradle/src/main/groovy/com/android/build/gradle/internal/LintGradleClient.java
@@ -19,6 +19,7 @@ package com.android.build.gradle.internal;
import com.google.common.collect.Lists;
import com.android.annotations.NonNull;
+import com.android.annotations.Nullable;
import com.android.tools.lint.LintCliClient;
import com.android.tools.lint.LintCliFlags;
import com.android.tools.lint.detector.api.Project;
@@ -28,9 +29,11 @@ import java.util.List;
public class LintGradleClient extends LintCliClient {
private List<File> mCustomRules = Lists.newArrayList();
+ private File mySdkHome;
- public LintGradleClient(LintCliFlags flags) {
+ public LintGradleClient(@NonNull LintCliFlags flags, @Nullable File sdkHome) {
super(flags);
+ mySdkHome = sdkHome;
}
public void setCustomRules(List<File> customRules) {
@@ -46,4 +49,16 @@ public class LintGradleClient extends LintCliClient {
protected Project createProject(@NonNull File dir, @NonNull File referenceDir) {
return new LintGradleProject(this, dir, referenceDir);
}
+
+ public void setSdkHome(File home) {
+ mySdkHome = home;
+ }
+
+ @Override
+ public File getSdkHome() {
+ if (mySdkHome != null) {
+ return mySdkHome;
+ }
+ return super.getSdkHome();
+ }
}
diff --git a/gradle/src/main/groovy/com/android/build/gradle/tasks/Lint.groovy b/gradle/src/main/groovy/com/android/build/gradle/tasks/Lint.groovy
index 3aa884d..2c756ce 100644
--- a/gradle/src/main/groovy/com/android/build/gradle/tasks/Lint.groovy
+++ b/gradle/src/main/groovy/com/android/build/gradle/tasks/Lint.groovy
@@ -16,6 +16,9 @@
package com.android.build.gradle.tasks
+import com.android.annotations.NonNull
+import com.android.annotations.Nullable
+import com.android.build.gradle.BasePlugin
import com.android.build.gradle.internal.LintGradleClient
import com.android.tools.lint.HtmlReporter
import com.android.tools.lint.LintCliFlags
@@ -29,58 +32,41 @@ import org.gradle.api.GradleException
import org.gradle.api.tasks.TaskAction
public class Lint extends DefaultTask {
+ @NonNull private BasePlugin mPlugin
+ @Nullable private List<File> mCustomRules
+ @Nullable private File mConfigFile
+ @Nullable private File mHtmlOutput
+ @Nullable private File mXmlOutput
+ @Nullable private List<Set<File>> mSourceSets
+ @Nullable private String mClassPath
+ @Nullable private List<Set<File>> mResourceSets
+ private boolean mQuiet
+
+ public void setPlugin(@NonNull BasePlugin plugin) {
+ mPlugin = plugin
+ }
- private LintCliFlags flags = new LintCliFlags()
- private LintGradleClient client = new LintGradleClient(flags)
- private IssueRegistry registry = new BuiltinIssueRegistry()
- private List<File> customRules = new ArrayList<File>()
-
- public void addCustomRule(File f) {
- customRules.add(f)
+ public void addCustomRule(@NonNull File jar) {
+ if (mCustomRules == null) {
+ mCustomRules = new ArrayList<File>()
+ }
+ mCustomRules.add(jar)
}
public void setQuiet() {
- flags.setQuiet(true)
+ mQuiet = true
}
- public void setConfig(File f) {
- flags.setDefaultConfiguration(client.createConfigurationFromFile(f))
+ public void setConfig(@NonNull File configFile) {
+ mConfigFile = configFile
}
- public void setHtmlOutput(File output) {
- output = output.getAbsoluteFile()
- if (output.exists()) {
- boolean delete = output.delete()
- if (!delete) {
- throw new GradleException("Could not delete old " + output)
- }
- }
- if (output.getParentFile() != null && !output.getParentFile().canWrite()) {
- throw new GradleException("Cannot write HTML output file " + output)
- }
- try {
- flags.getReporters().add(new HtmlReporter(client, output))
- } catch (IOException e) {
- throw new GradleException("HTML invalid argument.")
- }
+ public void setHtmlOutput(@NonNull File htmlOutput) {
+ mHtmlOutput = htmlOutput
}
- public void setXmlOutput(File output) {
- output = output.getAbsoluteFile()
- if (output.exists()) {
- boolean delete = output.delete();
- if (!delete) {
- throw new GradleException("Could not delete old " + output)
- }
- }
- if (output.getParentFile() != null && !output.getParentFile().canWrite()) {
- throw new GradleException("Cannot write XML output file " + output)
- }
- try {
- flags.getReporters().add(new XmlReporter(client, output))
- } catch (IOException e) {
- throw new GradleException("XML invalid argument.")
- }
+ public void setXmlOutput(@NonNull File xmlOutput) {
+ mXmlOutput = xmlOutput;
}
/**
@@ -88,8 +74,97 @@ public class Lint extends DefaultTask {
*
* @param sourceSets files to be added to sources.
*/
- public void setSources(List<Set<File>> sourceSets) {
- for (Set<File> args : sourceSets) {
+ public void setSources(@NonNull List<Set<File>> sourceSets) {
+ mSourceSets = sourceSets
+ }
+
+ /**
+ * Adds all class files in directory specified by paths for lint.
+ *
+ * @param paths A set of paths to class files separated with path separators
+ */
+ public void setClasspath(@NonNull String paths) {
+ mClassPath = paths
+ }
+
+ /**
+ * Adds all files in resourceSets as a resource file for lint.
+ *
+ * @param resourceSets files to be added to resources.
+ */
+ public void setLintResources(@NonNull List<Set<File>> resourceSets) {
+ mResourceSets = resourceSets
+ }
+
+ @SuppressWarnings("GroovyUnusedDeclaration")
+ @TaskAction
+ public void lint() {
+ IssueRegistry registry = new BuiltinIssueRegistry()
+ LintCliFlags flags = new LintCliFlags()
+ LintGradleClient client = new LintGradleClient(flags, mPlugin.getSdkDirectory())
+
+ // Configure Reporters
+
+ if (mHtmlOutput != null) {
+ mHtmlOutput = mHtmlOutput.getAbsoluteFile()
+ if (mHtmlOutput.exists()) {
+ boolean delete = mHtmlOutput.delete()
+ if (!delete) {
+ throw new GradleException("Could not delete old " + mHtmlOutput)
+ }
+ }
+ if (mHtmlOutput.getParentFile() != null && !mHtmlOutput.getParentFile().canWrite()) {
+ throw new GradleException("Cannot write HTML output file " + mHtmlOutput)
+ }
+ try {
+ flags.getReporters().add(new HtmlReporter(client, mHtmlOutput))
+ } catch (IOException e) {
+ throw new GradleException("HTML invalid argument.", e)
+ }
+ }
+
+ if (mXmlOutput != null) {
+ mXmlOutput = mXmlOutput.getAbsoluteFile()
+ if (mXmlOutput.exists()) {
+ boolean delete = mXmlOutput.delete();
+ if (!delete) {
+ throw new GradleException("Could not delete old " + mXmlOutput)
+ }
+ }
+ if (mXmlOutput.getParentFile() != null && !mXmlOutput.getParentFile().canWrite()) {
+ throw new GradleException("Cannot write XML output file " + mXmlOutput)
+ }
+ try {
+ flags.getReporters().add(new XmlReporter(client, mXmlOutput))
+ } catch (IOException e) {
+ throw new GradleException("XML invalid argument.", e)
+ }
+ }
+
+ List<Reporter> reporters = flags.getReporters()
+ if (reporters.isEmpty()) {
+ throw new GradleException("No reporter specified.")
+ }
+
+ Map<String, String> map = new HashMap<String, String>(){{
+ put("", "file://")
+ }}
+ for (Reporter reporter : reporters) {
+ reporter.setUrlMap(map)
+ }
+
+ // Flags
+
+ if (mQuiet) {
+ flags.setQuiet(true)
+ }
+ if (mConfigFile != null) {
+ flags.setDefaultConfiguration(client.createConfigurationFromFile(mConfigFile))
+ }
+
+ // Flags: sources, resources, classes
+
+ for (Set<File> args : mSourceSets) {
for (File input : args) {
if (input.exists()) {
List<File> sources = flags.getSourcesOverride()
@@ -101,15 +176,8 @@ public class Lint extends DefaultTask {
}
}
}
- }
- /**
- * Adds all class files in directory specified by paths for lint.
- *
- * @param paths The absolute path to a directory containing class files
- */
- public void setClasspath(String paths) {
- for (String path : LintUtils.splitPath(paths)) {
+ for (String path : LintUtils.splitPath(mClassPath)) {
File input = new File(path);
if (!input.exists()) {
throw new GradleException("Class path entry " + input + " does not exist.")
@@ -121,15 +189,8 @@ public class Lint extends DefaultTask {
}
classes.add(input);
}
- }
- /**
- * Adds all files in resourceSets as a resource file for lint.
- *
- * @param resourceSets files to be added to resources.
- */
- public void setLintResources(List<Set<File>> resourceSets) {
- for (Set<File> args : resourceSets) {
+ for (Set<File> args : mResourceSets) {
for (File input : args) {
if (input.exists()) {
List<File> resources = flags.getResourcesOverride()
@@ -141,28 +202,19 @@ public class Lint extends DefaultTask {
}
}
}
- }
- @TaskAction
- public void lint() {
- List<Reporter> reporters = flags.getReporters()
- if (reporters.isEmpty()) {
- throw new GradleException("No reporter specified.")
- }
+ // Client setup
- Map<String, String> map = new HashMap<String, String>(){{
- put("", "file://")
- }}
- for (Reporter reporter : reporters) {
- reporter.setUrlMap(map)
+ if (mCustomRules != null) {
+ client.setCustomRules(mCustomRules)
}
- client.setCustomRules(customRules)
+ // Finally perform lint run
try {
client.run(registry, Arrays.asList(project.projectDir));
} catch (IOException e) {
- throw new GradleException("Invalid arguments.")
+ throw new GradleException("Invalid arguments.", e)
}
}
}