diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f411814..0602f2f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,4 @@ [versions] -gradle = "7.0.4" gradleVersion = "8.4.2" junit = "4.13.2" kotlin = "1.9.24" @@ -10,7 +9,7 @@ mockitoKotlin = "2.2.0" secretsGradlePlugin = "2.0.1" [libraries] -gradle = { module = "com.android.tools.build:gradle", version.ref = "gradle" } +agp-api = "com.android.tools.build:gradle-api:7.0.4" gradle-v842 = { module = "com.android.tools.build:gradle", version.ref = "gradleVersion" } junit = { module = "junit:junit", version.ref = "junit" } kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlinGradlePlugin" } diff --git a/secrets-gradle-plugin/build.gradle.kts b/secrets-gradle-plugin/build.gradle.kts index 1266e69..03d00b2 100644 --- a/secrets-gradle-plugin/build.gradle.kts +++ b/secrets-gradle-plugin/build.gradle.kts @@ -24,9 +24,9 @@ java { } dependencies { - compileOnly(libs.gradle) + compileOnly(libs.agp.api) implementation(libs.kotlin.stdlib) - testImplementation(libs.gradle) + testImplementation(libs.agp.api) testImplementation(libs.junit) testImplementation(libs.mockito.kotlin) } diff --git a/secrets-gradle-plugin/src/main/java/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/Extensions.kt b/secrets-gradle-plugin/src/main/java/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/Extensions.kt index c9b877d..75c8e0e 100644 --- a/secrets-gradle-plugin/src/main/java/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/Extensions.kt +++ b/secrets-gradle-plugin/src/main/java/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/Extensions.kt @@ -20,9 +20,6 @@ import com.android.build.api.variant.ApplicationAndroidComponentsExtension import com.android.build.api.variant.BuildConfigField import com.android.build.api.variant.LibraryAndroidComponentsExtension import com.android.build.api.variant.Variant -import com.android.build.gradle.AppExtension -import com.android.build.gradle.LibraryExtension -import com.android.build.gradle.internal.core.InternalBaseVariant import org.gradle.api.Project import java.io.FileNotFoundException import java.util.Properties @@ -33,12 +30,6 @@ fun Project.androidAppComponent(): ApplicationAndroidComponentsExtension? = fun Project.androidLibraryComponent(): LibraryAndroidComponentsExtension? = extensions.findByType(LibraryAndroidComponentsExtension::class.java) -fun Project.androidProject(): AppExtension? = - extensions.findByType(AppExtension::class.java) - -fun Project.libraryProject(): LibraryExtension? = - extensions.findByType(LibraryExtension::class.java) - fun Project.loadPropertiesFile(fileName: String): Properties { // Load file val propertiesFile = file(fileName) @@ -73,20 +64,6 @@ fun Variant.inject(properties: Properties, ignore: List) { } } -fun InternalBaseVariant.inject(properties: Properties, ignore: List) { - val ignoreRegexs = ignore.map { Regex(pattern = it) } - properties.keys.map { key -> - key as String - }.filter { key -> - key.isNotEmpty() && !ignoreRegexs.any { it.containsMatchIn(key) } - }.forEach { key -> - val value = properties.getProperty(key).removeSurrounding("\"") - val translatedKey = key.replace(javaVarRegexp, "") - buildConfigField("String", translatedKey, value.addParenthesisIfNeeded()) - mergedFlavor.manifestPlaceholders[translatedKey] = value - } -} - fun String.addParenthesisIfNeeded(): String { if (isEmpty()) { return this diff --git a/secrets-gradle-plugin/src/main/java/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/SecretsPlugin.kt b/secrets-gradle-plugin/src/main/java/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/SecretsPlugin.kt index 0ce85a1..3eb5f9b 100644 --- a/secrets-gradle-plugin/src/main/java/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/SecretsPlugin.kt +++ b/secrets-gradle-plugin/src/main/java/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/SecretsPlugin.kt @@ -16,7 +16,6 @@ package com.google.android.libraries.mapsplatform.secrets_gradle_plugin import com.android.build.api.variant.Variant -import com.android.build.gradle.internal.core.InternalBaseVariant import org.gradle.api.Plugin import org.gradle.api.Project import java.io.FileNotFoundException diff --git a/secrets-gradle-plugin/src/test/kotlin/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/SecretsPluginTest.kt b/secrets-gradle-plugin/src/test/kotlin/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/SecretsPluginTest.kt index 297470e..82d376d 100644 --- a/secrets-gradle-plugin/src/test/kotlin/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/SecretsPluginTest.kt +++ b/secrets-gradle-plugin/src/test/kotlin/com/google/android/libraries/mapsplatform/secrets_gradle_plugin/SecretsPluginTest.kt @@ -14,15 +14,16 @@ package com.google.android.libraries.mapsplatform.secrets_gradle_plugin -import com.android.build.gradle.internal.core.InternalBaseVariant +import com.android.build.api.variant.BuildConfigField +import com.android.build.api.variant.Variant import com.nhaarman.mockitokotlin2.doReturn import com.nhaarman.mockitokotlin2.mock -import com.nhaarman.mockitokotlin2.verify import org.gradle.api.Project import org.gradle.api.ProjectConfigurationException import org.gradle.api.internal.project.ProjectInternal import org.gradle.testfixtures.ProjectBuilder -import org.junit.Assert +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Ignore import org.junit.Rule @@ -35,10 +36,9 @@ class SecretsPluginTest { @JvmField val tempFolder = TemporaryFolder() - lateinit var placeholders: MutableMap lateinit var root: Project lateinit var project: Project - lateinit var variant: InternalBaseVariant + lateinit var variant: Variant @Before fun setUp() { @@ -51,12 +51,11 @@ class SecretsPluginTest { .withName("project") .withParent(root) .build() - placeholders = mutableMapOf() - val flavor = mock { - on { manifestPlaceholders } doReturn placeholders - } variant = mock { - on { mergedFlavor } doReturn flavor + on { buildConfigFields } doReturn + project.objects.mapProperty(String::class.java, BuildConfigField::class.java) + on { manifestPlaceholders } doReturn + project.objects.mapProperty(String::class.java, String::class.java) } project.pluginManager.apply("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") } @@ -157,14 +156,18 @@ class SecretsPluginTest { private fun check(vararg keyValues: Pair) { keyValues.forEach { (key, value) -> - Assert.assertEquals(value, placeholders[key]) - verify(variant).buildConfigField("String", key, value.addParenthesisIfNeeded()) + assertEquals(value, variant.manifestPlaceholders.getting(key).get()) + variant.buildConfigFields.getting(key).get().let { + assertEquals("String", it.type) + assertEquals(value.addParenthesisIfNeeded(), it.value) + assertEquals(null, it.comment) + } } } private fun checkKeysNotIn(vararg keys: String) { keys.forEach { - Assert.assertFalse(placeholders.containsKey(it)) + assertTrue(variant.manifestPlaceholders.getting(it).orNull == null) } } }