Skip to content

Commit

Permalink
"com.android.library" Gradle Module Support (#15)
Browse files Browse the repository at this point in the history
* ready to review

* remove unused import

* update README.md with new use case
  • Loading branch information
ErickSumargo authored Apr 14, 2021
1 parent 342afa4 commit 0564ab6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ plugins {
}
```

This plugin also supports library module type (`com.android.library`). Just install the plugin in your library-level `build.gradle` file and keys will be visible inside that module as well.

## Example Usage

Example contents of `local.properties` under your root project:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
package com.google.secrets_gradle_plugin

import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
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

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)
Expand All @@ -40,7 +44,7 @@ fun Project.loadPropertiesFile(fileName: String) : Properties {

private val javaVarRegexp = Regex(pattern = "((?![a-zA-Z_\$0-9]).)")

fun ApplicationVariant.inject(properties: Properties, ignore: List<String>) {
fun InternalBaseVariant.inject(properties: Properties, ignore: List<String>) {
val ignoreRegexs = ignore.map { Regex(pattern = it) }
properties.keys.map { key ->
key as String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.secrets_gradle_plugin

import com.android.build.gradle.internal.core.InternalBaseVariant
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.io.FileNotFoundException
Expand Down Expand Up @@ -49,38 +50,54 @@ class SecretsPlugin : Plugin<Project> {
defaultProperties ?: throw e
}

// Target: "com.android.application" module variants
project.androidProject()?.applicationVariants?.all { variant ->
// Inject defaults first
defaultProperties?.let {
variant.inject(it, extension.ignoreList)
}
generateConfigKey(project, extension, defaultProperties, properties, variant)
}

properties?.let {
variant.inject(properties, extension.ignoreList)
}
// Target: "com.android.library" module variants
project.libraryProject()?.libraryVariants?.all { variant ->
generateConfigKey(project, extension, defaultProperties, properties, variant)
}
}
}

// Inject build-type specific properties
val buildTypeFileName = "${variant.buildType.name}.properties"
val buildTypeProperties = try {
project.rootProject.loadPropertiesFile(buildTypeFileName)
} catch (e: FileNotFoundException) {
null
}
buildTypeProperties?.let {
variant.inject(it, extension.ignoreList)
}
private fun generateConfigKey(
project: Project,
extension: SecretsPluginExtension,
defaultProperties: Properties?,
properties: Properties?,
variant: InternalBaseVariant
) {
// Inject defaults first
defaultProperties?.let {
variant.inject(it, extension.ignoreList)
}

// Inject flavor-specific properties
val flavorFileName = "${variant.flavorName}.properties"
val flavorProperties = try {
project.rootProject.loadPropertiesFile(flavorFileName)
} catch (e: FileNotFoundException) {
null
}
flavorProperties?.let {
variant.inject(it, extension.ignoreList)
}
}
properties?.let {
variant.inject(properties, extension.ignoreList)
}

// Inject build-type specific properties
val buildTypeFileName = "${variant.buildType.name}.properties"
val buildTypeProperties = try {
project.rootProject.loadPropertiesFile(buildTypeFileName)
} catch (e: FileNotFoundException) {
null
}
buildTypeProperties?.let {
variant.inject(it, extension.ignoreList)
}

// Inject flavor-specific properties
val flavorFileName = "${variant.flavorName}.properties"
val flavorProperties = try {
project.rootProject.loadPropertiesFile(flavorFileName)
} catch (e: FileNotFoundException) {
null
}
flavorProperties?.let {
variant.inject(it, extension.ignoreList)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.google.secrets_gradle_plugin

import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.internal.core.InternalBaseVariant
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
Expand All @@ -38,7 +37,7 @@ class SecretsPluginTest {
lateinit var placeholders: MutableMap<String, Any>
lateinit var root: Project
lateinit var project: Project
lateinit var variant: ApplicationVariant
lateinit var variant: InternalBaseVariant

@Before
fun setUp() {
Expand Down

0 comments on commit 0564ab6

Please sign in to comment.