Skip to content

Commit ed8977b

Browse files
authored
Migrate metalava download to gradle's resolution process. (#1788)
* Migrate metalava download to gradle's resolution process. Additionally rewrite `GenerateStubsTask` to Kotlin. * Fix tests.
1 parent 1f78af9 commit ed8977b

File tree

11 files changed

+161
-171
lines changed

11 files changed

+161
-171
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ configure(subprojects) {
7676
google()
7777
jcenter()
7878
mavenLocal()
79+
maven {
80+
url 'https://storage.googleapis.com/android-ci/mvn/'
81+
}
7982
}
8083

8184
apply plugin: 'com.github.sherter.google-java-format'

buildSrc/src/main/java/com/google/firebase/gradle/plugins/FirebaseJavaLibraryPlugin.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.common.collect.ImmutableMap;
1919
import com.google.firebase.gradle.plugins.apiinfo.ApiInformationTask;
2020
import com.google.firebase.gradle.plugins.apiinfo.GenerateApiTxtFileTask;
21-
import com.google.firebase.gradle.plugins.apiinfo.GenerateStubsTask;
2221
import com.google.firebase.gradle.plugins.apiinfo.GetMetalavaJarTask;
2322
import com.google.firebase.gradle.plugins.ci.Coverage;
2423
import java.io.File;
@@ -161,8 +160,6 @@ private static void setupApiInformationAnalysis(Project project) {
161160
"docStubs",
162161
GenerateStubsTask.class,
163162
task -> {
164-
task.setMetalavaJarPath(metalavaOutputJarFile.getAbsolutePath());
165-
task.setOutputDir(new File(project.getBuildDir(), "doc-stubs"));
166163
task.dependsOn("getMetalavaJar");
167164

168165
task.setSourceSet(mainSourceSet);

buildSrc/src/main/java/com/google/firebase/gradle/plugins/FirebaseLibraryPlugin.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.common.collect.ImmutableMap;
2323
import com.google.firebase.gradle.plugins.apiinfo.ApiInformationTask;
2424
import com.google.firebase.gradle.plugins.apiinfo.GenerateApiTxtFileTask;
25-
import com.google.firebase.gradle.plugins.apiinfo.GenerateStubsTask;
2625
import com.google.firebase.gradle.plugins.apiinfo.GetMetalavaJarTask;
2726
import com.google.firebase.gradle.plugins.ci.Coverage;
2827
import com.google.firebase.gradle.plugins.ci.device.FirebaseTestServer;
@@ -102,7 +101,6 @@ public void apply(Project project) {
102101
.setFreeCompilerArgs(
103102
ImmutableList.of("-module-name", kotlinModuleName(project))));
104103

105-
106104
Dokka.configure(project, android, firebaseLibrary);
107105
}
108106

@@ -174,10 +172,6 @@ private static void setupApiInformationAnalysis(Project project, LibraryExtensio
174172
"docStubs",
175173
GenerateStubsTask.class,
176174
task -> {
177-
task.setMetalavaJarPath(metalavaOutputJarFile.getAbsolutePath());
178-
task.setOutputDir(new File(project.getBuildDir(), "doc-stubs"));
179-
task.dependsOn("getMetalavaJar");
180-
181175
task.setSourceSet(mainSourceSet);
182176
});
183177
project.getTasks().getByName("check").dependsOn(docStubs);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.gradle.plugins
16+
17+
import com.android.build.gradle.api.AndroidSourceSet
18+
import java.io.File
19+
import org.gradle.api.DefaultTask
20+
import org.gradle.api.Project
21+
import org.gradle.api.artifacts.Configuration
22+
import org.gradle.api.file.FileCollection
23+
import org.gradle.api.tasks.InputFiles
24+
import org.gradle.api.tasks.OutputDirectory
25+
import org.gradle.api.tasks.SourceSet
26+
import org.gradle.api.tasks.TaskAction
27+
28+
val Project.metalavaConfig: Configuration
29+
get() =
30+
configurations.findByName("metalavaArtifacts")
31+
?: configurations.create("metalavaArtifacts") {
32+
this.dependencies.add(this@metalavaConfig.dependencies.create("com.android:metalava:1.3.0"))
33+
}
34+
35+
fun Project.runMetalavaWithArgs(
36+
arguments: List<String>
37+
) {
38+
val allArgs = listOf(
39+
"--no-banner",
40+
"--hide",
41+
"HiddenSuperclass" // We allow having a hidden parent class
42+
) + arguments
43+
44+
project.javaexec {
45+
main = "com.android.tools.metalava.Driver"
46+
classpath = project.metalavaConfig
47+
args = allArgs
48+
}
49+
}
50+
51+
abstract class GenerateStubsTask : DefaultTask() {
52+
/** Source files against which API signatures will be validated. */
53+
lateinit var sourceSet: Object
54+
55+
@get:InputFiles
56+
lateinit var classPath: FileCollection
57+
58+
@get:OutputDirectory
59+
val outputDir: File = File(project.buildDir, "doc-stubs")
60+
61+
private val sourceDirs: Set<File>
62+
get() = with(sourceSet) {
63+
when (this) {
64+
is SourceSet -> java.srcDirs
65+
is AndroidSourceSet -> java.srcDirs
66+
else -> throw IllegalStateException("Unsupported sourceSet provided: $javaClass")
67+
}
68+
}
69+
70+
@TaskAction
71+
fun run() {
72+
val sourcePath = sourceDirs.asSequence()
73+
.filter { it.exists() }
74+
.map { it.absolutePath }
75+
.joinToString(":")
76+
77+
val classPath = classPath.files.asSequence()
78+
.map { it.absolutePath }.toMutableList()
79+
project.androidJar?.let {
80+
classPath += listOf(it.absolutePath)
81+
}
82+
83+
project.runMetalavaWithArgs(
84+
listOf(
85+
"--source-path",
86+
sourcePath,
87+
"--classpath",
88+
classPath.joinToString(":"),
89+
"--include-annotations",
90+
"--doc-stubs",
91+
outputDir.absolutePath
92+
))
93+
}
94+
}

buildSrc/src/main/java/com/google/firebase/gradle/plugins/SdkUtil.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.firebase.gradle.plugins
15+
16+
import com.android.build.gradle.LibraryExtension
17+
import java.io.File
18+
import java.io.FileInputStream
19+
import java.io.IOException
20+
import java.util.Properties
21+
import org.gradle.api.GradleException
22+
import org.gradle.api.Project
23+
24+
val Project.sdkDir: File
25+
get() {
26+
val properties = Properties()
27+
val localProperties = rootProject.file("local.properties")
28+
if (localProperties.exists()) {
29+
try {
30+
FileInputStream(localProperties).use { fis -> properties.load(fis) }
31+
} catch (ex: IOException) {
32+
throw GradleException("Could not load local.properties", ex)
33+
}
34+
}
35+
val sdkDir = properties.getProperty("sdk.dir")
36+
if (sdkDir != null) {
37+
return file(sdkDir)
38+
}
39+
val androidHome = System.getenv("ANDROID_HOME")
40+
?: throw GradleException("No sdk.dir or ANDROID_HOME set.")
41+
return file(androidHome)
42+
}
43+
44+
val Project.androidJar: File?
45+
get() {
46+
val android = project.extensions.findByType(LibraryExtension::class.java)
47+
?: return null
48+
return File(
49+
sdkDir, String.format("/platforms/%s/android.jar", android.compileSdkVersion))
50+
}

buildSrc/src/main/java/com/google/firebase/gradle/plugins/apiinfo/ApiInformationTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.google.firebase.gradle.plugins.apiinfo;
1616

1717
import com.android.build.gradle.api.AndroidSourceSet;
18-
import com.google.firebase.gradle.plugins.SdkUtil;
18+
import com.google.firebase.gradle.plugins.SdkUtilKt;
1919
import java.io.File;
2020
import java.io.FileNotFoundException;
2121
import java.io.FileOutputStream;
@@ -107,7 +107,7 @@ void execute() {
107107
.map(File::getAbsolutePath)
108108
.collect(Collectors.joining(":"));
109109

110-
File androidJar = SdkUtil.getAndroidJar(getProject());
110+
File androidJar = SdkUtilKt.getAndroidJar(getProject());
111111
if (androidJar != null) {
112112
classPath += ":" + androidJar.getAbsolutePath();
113113
}

buildSrc/src/main/java/com/google/firebase/gradle/plugins/apiinfo/GenerateApiTxtFileTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.google.firebase.gradle.plugins.apiinfo;
1616

1717
import com.android.build.gradle.api.AndroidSourceSet;
18-
import com.google.firebase.gradle.plugins.SdkUtil;
18+
import com.google.firebase.gradle.plugins.SdkUtilKt;
1919
import java.io.File;
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
@@ -88,7 +88,7 @@ void execute() {
8888
.map(File::getAbsolutePath)
8989
.collect(Collectors.joining(":"));
9090

91-
File androidJar = SdkUtil.getAndroidJar(getProject());
91+
File androidJar = SdkUtilKt.getAndroidJar(getProject());
9292
if (androidJar != null) {
9393
classPath += ":" + androidJar.getAbsolutePath();
9494
}

buildSrc/src/main/java/com/google/firebase/gradle/plugins/apiinfo/GenerateStubsTask.java

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)