🔥

Android Studio: 環境によって secrets.properties を分ける

に公開

secrets-gradle-plugin をインストールして、buildTypes によって dubug, release それぞれの properties を用意し、ルート直下に配置すればよかった。なおプロジェクトViewでないと local.properties などは表示されないので注意。

  • debug.properties
  • release.properties

https://github.com/google/secrets-gradle-plugin

// build.gradle.kts
android {
    secrets {
        defaultPropertiesFileName = "local.defaults.properties"
        ignoreList.add("keyToIgnore")
        ignoreList.add("sdk.*")
    }

    buildTypes {
        // https://developer.android.com/build/gradle-tips#simplify-app-development
        release {
//            buildConfigField("String", "APP_BASE_URL", "\"https://app.yacho-go.com\"")
            isMinifyEnabled = false // TODO: Enable ProGuard
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
        debug {
            applicationIdSuffix = ".debug"
//            buildConfigField("String", "APP_BASE_URL", "\"http://localhost:3000\"")
            resValue("string", "app_name", "YachoGo Debug")
        }

Gemini Says..

Android allows for secrets.properties to change by flavor, enabling different configurations or sensitive data for each product flavor. This is commonly achieved using the Secrets Gradle Plugin for Android or by manually managing properties files per flavor.

1. Using the Secrets Gradle Plugin for Android:

Installation:

Add the plugin to your project's build.gradle files.
secrets.properties:
Create a secrets.properties file in your project's root directory and define your secrets.

Flavor-specific files:

For flavor-specific secrets, create additional properties files named after your flavors (e.g., flavorName.properties) in the same root directory. The plugin will automatically prioritize and load secrets from these flavor-specific files when building that particular flavor.

Accessing Secrets:

Access the secrets in your build.gradle file using project.properties or in your code via BuildConfig fields or manifest placeholders.

2. Manual Management with Product Flavors:

Define Product Flavors:

Configure your product flavors in the productFlavors block of your module-level build.gradle file.

Flavor-specific secrets.properties (or similar):
Create separate secrets.properties files (or other configuration files) within the source sets of each flavor (e.g., app/src/flavorName/secrets.properties).

Loading Properties:

Write Gradle logic in your build.gradle file to load the appropriate secrets.properties file based on the current build variant. You can then use these loaded properties to define buildConfigField entries or manifest placeholders for use in your application.

Benefits:

Security: Keeps sensitive information out of version control by placing it in .gitignored files.
Flexibility: Allows for different API keys, configurations, or credentials for different app versions (e.g., staging vs. production).
Cleanliness: Separates sensitive data from your main build.gradle file.

Discussion