Skip to content

Migrate metalava download to gradle's resolution process. #1788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ configure(subprojects) {
google()
jcenter()
mavenLocal()
maven {
url 'https://storage.googleapis.com/android-ci/mvn/'
}
}

apply plugin: 'com.github.sherter.google-java-format'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.firebase.gradle.plugins.apiinfo.ApiInformationTask;
import com.google.firebase.gradle.plugins.apiinfo.GenerateApiTxtFileTask;
import com.google.firebase.gradle.plugins.apiinfo.GenerateStubsTask;
import com.google.firebase.gradle.plugins.apiinfo.GetMetalavaJarTask;
import com.google.firebase.gradle.plugins.ci.Coverage;
import java.io.File;
Expand Down Expand Up @@ -161,8 +160,6 @@ private static void setupApiInformationAnalysis(Project project) {
"docStubs",
GenerateStubsTask.class,
task -> {
task.setMetalavaJarPath(metalavaOutputJarFile.getAbsolutePath());
task.setOutputDir(new File(project.getBuildDir(), "doc-stubs"));
task.dependsOn("getMetalavaJar");

task.setSourceSet(mainSourceSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.firebase.gradle.plugins.apiinfo.ApiInformationTask;
import com.google.firebase.gradle.plugins.apiinfo.GenerateApiTxtFileTask;
import com.google.firebase.gradle.plugins.apiinfo.GenerateStubsTask;
import com.google.firebase.gradle.plugins.apiinfo.GetMetalavaJarTask;
import com.google.firebase.gradle.plugins.ci.Coverage;
import com.google.firebase.gradle.plugins.ci.device.FirebaseTestServer;
Expand Down Expand Up @@ -102,7 +101,6 @@ public void apply(Project project) {
.setFreeCompilerArgs(
ImmutableList.of("-module-name", kotlinModuleName(project))));


Dokka.configure(project, android, firebaseLibrary);
}

Expand Down Expand Up @@ -174,10 +172,6 @@ private static void setupApiInformationAnalysis(Project project, LibraryExtensio
"docStubs",
GenerateStubsTask.class,
task -> {
task.setMetalavaJarPath(metalavaOutputJarFile.getAbsolutePath());
task.setOutputDir(new File(project.getBuildDir(), "doc-stubs"));
task.dependsOn("getMetalavaJar");

task.setSourceSet(mainSourceSet);
});
project.getTasks().getByName("check").dependsOn(docStubs);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2020 Google LLC
//
// 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 com.google.firebase.gradle.plugins

import com.android.build.gradle.api.AndroidSourceSet
import java.io.File
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.TaskAction

val Project.metalavaConfig: Configuration
get() =
configurations.findByName("metalavaArtifacts")
?: configurations.create("metalavaArtifacts") {
this.dependencies.add([email protected]("com.android:metalava:1.3.0"))
}

fun Project.runMetalavaWithArgs(
arguments: List<String>
) {
val allArgs = listOf(
"--no-banner",
"--hide",
"HiddenSuperclass" // We allow having a hidden parent class
) + arguments

project.javaexec {
main = "com.android.tools.metalava.Driver"
classpath = project.metalavaConfig
args = allArgs
}
}

abstract class GenerateStubsTask : DefaultTask() {
/** Source files against which API signatures will be validated. */
lateinit var sourceSet: Object

@get:InputFiles
lateinit var classPath: FileCollection

@get:OutputDirectory
val outputDir: File = File(project.buildDir, "doc-stubs")

private val sourceDirs: Set<File>
get() = with(sourceSet) {
when (this) {
is SourceSet -> java.srcDirs
is AndroidSourceSet -> java.srcDirs
else -> throw IllegalStateException("Unsupported sourceSet provided: $javaClass")
}
}

@TaskAction
fun run() {
val sourcePath = sourceDirs.asSequence()
.filter { it.exists() }
.map { it.absolutePath }
.joinToString(":")

val classPath = classPath.files.asSequence()
.map { it.absolutePath }.toMutableList()
project.androidJar?.let {
classPath += listOf(it.absolutePath)
}

project.runMetalavaWithArgs(
listOf(
"--source-path",
sourcePath,
"--classpath",
classPath.joinToString(":"),
"--include-annotations",
"--doc-stubs",
outputDir.absolutePath
))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2019 Google LLC
//
// 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 com.google.firebase.gradle.plugins

import com.android.build.gradle.LibraryExtension
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.util.Properties
import org.gradle.api.GradleException
import org.gradle.api.Project

val Project.sdkDir: File
get() {
val properties = Properties()
val localProperties = rootProject.file("local.properties")
if (localProperties.exists()) {
try {
FileInputStream(localProperties).use { fis -> properties.load(fis) }
} catch (ex: IOException) {
throw GradleException("Could not load local.properties", ex)
}
}
val sdkDir = properties.getProperty("sdk.dir")
if (sdkDir != null) {
return file(sdkDir)
}
val androidHome = System.getenv("ANDROID_HOME")
?: throw GradleException("No sdk.dir or ANDROID_HOME set.")
return file(androidHome)
}

val Project.androidJar: File?
get() {
val android = project.extensions.findByType(LibraryExtension::class.java)
?: return null
return File(
sdkDir, String.format("/platforms/%s/android.jar", android.compileSdkVersion))
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package com.google.firebase.gradle.plugins.apiinfo;

import com.android.build.gradle.api.AndroidSourceSet;
import com.google.firebase.gradle.plugins.SdkUtil;
import com.google.firebase.gradle.plugins.SdkUtilKt;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -107,7 +107,7 @@ void execute() {
.map(File::getAbsolutePath)
.collect(Collectors.joining(":"));

File androidJar = SdkUtil.getAndroidJar(getProject());
File androidJar = SdkUtilKt.getAndroidJar(getProject());
if (androidJar != null) {
classPath += ":" + androidJar.getAbsolutePath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package com.google.firebase.gradle.plugins.apiinfo;

import com.android.build.gradle.api.AndroidSourceSet;
import com.google.firebase.gradle.plugins.SdkUtil;
import com.google.firebase.gradle.plugins.SdkUtilKt;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -88,7 +88,7 @@ void execute() {
.map(File::getAbsolutePath)
.collect(Collectors.joining(":"));

File androidJar = SdkUtil.getAndroidJar(getProject());
File androidJar = SdkUtilKt.getAndroidJar(getProject());
if (androidJar != null) {
classPath += ":" + androidJar.getAbsolutePath();
}
Expand Down

This file was deleted.

Loading