diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 47b8af943..b394a1544 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -20,11 +20,12 @@ plugins { alias(libs.plugins.nowinandroid.android.application.compose) alias(libs.plugins.nowinandroid.android.application.flavors) alias(libs.plugins.nowinandroid.android.application.jacoco) - alias(libs.plugins.nowinandroid.android.hilt) alias(libs.plugins.nowinandroid.android.application.firebase) - id("com.google.android.gms.oss-licenses-plugin") + alias(libs.plugins.nowinandroid.android.application.testoptions) + alias(libs.plugins.nowinandroid.android.hilt) alias(libs.plugins.baselineprofile) alias(libs.plugins.roborazzi) + id("com.google.android.gms.oss-licenses-plugin") } android { @@ -63,11 +64,6 @@ android { excludes.add("/META-INF/{AL2.0,LGPL2.1}") } } - testOptions { - unitTests { - isIncludeAndroidResources = true - } - } namespace = "com.google.samples.apps.nowinandroid" } diff --git a/build-logic/convention/build.gradle.kts b/build-logic/convention/build.gradle.kts index dc478a829..0c004a90f 100644 --- a/build-logic/convention/build.gradle.kts +++ b/build-logic/convention/build.gradle.kts @@ -57,45 +57,43 @@ tasks { gradlePlugin { plugins { - register("androidApplicationCompose") { - id = "nowinandroid.android.application.compose" - implementationClass = "AndroidApplicationComposeConventionPlugin" - } register("androidApplication") { id = "nowinandroid.android.application" implementationClass = "AndroidApplicationConventionPlugin" } + register("androidApplicationCompose") { + id = "nowinandroid.android.application.compose" + implementationClass = "AndroidApplicationComposeConventionPlugin" + } register("androidApplicationJacoco") { id = "nowinandroid.android.application.jacoco" implementationClass = "AndroidApplicationJacocoConventionPlugin" } - register("androidLibraryCompose") { - id = "nowinandroid.android.library.compose" - implementationClass = "AndroidLibraryComposeConventionPlugin" + register("androidApplicationTestOptionsUnitTests") { + id = "nowinandroid.android.application.testoptions" + implementationClass = "AndroidApplicationTestOptionsUnitTestsConventionPlugin" } + register("androidLibrary") { id = "nowinandroid.android.library" implementationClass = "AndroidLibraryConventionPlugin" } - register("androidFeature") { - id = "nowinandroid.android.feature" - implementationClass = "AndroidFeatureConventionPlugin" + register("androidLibraryCompose") { + id = "nowinandroid.android.library.compose" + implementationClass = "AndroidLibraryComposeConventionPlugin" } register("androidLibraryJacoco") { id = "nowinandroid.android.library.jacoco" implementationClass = "AndroidLibraryJacocoConventionPlugin" } - register("androidTest") { - id = "nowinandroid.android.test" - implementationClass = "AndroidTestConventionPlugin" - } - register("androidHilt") { - id = "nowinandroid.android.hilt" - implementationClass = "AndroidHiltConventionPlugin" + register("androidLibraryTestOptionsUnitTests") { + id = "nowinandroid.android.library.testoptions" + implementationClass = "AndroidLibraryTestOptionsUnitTestsConventionPlugin" } - register("androidRoom") { - id = "nowinandroid.android.room" - implementationClass = "AndroidRoomConventionPlugin" + + register("androidFeature") { + id = "nowinandroid.android.feature" + implementationClass = "AndroidFeatureConventionPlugin" } register("androidFirebase") { id = "nowinandroid.android.application.firebase" @@ -109,6 +107,18 @@ gradlePlugin { id = "nowinandroid.android.lint" implementationClass = "AndroidLintConventionPlugin" } + register("androidHilt") { + id = "nowinandroid.android.hilt" + implementationClass = "AndroidHiltConventionPlugin" + } + register("androidRoom") { + id = "nowinandroid.android.room" + implementationClass = "AndroidRoomConventionPlugin" + } + register("androidTest") { + id = "nowinandroid.android.test" + implementationClass = "AndroidTestConventionPlugin" + } register("jvmLibrary") { id = "nowinandroid.jvm.library" implementationClass = "JvmLibraryConventionPlugin" diff --git a/build-logic/convention/src/main/kotlin/AndroidApplicationsTestOptionsUnitTestsConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidApplicationsTestOptionsUnitTestsConventionPlugin.kt new file mode 100644 index 000000000..c38e09fb4 --- /dev/null +++ b/build-logic/convention/src/main/kotlin/AndroidApplicationsTestOptionsUnitTestsConventionPlugin.kt @@ -0,0 +1,31 @@ +/* + * 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. + */ + +import com.android.build.api.dsl.ApplicationExtension +import com.google.samples.apps.nowinandroid.configureTestOptionsUnitTests +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.configure + +class AndroidApplicationTestOptionsUnitTestsConventionPlugin : Plugin { + override fun apply(target: Project) { + with(target) { + extensions.configure { + configureTestOptionsUnitTests(this) + } + } + } +} diff --git a/build-logic/convention/src/main/kotlin/AndroidLibraryTestOptionsUnitTestsConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidLibraryTestOptionsUnitTestsConventionPlugin.kt new file mode 100644 index 000000000..c8d769e91 --- /dev/null +++ b/build-logic/convention/src/main/kotlin/AndroidLibraryTestOptionsUnitTestsConventionPlugin.kt @@ -0,0 +1,31 @@ +/* + * 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. + */ + +import com.android.build.api.dsl.LibraryExtension +import com.google.samples.apps.nowinandroid.configureTestOptionsUnitTests +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.configure + +class AndroidLibraryTestOptionsUnitTestsConventionPlugin : Plugin { + override fun apply(target: Project) { + with(target) { + extensions.configure { + configureTestOptionsUnitTests(this) + } + } + } +} diff --git a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/AndroidCompose.kt b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/AndroidCompose.kt index f16a8051a..bbae6d801 100644 --- a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/AndroidCompose.kt +++ b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/AndroidCompose.kt @@ -42,13 +42,6 @@ internal fun Project.configureAndroidCompose( add("implementation", libs.findLibrary("androidx-compose-ui-tooling-preview").get()) add("debugImplementation", libs.findLibrary("androidx-compose-ui-tooling").get()) } - - testOptions { - unitTests { - // For Robolectric - isIncludeAndroidResources = true - } - } } extensions.configure { diff --git a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/TestOptions.kt b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/TestOptions.kt new file mode 100644 index 000000000..f66a934e2 --- /dev/null +++ b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/TestOptions.kt @@ -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 com.google.samples.apps.nowinandroid + +import com.android.build.api.dsl.CommonExtension +import org.gradle.api.Project + +internal fun Project.configureTestOptionsUnitTests( + commonExtension: CommonExtension<*, *, *, *, *, *>, +) { + commonExtension.apply { + testOptions { + // For Robolectric + unitTests { + isIncludeAndroidResources = true + } + } + } +} diff --git a/core/data/build.gradle.kts b/core/data/build.gradle.kts index 142637ff9..d405ca5f1 100644 --- a/core/data/build.gradle.kts +++ b/core/data/build.gradle.kts @@ -16,18 +16,13 @@ plugins { alias(libs.plugins.nowinandroid.android.library) alias(libs.plugins.nowinandroid.android.library.jacoco) + alias(libs.plugins.nowinandroid.android.library.testoptions) alias(libs.plugins.nowinandroid.android.hilt) id("kotlinx-serialization") } android { namespace = "com.google.samples.apps.nowinandroid.core.data" - testOptions { - unitTests { - isIncludeAndroidResources = true - isReturnDefaultValues = true - } - } } dependencies { diff --git a/core/designsystem/build.gradle.kts b/core/designsystem/build.gradle.kts index 31635865c..53d99af4a 100644 --- a/core/designsystem/build.gradle.kts +++ b/core/designsystem/build.gradle.kts @@ -17,6 +17,7 @@ plugins { alias(libs.plugins.nowinandroid.android.library) alias(libs.plugins.nowinandroid.android.library.compose) alias(libs.plugins.nowinandroid.android.library.jacoco) + alias(libs.plugins.nowinandroid.android.library.testoptions) alias(libs.plugins.roborazzi) } diff --git a/core/network/build.gradle.kts b/core/network/build.gradle.kts index 689a99e73..5da041bbc 100644 --- a/core/network/build.gradle.kts +++ b/core/network/build.gradle.kts @@ -17,6 +17,7 @@ plugins { alias(libs.plugins.nowinandroid.android.library) alias(libs.plugins.nowinandroid.android.library.jacoco) + alias(libs.plugins.nowinandroid.android.library.testoptions) alias(libs.plugins.nowinandroid.android.hilt) id("kotlinx-serialization") id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") @@ -27,11 +28,6 @@ android { buildConfig = true } namespace = "com.google.samples.apps.nowinandroid.core.network" - testOptions { - unitTests { - isIncludeAndroidResources = true - } - } } secrets { diff --git a/feature/foryou/build.gradle.kts b/feature/foryou/build.gradle.kts index 004fe8ad6..10deff781 100644 --- a/feature/foryou/build.gradle.kts +++ b/feature/foryou/build.gradle.kts @@ -18,6 +18,7 @@ plugins { alias(libs.plugins.nowinandroid.android.feature) alias(libs.plugins.nowinandroid.android.library.compose) alias(libs.plugins.nowinandroid.android.library.jacoco) + alias(libs.plugins.nowinandroid.android.library.testoptions) alias(libs.plugins.roborazzi) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 530123c8c..3f41a698e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -182,11 +182,13 @@ nowinandroid-android-application-compose = { id = "nowinandroid.android.applicat nowinandroid-android-application-firebase = { id = "nowinandroid.android.application.firebase", version = "unspecified" } nowinandroid-android-application-flavors = { id = "nowinandroid.android.application.flavors", version = "unspecified" } nowinandroid-android-application-jacoco = { id = "nowinandroid.android.application.jacoco", version = "unspecified" } +nowinandroid-android-application-testoptions = { id = "nowinandroid.android.application.testoptions", version = "unspecified" } nowinandroid-android-feature = { id = "nowinandroid.android.feature", version = "unspecified" } nowinandroid-android-hilt = { id = "nowinandroid.android.hilt", version = "unspecified" } nowinandroid-android-library = { id = "nowinandroid.android.library", version = "unspecified" } nowinandroid-android-library-compose = { id = "nowinandroid.android.library.compose", version = "unspecified" } nowinandroid-android-library-jacoco = { id = "nowinandroid.android.library.jacoco", version = "unspecified" } +nowinandroid-android-library-testoptions = { id = "nowinandroid.android.library.testoptions", version = "unspecified" } nowinandroid-android-lint = { id = "nowinandroid.android.lint", version = "unspecified" } nowinandroid-android-room = { id = "nowinandroid.android.room", version = "unspecified" } nowinandroid-android-test = { id = "nowinandroid.android.test", version = "unspecified" }