Skip to content
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

Add support for NiA :app project to use declarative syntax #91

Merged
merged 6 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add licenses feature, some minor adjustments and cleanup
  • Loading branch information
tresat committed Jun 10, 2024
commit eeea165eca79341b84bbb8a3fbb4421e4d220843
6 changes: 4 additions & 2 deletions unified-prototype/unified-plugin/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ firebasePerf = "1.4.2"
gms = "4.4.1"
hilt = "2.50"
room = "2.6.1"
roborazzi = "1.7.0"

#Other Libs
apache-commons = "3.3.1"
dependency-guard = "0.4.3"
truth = "1.4.2"
protobuf = "0.9.4"
oss-licenses = "0.10.6"
roborazzi = "1.7.0"

[libraries]
# Kotlin Libs
Expand All @@ -43,6 +44,7 @@ room-plugin = { module = "androidx.room:room-gradle-plugin", version.ref = "room
#Other Libs
apache-commons-lang = { module = "org.apache.commons:commons-lang3", version.ref = "apache-commons" }
dependency-guard-plugin = { module = "com.dropbox.dependency-guard:com.dropbox.dependency-guard.gradle.plugin", version.ref = "dependency-guard"}
truth = { module = "com.google.truth:truth", version.ref = "truth" }
oss-licenses-plugin = { group = "com.google.android.gms", name = "oss-licenses-plugin", version.ref = "oss-licenses" }
protobuf-plugin = { module = "com.google.protobuf:protobuf-gradle-plugin", version.ref = "protobuf" }
roborazzi-plugin = { module = "io.github.takahirom.roborazzi:roborazzi-gradle-plugin", version.ref = "roborazzi" }
truth = { module = "com.google.truth:truth", version.ref = "truth" }
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
implementation(libs.google.services.plugin)
implementation(libs.firebase.perf.plugin)
implementation(libs.firebase.crashlytics.plugin)
implementation(libs.oss.licenses.plugin)

implementation(libs.apache.commons.lang)
implementation(libs.android.tools.common)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.android.build.api.dsl.CommonExtension;
import com.android.build.api.dsl.UnitTestOptions;
import com.google.devtools.ksp.gradle.KspExtension;
import org.gradle.api.Action;
import org.gradle.api.JavaVersion;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Plugin;
Expand All @@ -14,11 +13,11 @@
import org.gradle.api.experimental.android.extensions.testing.AndroidTestDependencies;
import org.gradle.api.experimental.android.extensions.testing.TestOptions;
import org.gradle.api.experimental.android.extensions.testing.Testing;
import org.gradle.api.provider.Property;
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension;

import java.io.File;

import static org.gradle.api.experimental.android.AndroidSupport.ifPresent;
import static org.gradle.api.experimental.android.extensions.ComposeSupport.configureCompose;

public abstract class AbstractAndroidSoftwarePlugin implements Plugin<Project> {
Expand Down Expand Up @@ -52,6 +51,7 @@ public void apply(Project project) {
dslModel.getHilt().getEnabled().convention(false);
dslModel.getRoom().getEnabled().convention(false);
dslModel.getRoom().getVersion().convention("2.6.1");
dslModel.getLicenses().getEnabled().convention(false);

// Setup Test Options conventions
dslModel.getTesting().getTestOptions().getIncludeAndroidResources().convention(false);
Expand Down Expand Up @@ -108,12 +108,12 @@ protected void linkDslModelToPlugin(Project project, AndroidSoftware dslModel, C

configureKotlinSerialization(project, dslModel);
configureDesugaring(project, dslModel, android);
configureHilt(project, dslModel, android);
configureHilt(project, dslModel);
configureCompose(project, dslModel, android);
configureRoom(project, dslModel, android);
configureRoom(project, dslModel);
}

protected void configureHilt(Project project, AndroidSoftware dslModel, CommonExtension<?, ?, ?, ?, ?, ?> android) {
protected void configureHilt(Project project, AndroidSoftware dslModel) {
if (dslModel.getHilt().getEnabled().get()) {
project.getLogger().info("Hilt is enabled in: " + project.getPath());

Expand All @@ -130,7 +130,7 @@ protected void configureHilt(Project project, AndroidSoftware dslModel, CommonEx
}

@SuppressWarnings("UnstableApiUsage")
protected void configureRoom(Project project, AndroidSoftware dslModel, CommonExtension<?, ?, ?, ?, ?, ?> android) {
protected void configureRoom(Project project, AndroidSoftware dslModel) {
if (dslModel.getRoom().getEnabled().get()) {
project.getLogger().info("Room is enabled in: " + project.getPath());

Expand Down Expand Up @@ -228,10 +228,4 @@ protected void linkBuildTypeDependencies(BuildType buildType, AndroidSoftwareDep
configurations.getByName(name + "CompileOnly").fromDependencyCollector(dependencies.getCompileOnly());
configurations.getByName(name + "RuntimeOnly").fromDependencyCollector(dependencies.getRuntimeOnly());
}

protected <T> void ifPresent(Property<T> property, Action<T> action) {
if (property.isPresent()) {
action.execute(property.get());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.gradle.api.experimental.android.extensions.Room;
import org.gradle.api.experimental.android.extensions.testing.Testing;
import org.gradle.api.experimental.android.nia.Feature;
import org.gradle.api.experimental.android.nia.Licenses;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Nested;
import org.gradle.declarative.dsl.model.annotations.Configuring;
Expand Down Expand Up @@ -119,4 +120,18 @@ default void feature(Action<? super Feature> action) {
feature.getEnabled().set(true);
action.execute(feature);
}

/**
* Support for NiA projects using the com.google.android.gms.oss-licenses-plugin
* TODO: This is a temporary solution until we have a better way of applying plugins
*/
@Nested
Licenses getLicenses();

@Configuring
default void licenses(Action<? super Licenses> action) {
Licenses licenses = getLicenses();
licenses.getEnabled().set(true);
action.execute(licenses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 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
*
* https://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 org.gradle.api.experimental.android;

import org.gradle.api.Action;
import org.gradle.api.provider.Property;

/**
* Static util class containing common methods.
*/
public class AndroidSupport {
private AndroidSupport() { /* not instantiable */ }

public static <T> void ifPresent(Property<T> property, Action<T> action) {
if (property.isPresent()) {
action.execute(property.get());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.gradle.api.experimental.android.nia.NiaSupport;
import org.gradle.api.internal.plugins.software.SoftwareType;

import static org.gradle.api.experimental.android.AndroidSupport.ifPresent;

/**
* Creates a declarative {@link AndroidApplication} DSL model, applies the official Android plugin,
* and links the declarative model to the official plugin.
Expand All @@ -36,6 +38,9 @@ public void apply(Project project) {
dslModel.getFirebase().getEnabled().convention(false);
dslModel.getFirebase().getVersion().convention("32.4.0");

dslModel.getBuildTypes().getDebug().getApplicationIdSuffix().convention((String) null);
dslModel.getBuildTypes().getRelease().getApplicationIdSuffix().convention((String) null);

// Register an afterEvaluate listener before we apply the Android plugin to ensure we can
// run actions before Android does.
project.afterEvaluate(p -> linkDslModelToPlugin(p, dslModel));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Objects;
import java.util.Set;

import static org.gradle.api.experimental.android.AndroidSupport.ifPresent;

/**
* Creates a declarative {@link AndroidLibrary} DSL model, applies the official Android plugin,
* and links the declarative model to the official plugin.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 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
*
* https://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 org.gradle.api.experimental.android.nia;

import org.gradle.api.provider.Property;
import org.gradle.declarative.dsl.model.annotations.Restricted;

@Restricted
public interface Licenses {
/**
* Internal property purposely not exposed to the DSL.
*/
Property<Boolean> getEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import static org.gradle.api.experimental.android.AndroidSupport.ifPresent;

// TODO: This class should be moved to the NiA project
/**
* This is a utility class that configures an Android project with conventions
Expand All @@ -67,6 +69,8 @@ public static void configureNiaLibrary(Project project, AndroidLibrary dslModel)
LibraryExtension androidLib = project.getExtensions().getByType(LibraryExtension.class);
LibraryAndroidComponentsExtension androidLibComponents = project.getExtensions().getByType(LibraryAndroidComponentsExtension.class);

androidLib.setResourcePrefix(buildResourcePrefix(project));

configureNia(project, dslModel, androidLib, androidLibComponents);
disableUnnecessaryAndroidTests(project, androidLibComponents);
}
Expand All @@ -81,7 +85,11 @@ public static void configureNiaApplication(Project project, AndroidApplication d
TestOptions testOptions = androidApp.getTestOptions();
testOptions.setAnimationsDisabled(true);

ifPresent(dslModel.getBuildTypes().getDebug().getApplicationIdSuffix(), androidApp.getBuildTypes().getByName("debug")::setApplicationIdSuffix);
ifPresent(dslModel.getBuildTypes().getRelease().getApplicationIdSuffix(), androidApp.getBuildTypes().getByName("release")::setApplicationIdSuffix);

configureBadgingTasks(project, androidAppComponents);

configureDependencyGuard(project, dslModel);
configureFirebase(project, dslModel, androidApp);
}
Expand All @@ -91,19 +99,25 @@ private static void configureNia(Project project, AndroidSoftware dslModel, Comm
dslModel.getDependencies().getImplementation().add("androidx.tracing:tracing-ktx:1.3.0-alpha02");
dslModel.getTesting().getDependencies().getImplementation().add("org.jetbrains.kotlin:kotlin-test");

android.setResourcePrefix(buildResourcePrefix(project));
configureFlavors(android, (flavor, niaFlavor) -> {});
configureKotlin(project);

configureGradleManagedDevices(android);
configureLint(android);
configurePrintApksTask(project, androidComponents);
configureLicenses(project, dslModel);

configureJacoco(project, dslModel, android);

configureFeature(project, dslModel, android);
}

private static void configureLicenses(Project project, AndroidSoftware dslModel) {
if (dslModel.getLicenses().getEnabled().get()) {
project.getPlugins().apply("com.google.android.gms.oss-licenses-plugin");
}
}

@SuppressWarnings("UnstableApiUsage")
private static void configureFirebase(Project project, AndroidApplication dslModel, ApplicationExtension androidApp) {
if (dslModel.getFirebase().getEnabled().get()) {
Expand Down