diff options
author | Xavier Ducrohet <xav@google.com> | 2013-11-22 13:38:51 -0800 |
---|---|---|
committer | Xavier Ducrohet <xav@google.com> | 2013-11-22 14:49:05 -0800 |
commit | 6dde98d5274118574ae29a19665aef7c5f9d1800 (patch) | |
tree | 9ab625fcd5188b7261bbf112826e171b39e2a753 | |
parent | 6c7f1356dfc68aac0cfc5cdbb6d1e84fd9e55f21 (diff) | |
download | build-6dde98d5274118574ae29a19665aef7c5f9d1800.tar.gz |
Add API to specify new generated source folders.
Change-Id: I64085181917a81deadac94889adbb37b41298ac0
13 files changed, 210 insertions, 5 deletions
diff --git a/changelog.txt b/changelog.txt index 21148bc..70ed31e 100644 --- a/changelog.txt +++ b/changelog.txt @@ -11,6 +11,10 @@ - Added ability to test proguarded (obfuscated) apps. - Jar files are now pre-dexed for faster dexing. - First pass at NDK integration +- API to add new generated source folders: + variant.addGeneratedSourceFolders(generatingTask, sourceFolder1, sourceFolders2,...) + This automatically adds the dependency on the task, sets up the JavaCompile task inputs and propagates + the folders to the model for IDE integration. 0.6.3 - Fixed ClassNotFoundException:MergingException introduced in 0.6.2 diff --git a/gradle-model/src/test/java/com/android/build/gradle/model/AndroidProjectTest.java b/gradle-model/src/test/java/com/android/build/gradle/model/AndroidProjectTest.java index 4118c87..cee947a 100644 --- a/gradle-model/src/test/java/com/android/build/gradle/model/AndroidProjectTest.java +++ b/gradle-model/src/test/java/com/android/build/gradle/model/AndroidProjectTest.java @@ -467,6 +467,40 @@ public class AndroidProjectTest extends TestCase { assertEquals("jar dep count", 2, jars.size()); } + public void testGenFolderApi() throws Exception { + // Load the custom model for the project + ProjectData projectData = getModelForProject("genFolderApi"); + + AndroidProject model = projectData.model; + File projectDir = projectData.projectDir; + + File buildDir = new File(projectDir, "build"); + + for (Variant variant : model.getVariants().values()) { + + ArtifactInfo mainInfo = variant.getMainArtifactInfo(); + assertNotNull( + "Null-check on mainArtifactInfo for " + variant.getDisplayName(), + mainInfo); + + // get the generated source folders. + List<File> genFolder = mainInfo.getGeneratedSourceFolders(); + + // We're looking for a custom folder + String folderStart = new File(buildDir, "customCode").getAbsolutePath() + File.separatorChar; + boolean found = false; + for (File f : genFolder) { + if (f.getAbsolutePath().startsWith(folderStart)) { + found = true; + break; + } + } + + assertTrue("custom generated source folder check", found); + } + } + + /** * Returns the SDK folder as built from the Android source tree. * @return the SDK diff --git a/gradle/src/build-test/groovy/com/android/build/gradle/AutomatedBuildTest.java b/gradle/src/build-test/groovy/com/android/build/gradle/AutomatedBuildTest.java index b7fcc98..5372005 100644 --- a/gradle/src/build-test/groovy/com/android/build/gradle/AutomatedBuildTest.java +++ b/gradle/src/build-test/groovy/com/android/build/gradle/AutomatedBuildTest.java @@ -35,11 +35,11 @@ public class AutomatedBuildTest extends BuildTest { private static final String[] sBuiltProjects = new String[] { "aidl", "api", "applibtest", "assets", "attrOrder", "basic", "dependencies", - "dependencyChecker", "flavored", "flavorlib", "flavors", "libProguardJarDep", - "libProguardLibDep", "libTestDep", "libsTest", "localJars", "migrated", "multiproject", - "multires", "ndkSanAngeles", "overlay1", "overlay2", "pkgOverride", "proguard", - "proguardLib", "renderscript", "renderscriptInLib", "renderscriptMultiSrc", - "rsSupportMode", "sameNamedLibs", "tictactoe" /*, "autorepo"*/ + "dependencyChecker", "flavored", "flavorlib", "flavors", "genFolderApi", + "libProguardJarDep", "libProguardLibDep", "libTestDep", "libsTest", "localJars", + "migrated", "multiproject", "multires", "ndkSanAngeles", "overlay1", "overlay2", + "pkgOverride", "proguard", "proguardLib", "renderscript", "renderscriptInLib", + "renderscriptMultiSrc", "rsSupportMode", "sameNamedLibs", "tictactoe" /*, "autorepo"*/ }; private static final String[] sReportProjects = new String[] { diff --git a/gradle/src/main/groovy/com/android/build/gradle/api/BaseVariant.java b/gradle/src/main/groovy/com/android/build/gradle/api/BaseVariant.java index cfc1fd3..854f4a5 100644 --- a/gradle/src/main/groovy/com/android/build/gradle/api/BaseVariant.java +++ b/gradle/src/main/groovy/com/android/build/gradle/api/BaseVariant.java @@ -152,4 +152,26 @@ public interface BaseVariant { */ @Nullable Task getAssemble(); + + /** + * Adds to the variant a task that generates Java source Code. + * + * This will make the compileJava task depend on this task and add the + * new source folders as compilation inputs. + * + * @param task the task + * @param sourceFolders the source folders where the generated source code is. + */ + void addGeneratedSourceFolders(@NonNull Task task, @NonNull File... sourceFolders); + + /** + * Adds to the variant a task that generates Java source Code. + * + * This will make the compileJava task depend on this task and add the + * new source folders as compilation inputs. + * + * @param task the task + * @param sourceFolders the source folders where the generated source code is. + */ + void addGeneratedSourceFolders(@NonNull Task task, @NonNull Iterable<File> sourceFolders); } diff --git a/gradle/src/main/groovy/com/android/build/gradle/internal/api/BaseVariantImpl.java b/gradle/src/main/groovy/com/android/build/gradle/internal/api/BaseVariantImpl.java index e49b7c0..94ee0ab 100644 --- a/gradle/src/main/groovy/com/android/build/gradle/internal/api/BaseVariantImpl.java +++ b/gradle/src/main/groovy/com/android/build/gradle/internal/api/BaseVariantImpl.java @@ -137,4 +137,14 @@ abstract class BaseVariantImpl implements BaseVariant { public Task getAssemble() { return getVariantData().assembleTask; } + + @Override + public void addGeneratedSourceFolders(@NonNull Task task, @NonNull File... sourceFolders) { + getVariantData().addGeneratedSourceFolders(task, sourceFolders); + } + + @Override + public void addGeneratedSourceFolders(@NonNull Task task, @NonNull Iterable<File> sourceFolders) { + getVariantData().addGeneratedSourceFolders(task, sourceFolders); + } } diff --git a/gradle/src/main/groovy/com/android/build/gradle/internal/model/ModelBuilder.groovy b/gradle/src/main/groovy/com/android/build/gradle/internal/model/ModelBuilder.groovy index 037b789..c3ede88 100644 --- a/gradle/src/main/groovy/com/android/build/gradle/internal/model/ModelBuilder.groovy +++ b/gradle/src/main/groovy/com/android/build/gradle/internal/model/ModelBuilder.groovy @@ -220,6 +220,11 @@ public class ModelBuilder implements ToolingModelBuilder { folders.add(variantData.renderscriptCompileTask.sourceOutputDir) folders.add(variantData.generateBuildConfigTask.sourceOutputDir) + List<File> extraFolders = variantData.extraGeneratedSourceFolders + if (extraFolders != null) { + folders.addAll(extraFolders) + } + return folders } diff --git a/gradle/src/main/groovy/com/android/build/gradle/internal/variant/BaseVariantData.java b/gradle/src/main/groovy/com/android/build/gradle/internal/variant/BaseVariantData.java index 84241c5..6c68201 100644 --- a/gradle/src/main/groovy/com/android/build/gradle/internal/variant/BaseVariantData.java +++ b/gradle/src/main/groovy/com/android/build/gradle/internal/variant/BaseVariantData.java @@ -30,6 +30,7 @@ import com.android.build.gradle.tasks.ProcessAndroidResources; import com.android.build.gradle.tasks.ProcessManifest; import com.android.build.gradle.tasks.RenderscriptCompile; import com.android.builder.VariantConfiguration; +import com.google.common.collect.Lists; import groovy.lang.Closure; import org.gradle.api.Task; import org.gradle.api.tasks.Copy; @@ -37,6 +38,7 @@ import org.gradle.api.tasks.compile.JavaCompile; import proguard.gradle.ProGuardTask; import java.io.File; +import java.util.List; /** * Base data about a variant. @@ -67,6 +69,8 @@ public abstract class BaseVariantData { public Task assembleTask; + private List<File> extraGeneratedSourceFolders; + public BaseVariantData(@NonNull VariantConfiguration variantConfiguration) { this.variantConfiguration = variantConfiguration; } @@ -124,4 +128,35 @@ public abstract class BaseVariantData { String getName() { return variantConfiguration.getFullName(); } + + @Nullable + public List<File> getExtraGeneratedSourceFolders() { + return extraGeneratedSourceFolders; + } + + public void addGeneratedSourceFolders(@NonNull Task task, @NonNull File... generatedSourceFolders) { + if (extraGeneratedSourceFolders == null) { + extraGeneratedSourceFolders = Lists.newArrayList(); + } + + javaCompileTask.dependsOn(task); + + for (File f : generatedSourceFolders) { + extraGeneratedSourceFolders.add(f); + javaCompileTask.source(f); + } + } + + public void addGeneratedSourceFolders(@NonNull Task task, @NonNull Iterable<File> generatedSourceFolders) { + if (extraGeneratedSourceFolders == null) { + extraGeneratedSourceFolders = Lists.newArrayList(); + } + + javaCompileTask.dependsOn(task); + + for (File f : generatedSourceFolders) { + extraGeneratedSourceFolders.add(f); + javaCompileTask.source(f); + } + } } diff --git a/tests/genFolderApi/build.gradle b/tests/genFolderApi/build.gradle new file mode 100644 index 0000000..542d956 --- /dev/null +++ b/tests/genFolderApi/build.gradle @@ -0,0 +1,45 @@ +buildscript { + repositories { + maven { url '../../../../out/host/gradle/repo' } + } + dependencies { + classpath 'com.android.tools.build:gradle:0.7.0-SNAPSHOT' + } +} +apply plugin: 'android' + +android { + compileSdkVersion 15 + buildToolsVersion "18.0.1" +} + + +public class GenerateCode extends DefaultTask { + @Input + String value + + @OutputFile + File outputFile + + @TaskAction + void taskAction() { + getOutputFile().text = + "package com.custom;\n" + + "public class Foo {\n" + + " public static String getBuildDate() { return \"${getValue()}\"; }\n" + + "}\n"; + } +} + + +android.applicationVariants.all { variant -> + + // create a task that generates a java class + File sourceFolder = file("${buildDir}/customCode/${variant.dirName}") + def javaGenerationTask = tasks.create(name: "generatedCodeFor${variant.name.capitalize()}", type: GenerateCode) { + value new Date().format("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC")) + outputFile file("${sourceFolder.absolutePath}/com/custom/Foo.java") + } + + variant.addGeneratedSourceFolders(javaGenerationTask, sourceFolder) +}
\ No newline at end of file diff --git a/tests/genFolderApi/src/main/AndroidManifest.xml b/tests/genFolderApi/src/main/AndroidManifest.xml new file mode 100644 index 0000000..a34d937 --- /dev/null +++ b/tests/genFolderApi/src/main/AndroidManifest.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.tests.basic"> + <application android:label="@string/app_name" android:icon="@drawable/icon"> + <activity android:name=".Main" + android:label="@string/app_name"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + </application> +</manifest> diff --git a/tests/genFolderApi/src/main/java/com/android/tests/basic/Main.java b/tests/genFolderApi/src/main/java/com/android/tests/basic/Main.java new file mode 100644 index 0000000..df05828 --- /dev/null +++ b/tests/genFolderApi/src/main/java/com/android/tests/basic/Main.java @@ -0,0 +1,19 @@ +package com.android.tests.basic; + +import android.app.Activity; +import android.os.Bundle; +import android.widget.TextView; + +public class Main extends Activity +{ + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + + TextView tv = (TextView) findViewById(R.id.text); + tv.setText(com.custom.Foo.getBuildDate()); + } +} diff --git a/tests/genFolderApi/src/main/res/drawable/icon.png b/tests/genFolderApi/src/main/res/drawable/icon.png Binary files differnew file mode 100644 index 0000000..a07c69f --- /dev/null +++ b/tests/genFolderApi/src/main/res/drawable/icon.png diff --git a/tests/genFolderApi/src/main/res/layout/main.xml b/tests/genFolderApi/src/main/res/layout/main.xml new file mode 100644 index 0000000..ee817cf --- /dev/null +++ b/tests/genFolderApi/src/main/res/layout/main.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + > +<TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="Test App - GenFolderApi" + android:id="@+id/text" + /> +</LinearLayout> + diff --git a/tests/genFolderApi/src/main/res/values/strings.xml b/tests/genFolderApi/src/main/res/values/strings.xml new file mode 100644 index 0000000..64ee88e --- /dev/null +++ b/tests/genFolderApi/src/main/res/values/strings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="app_name">_Test-genFolderApi</string> +</resources> |