Skip to content

Commit

Permalink
Support for BaselineProfile and cleanup TODOs to be TODO:DG
Browse files Browse the repository at this point in the history
- Also add support for vectorDrawables.
  • Loading branch information
tresat committed Jun 10, 2024
1 parent 68c1e93 commit 772b77f
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 26 deletions.
2 changes: 2 additions & 0 deletions unified-prototype/unified-plugin/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ksp = "1.9.23-1.0.20"
# Find latest version at: https://developer.android.com/reference/tools/gradle-api
android = "8.3.0"
androidTools = "31.3.0"
baselineProfile = "1.2.2"
firebaseCrashlytics = "2.9.9"
firebasePerf = "1.4.2"
gms = "4.4.1"
Expand All @@ -33,6 +34,7 @@ kotlin-serialization-plugin = { module = "org.jetbrains.kotlin:kotlin-serializat
android-agp-application = { module = "com.android.application:com.android.application.gradle.plugin", version.ref = "android" }
android-kotlin-android = { module = "org.jetbrains.kotlin.android:org.jetbrains.kotlin.android.gradle.plugin", version.ref = "kotlin" }
android-tools-common = { module = "com.android.tools:common", version.ref = "androidTools" }
baseline-profile-plugin = { module = "androidx.baselineprofile:androidx.baselineprofile.gradle.plugin", version.ref = "baselineProfile"}
firebase-perf-plugin = { module = "com.google.firebase:perf-plugin", version.ref = "firebasePerf" }
firebase-crashlytics-plugin = { module = "com.google.firebase:firebase-crashlytics-gradle", version.ref = "firebaseCrashlytics" }
google-services-plugin = { module = "com.google.gms.google-services:com.google.gms.google-services.gradle.plugin", version.ref = "gms" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
api(libs.android.kotlin.android)

implementation(project(":plugin-jvm"))
implementation(libs.baseline.profile.plugin)
implementation(libs.dependency.guard.plugin)
implementation(libs.ksp.plugin)
implementation(libs.hilt.android.plugin)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gradle.api.experimental.android;

import androidx.baselineprofile.gradle.consumer.BaselineProfileConsumerExtension;
import androidx.room.gradle.RoomExtension;
import com.android.build.api.dsl.BuildType;
import com.android.build.api.dsl.CommonExtension;
Expand All @@ -10,6 +11,7 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.experimental.android.extensions.BaselineProfile;
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;
Expand All @@ -31,10 +33,13 @@ public void apply(Project project) {

// Setup Android software conventions
dslModel.getMinSdk().convention(DEFAULT_MIN_ANDROID_SDK); // https://developer.android.com/build/multidex#mdex-gradle
dslModel.getVectorDrawablesUseSupportLibrary().convention(false);

// Setup minify conventions
dslModel.getBuildTypes().getDebug().getMinify().getEnabled().convention(false);
dslModel.getBuildTypes().getDebug().getBaselineProfile().getEnabled().convention(false);
dslModel.getBuildTypes().getRelease().getMinify().getEnabled().convention(false);
dslModel.getBuildTypes().getRelease().getBaselineProfile().getEnabled().convention(false);

// Setup desugaring conventions and desugar automatically when JDK > 8 is targeted
dslModel.getCoreLibraryDesugaring().getEnabled().convention(project.provider(() -> dslModel.getJdkVersion().get() > 8));
Expand All @@ -52,6 +57,7 @@ public void apply(Project project) {
dslModel.getRoom().getEnabled().convention(false);
dslModel.getRoom().getVersion().convention("2.6.1");
dslModel.getLicenses().getEnabled().convention(false);
dslModel.getBaselineProfile().getEnabled().convention(false);

// Setup Test Options conventions
dslModel.getTesting().getTestOptions().getIncludeAndroidResources().convention(false);
Expand All @@ -76,13 +82,13 @@ protected void linkCommonDependencies(AndroidSoftwareDependencies dependencies,
*/
protected void linkDslModelToPlugin(Project project, AndroidSoftware dslModel, CommonExtension<?, ?, ?, ?, ?, ?> android) {
KotlinAndroidProjectExtension kotlin = project.getExtensions().getByType(KotlinAndroidProjectExtension.class);
ConfigurationContainer configurations = project.getConfigurations();

// Link common properties
ifPresent(dslModel.getNamespace(), android::setNamespace);
ifPresent(dslModel.getCompileSdk(), android::setCompileSdk);
android.defaultConfig(defaultConfig -> {
ifPresent(dslModel.getMinSdk(), defaultConfig::setMinSdk);
ifPresent(dslModel.getVectorDrawablesUseSupportLibrary(), defaultConfig.getVectorDrawables()::setUseSupportLibrary);
return null;
});
android.compileOptions(compileOptions -> {
Expand All @@ -101,8 +107,8 @@ protected void linkDslModelToPlugin(Project project, AndroidSoftware dslModel, C
// Link build types
AndroidSoftwareBuildTypes modelBuildType = dslModel.getBuildTypes();
NamedDomainObjectContainer<? extends BuildType> androidBuildTypes = android.getBuildTypes();
linkBuildType(androidBuildTypes.getByName("debug"), modelBuildType.getDebug(), configurations, android);
linkBuildType(androidBuildTypes.getByName("release"), modelBuildType.getRelease(), configurations, android);
linkBuildType(project, androidBuildTypes.getByName("debug"), modelBuildType.getDebug(), android);
linkBuildType(project, androidBuildTypes.getByName("release"), modelBuildType.getRelease(), android);

configureTesting(project, dslModel, android);

Expand All @@ -111,6 +117,18 @@ protected void linkDslModelToPlugin(Project project, AndroidSoftware dslModel, C
configureHilt(project, dslModel);
configureCompose(project, dslModel, android);
configureRoom(project, dslModel);
configureLicenses(project, dslModel);

if (project.getExtensions().findByName("baselineProfile") != null) {
BaselineProfileConsumerExtension baselineProfileExtension = project.getExtensions().getByType(BaselineProfileConsumerExtension.class);
configureBaselineProfile(project, dslModel.getBaselineProfile(), baselineProfileExtension);
}
}

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

protected void configureHilt(Project project, AndroidSoftware dslModel) {
Expand Down Expand Up @@ -205,12 +223,23 @@ protected void configureKotlinSerialization(Project project, AndroidSoftware dsl
}
}

@SuppressWarnings("UnstableApiUsage")
private static void configureBaselineProfile(Project project, BaselineProfile baselineProfile, BaselineProfileConsumerExtension baselineProfileExtension) {
if (baselineProfile.getEnabled().get()) {
project.getPlugins().apply("androidx.baselineprofile");

baselineProfileExtension.setAutomaticGenerationDuringBuild(baselineProfile.getAutomaticGenerationDuringBuild().get());

project.getConfigurations().getByName("baselineProfile").fromDependencyCollector(baselineProfile.getDependencies().getProfile());
}
}

/**
* Links build types from the model to the android extension.
*/
protected void linkBuildType(BuildType buildType, AndroidSoftwareBuildType model, ConfigurationContainer configurations, CommonExtension<?, ?, ?, ?, ?, ?> android) {
protected void linkBuildType(Project project, BuildType buildType, AndroidSoftwareBuildType model, CommonExtension<?, ?, ?, ?, ?, ?> android) {
buildType.setMinifyEnabled(model.getMinify().getEnabled().get());
linkBuildTypeDependencies(buildType, model.getDependencies(), configurations);
linkBuildTypeDependencies(buildType, model.getDependencies(), project.getConfigurations());

model.getDefaultProguardFiles().get().forEach(proguardFile -> {
File defaultProguardFile = android.getDefaultProguardFile(proguardFile.getName().get());
Expand All @@ -219,6 +248,11 @@ protected void linkBuildType(BuildType buildType, AndroidSoftwareBuildType model
model.getProguardFiles().get().forEach(proguardFile -> {
buildType.proguardFile(proguardFile.getName().get());
});

if (buildType.getExtensions().findByName("baselineProfile") != null) {
BaselineProfileConsumerExtension baselineProfileExtension = buildType.getExtensions().getByType(BaselineProfileConsumerExtension.class);
configureBaselineProfile(project, model.getBaselineProfile(), baselineProfileExtension);
}
}

@SuppressWarnings("UnstableApiUsage")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.android.build.api.dsl.BaseFlavor;
import com.android.build.api.dsl.CommonExtension;
import org.gradle.api.Action;
import org.gradle.api.experimental.android.extensions.BaselineProfile;
import org.gradle.api.experimental.android.extensions.Compose;
import org.gradle.api.experimental.android.extensions.CoreLibraryDesugaring;
import org.gradle.api.experimental.android.extensions.Hilt;
import org.gradle.api.experimental.android.extensions.KotlinSerialization;
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.experimental.android.extensions.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 @@ -41,6 +42,9 @@ public interface AndroidSoftware {
@Restricted
Property<Integer> getJdkVersion();

@Restricted
Property<Boolean> getVectorDrawablesUseSupportLibrary();

AndroidSoftwareBuildTypes getBuildTypes();

AndroidSoftwareDependencies getDependencies();
Expand Down Expand Up @@ -109,7 +113,7 @@ default void testing(Action<? super Testing> action) {

/**
* Support for NiA convention projects defining features.
* TODO: This is a temporary solution until we have a proper feature model.
* TODO:DG This is a temporary solution until we have a proper feature model.
*/
@Nested
Feature getFeature();
Expand All @@ -123,7 +127,7 @@ default void feature(Action<? super Feature> action) {

/**
* 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
* TODO:DG This is a temporary solution until we have a better way of applying plugins
*/
@Nested
Licenses getLicenses();
Expand All @@ -134,4 +138,14 @@ default void licenses(Action<? super Licenses> action) {
licenses.getEnabled().set(true);
action.execute(licenses);
}

@Nested
BaselineProfile getBaselineProfile();

@Configuring
default void baselineProfile(Action<? super BaselineProfile> action) {
BaselineProfile baselineProfile = getBaselineProfile();
baselineProfile.getEnabled().set(true);
action.execute(baselineProfile);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gradle.api.experimental.android;

import org.gradle.api.Action;
import org.gradle.api.experimental.android.extensions.BaselineProfile;
import org.gradle.api.experimental.android.extensions.Minify;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
Expand Down Expand Up @@ -49,4 +50,14 @@ default ProguardFile defaultProguardFile(Action<? super ProguardFile> configure)

@Inject
ObjectFactory getObjectFactory();

@Nested
BaselineProfile getBaselineProfile();

@Configuring
default void baselineProfile(Action<? super BaselineProfile> action) {
BaselineProfile baselineProfile = getBaselineProfile();
baselineProfile.getEnabled().set(true);
action.execute(baselineProfile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private void linkDslModelToPlugin(Project project, AndroidApplication dslModel)
return null;
});

// TODO: All this configuration should be moved to the NiA project
// TODO:DG All this configuration should be moved to the NiA project
if (NiaSupport.isNiaProject(project)) {
NiaSupport.configureNiaApplication(project, dslModel);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.extensions;

import org.gradle.api.Action;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Nested;
import org.gradle.declarative.dsl.model.annotations.Configuring;
import org.gradle.declarative.dsl.model.annotations.Restricted;

public interface BaselineProfile {
/**
* Internal property purposely not exposed to the DSL.
*/
Property<Boolean> getEnabled();

@Restricted
Property<Boolean> getAutomaticGenerationDuringBuild();

@Nested
BaselineProfileDependencies getDependencies();

@Configuring
default void dependencies(Action<? super BaselineProfileDependencies> action) {
action.execute(getDependencies());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.extensions;

import org.gradle.api.artifacts.dsl.Dependencies;
import org.gradle.api.artifacts.dsl.DependencyCollector;

@SuppressWarnings("UnstableApiUsage")
public interface BaselineProfileDependencies extends Dependencies {
DependencyCollector getProfile();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface Compose {
@Restricted
Property<Boolean> getEnabled();

// TODO: This should be a file property, and not assume it's a path from the root project
// TODO:DG This should be a file property, and not assume it's a path from the root project
@Restricted
Property<String> getStabilityConfigurationFilePath();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.gradle.api.experimental.android.nia;
package org.gradle.api.experimental.android.extensions;

import org.gradle.api.provider.Property;
import org.gradle.declarative.dsl.model.annotations.Restricted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ default void dependencies(Action<? super ProtobufDependencies> action) {
action.execute(getDependencies());
}

// TODO: This is modeled in a very limited manner for now
// TODO:DG This is modeled in a very limited manner for now
@Restricted
Property<String> getOption();

Expand All @@ -45,7 +45,7 @@ default void dependencies(Action<? super ProtobufDependencies> action) {
@Restricted
Property<String> getVersion();

// TODO: Should be a File based property, when we support these
// TODO:DG Should be a File based property, when we support these
@Restricted
Property<String> getGeneratedRootDir();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ default void buildTypes(Action<? super AndroidLibraryBuildTypes> action) {
action.execute(getBuildTypes());
}

// TODO: We really want to model a list of consumer proguard files here, but can't yet
// TODO:DG We really want to model a list of consumer proguard files here, but can't yet
@Restricted
Property<String> getConsumerProguardFile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void linkDslModelToPlugin(Project project, AndroidLibrary dslModel) {

configureProtobuf(project, dslModel, android);

// TODO: All this configuration should be moved to the NiA project
// TODO:DG All this configuration should be moved to the NiA project
if (NiaSupport.isNiaProject(project)) {
NiaSupport.configureNiaLibrary(project, dslModel);
}
Expand Down Expand Up @@ -92,7 +92,7 @@ protected void configureProtobuf(Project project, AndroidLibrary dslModel, Commo
});

/*
* TODO: We don't want to rely on beforeVariants here, but how to do without hardcoding:
* TODO:DG We don't want to rely on beforeVariants here, but how to do without hardcoding:
* the NiA variants: "demoDebug, demoRelease, prodDebug, prodRelease"?
* This would seem to require some sort of ProductFlavor support (and maybe enumerated buildTypes?)
* which we don't want to add just yet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

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

// TODO: This class should be moved to the NiA project
// TODO:DG This class should be moved to the NiA project
/**
* This is a utility class that configures an Android project with conventions
* for the Now in Android project.
Expand Down Expand Up @@ -112,19 +112,12 @@ private static void configureNia(Project project, AndroidSoftware dslModel, Comm
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
Loading

0 comments on commit 772b77f

Please sign in to comment.