Skip to content

Commit

Permalink
Fix configureNiA to not assume android library plugin
Browse files Browse the repository at this point in the history
Library-specific configuration applied by android library plugin on top of general NiA Android support now.
  • Loading branch information
tresat committed May 21, 2024
1 parent 43aede6 commit c8ac46a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ protected void linkDslModelToPlugin(Project project, AndroidSoftware dslModel, C
configureDesugaring(project, dslModel, android);
configureHilt(project, dslModel, android);
configureCompose(project, dslModel, android);

// TODO: All this configuration should be moved to the NiA project
if (Objects.equals(project.getRootProject().getName(), "nowinandroid")) {
NiaSupport.configureNia(project, dslModel);
}
}

protected void configureHilt(Project project, AndroidSoftware dslModel, CommonExtension<?, ?, ?, ?, ?, ?> android) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.experimental.android.AbstractAndroidSoftwarePlugin;
import org.gradle.api.experimental.android.AndroidSoftware;
import org.gradle.api.experimental.android.nia.NiaSupport;
import org.gradle.api.internal.plugins.software.SoftwareType;

import java.util.Objects;
Expand Down Expand Up @@ -41,6 +42,11 @@ public void apply(Project project) {

// After AGP creates configurations, link deps to the collectors
linkCommonDependencies(dslModel.getDependencies(), project.getConfigurations());

// TODO: All this configuration should be moved to the NiA project
if (Objects.equals(project.getRootProject().getName(), "nowinandroid")) {
NiaSupport.configureNiaApplication(project, dslModel);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.experimental.android.AbstractAndroidSoftwarePlugin;
import org.gradle.api.experimental.android.AndroidSoftware;
import org.gradle.api.experimental.android.nia.NiaSupport;
import org.gradle.api.internal.plugins.software.SoftwareType;

import java.util.Objects;

/**
* Creates a declarative {@link AndroidLibrary} DSL model, applies the official Android plugin,
* and links the declarative model to the official plugin.
Expand Down Expand Up @@ -38,6 +41,11 @@ public void apply(Project project) {

// After AGP creates configurations, link deps to the collectors
linkCommonDependencies(dslModel.getDependencies(), project.getConfigurations());

// TODO: All this configuration should be moved to the NiA project
if (Objects.equals(project.getRootProject().getName(), "nowinandroid")) {
NiaSupport.configureNiaLibrary(project, dslModel);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.android.build.api.artifact.SingleArtifact;
import com.android.build.api.dsl.*;
import com.android.build.api.variant.AndroidComponentsExtension;
import com.android.build.api.variant.BuiltArtifactsLoader;
import com.android.build.api.variant.HasAndroidTest;
import com.android.build.api.variant.LibraryAndroidComponentsExtension;
import com.android.build.api.variant.*;
import org.apache.commons.lang3.StringUtils;
import org.gradle.api.*;
import org.gradle.api.experimental.android.AndroidSoftware;
Expand Down Expand Up @@ -36,37 +33,55 @@
public final class NiaSupport {
private NiaSupport() { /* Not instantiable */ }

@SuppressWarnings("UnstableApiUsage")
public static void configureNia(Project project, AndroidSoftware dslModel) {
public static void configureNiaLibrary(Project project, AndroidSoftware dslModel) {
LibraryExtension androidLib = project.getExtensions().getByType(LibraryExtension.class);
LibraryAndroidComponentsExtension androidLibComponents = project.getExtensions().getByType(LibraryAndroidComponentsExtension.class);

configureNia(project, dslModel, androidLib, androidLibComponents);
disableUnnecessaryAndroidTests(project, androidLibComponents);
}

public static void configureNiaApplication(Project project, AndroidSoftware dslModel) {
ApplicationExtension androidApp = project.getExtensions().getByType(ApplicationExtension.class);
ApplicationAndroidComponentsExtension androidAppComponents = project.getExtensions().getByType(ApplicationAndroidComponentsExtension.class);

configureNia(project, dslModel, androidApp, androidAppComponents);
}

@SuppressWarnings("UnstableApiUsage")
private static void configureNia(Project project, AndroidSoftware dslModel, CommonExtension<?, ?, ?, ?, ?, ?> android, AndroidComponentsExtension<?, ?, ?> androidComponents) {
dslModel.getDependencies().getImplementation().add("androidx.tracing:tracing-ktx:1.3.0-alpha02");
dslModel.getTesting().getDependencies().getImplementation().add("org.jetbrains.kotlin:kotlin-test");

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

disableUnnecessaryAndroidTests(project, androidLibComponents);

configureGradleManagedDevices(androidLib);
configureLint(androidLib);
configurePrintApksTask(project, androidLibComponents);
configureGradleManagedDevices(android);
configureLint(android);
configurePrintApksTask(project, androidComponents);

if (dslModel.getTesting().getJacoco().getEnabled().get()) {
configureJacoco(dslModel.getTesting().getJacoco(), project, androidLib);
configureJacoco(dslModel.getTesting().getJacoco(), project, android);
}

if (dslModel.getFeature().getEnabled().get()) {
configureFeature(project, androidLib);
configureFeature(project, android);
}
}


@SuppressWarnings("deprecation")
private static void disableUnnecessaryAndroidTests(Project project, LibraryAndroidComponentsExtension androidLibComponents) {
androidLibComponents.beforeVariants(androidLibComponents.selector().all(), it -> {
it.setEnableAndroidTest(it.getEnableAndroidTest() && project.getLayout().getProjectDirectory().file("src/androidTest").getAsFile().exists());
});
}

@SuppressWarnings("UnstableApiUsage")
private static void configureFeature(Project project, LibraryExtension androidLib) {
androidLib.getDefaultConfig().setTestInstrumentationRunner("com.google.samples.apps.nowinandroid.core.testing.NiaTestRunner");
androidLib.getTestOptions().setAnimationsDisabled(true);
private static void configureFeature(Project project, CommonExtension<?, ?, ?, ?, ?, ?> android) {
android.getDefaultConfig().setTestInstrumentationRunner("com.google.samples.apps.nowinandroid.core.testing.NiaTestRunner");
android.getTestOptions().setAnimationsDisabled(true);

project.getDependencies().add("implementation", project.project(":core:ui"));
project.getDependencies().add("implementation", project.project(":core:designsystem"));
Expand All @@ -79,21 +94,21 @@ private static void configureFeature(Project project, LibraryExtension androidLi
project.getDependencies().add("androidTestImplementation", "androidx.lifecycle:lifecycle-runtime-testing:2.7.0");
}

private static void configureLint(LibraryExtension androidLib) {
androidLib.getLint().setXmlReport(true);
androidLib.getLint().setCheckDependencies(true);
private static void configureLint(CommonExtension<?, ?, ?, ?, ?, ?> android) {
android.getLint().setXmlReport(true);
android.getLint().setCheckDependencies(true);
}

private static void configureFlavors(
CommonExtension<?, ?, ?, ?, ?, ?> commonExtension,
CommonExtension<?, ?, ?, ?, ?, ?> android,
BiConsumer<ProductFlavor, NiaFlavor> flavorConfigurationBlock) {
commonExtension.getFlavorDimensions().add(FlavorDimension.contentType.name());
android.getFlavorDimensions().add(FlavorDimension.contentType.name());

Arrays.stream(NiaFlavor.values()).forEach(it -> commonExtension.getProductFlavors().create(it.name(), flavor -> {
Arrays.stream(NiaFlavor.values()).forEach(it -> android.getProductFlavors().create(it.name(), flavor -> {
setDimensionReflectively(flavor, it.dimension.name());
flavorConfigurationBlock.accept(flavor, it);

if (commonExtension instanceof ApplicationExtension && flavor instanceof ApplicationProductFlavor) {
if (android instanceof ApplicationExtension && flavor instanceof ApplicationProductFlavor) {
if (it.applicationIdSuffix != null) {
((ApplicationProductFlavor) flavor).setApplicationIdSuffix(it.applicationIdSuffix);
}
Expand Down Expand Up @@ -168,26 +183,19 @@ private static void configureKotlin(Project project) {
});
}

@SuppressWarnings("deprecation")
private static void disableUnnecessaryAndroidTests(Project project, LibraryAndroidComponentsExtension androidComponents) {
androidComponents.beforeVariants(androidComponents.selector().all(), it -> {
it.setEnableAndroidTest(it.getEnableAndroidTest() && project.getLayout().getProjectDirectory().file("src/androidTest").getAsFile().exists());
});
}

/**
* Configure project for Gradle managed devices
*/
@SuppressWarnings("UnstableApiUsage")
private static void configureGradleManagedDevices(CommonExtension<?, ?, ?, ?, ?, ?> commonExtension) {
private static void configureGradleManagedDevices(CommonExtension<?, ?, ?, ?, ?, ?> android) {
DeviceConfig pixel4 = new DeviceConfig("Pixel 4", 30, "aosp-atd");
DeviceConfig pixel6 = new DeviceConfig("Pixel 6", 31, "aosp");
DeviceConfig pixelC = new DeviceConfig("Pixel C", 30, "aosp-atd");

List<DeviceConfig> allDevices = Arrays.asList(pixel4, pixel6, pixelC);
List<DeviceConfig> ciDevices = Arrays.asList(pixel4, pixelC);

TestOptions testOptions = commonExtension.getTestOptions();
TestOptions testOptions = android.getTestOptions();

ExtensiblePolymorphicDomainObjectContainer<Device> devices = testOptions.getManagedDevices().getDevices();
allDevices.forEach(deviceConfig -> {
Expand All @@ -203,8 +211,8 @@ private static void configureGradleManagedDevices(CommonExtension<?, ?, ?, ?, ?,
}

@SuppressWarnings("UnstableApiUsage")
private static void configurePrintApksTask(Project project, AndroidComponentsExtension<?, ?, ?> extension) {
extension.onVariants(extension.selector().all(), variant -> {
private static void configurePrintApksTask(Project project, AndroidComponentsExtension<?, ?, ?> androidComponents) {
androidComponents.onVariants(androidComponents.selector().all(), variant -> {
if (variant instanceof HasAndroidTest hasAndroidTestVariant) {
BuiltArtifactsLoader loader = variant.getArtifacts().getBuiltArtifactsLoader();
Provider<Directory> artifact = hasAndroidTestVariant.getAndroidTest() != null ? hasAndroidTestVariant.getAndroidTest().getArtifacts().get(SingleArtifact.APK.INSTANCE) : null;
Expand Down Expand Up @@ -251,10 +259,10 @@ private static List<String> coverageExclusions() {
}

@SuppressWarnings("deprecation")
private static void configureJacoco(Jacoco jacocoExtension, Project project, LibraryExtension androidLib) {
private static void configureJacoco(Jacoco jacocoExtension, Project project, CommonExtension<?, ?, ?, ?, ?, ?> android) {
project.getPlugins().apply("jacoco");

androidLib.getBuildTypes().configureEach(buildType -> {
android.getBuildTypes().configureEach(buildType -> {
buildType.setEnableAndroidTestCoverage(true);
buildType.setEnableUnitTestCoverage(true);
});
Expand Down

0 comments on commit c8ac46a

Please sign in to comment.