简体   繁体   中英

Android - Changing Gradle.properties according to flavor

I'm building an Android app with React Native and I have troubles configuring some of my API key.

I have one of my API key in my Gradle.properties

RNGP_ANDROID_API_KEY=mysupersecretkey

And I have two different flavor: beta for testing and prod for production. I would like to override the RNGP_ANDROID_API_KEY in my two flavors Like this:

flavorDimensions 'env'
productFlavors {
    beta {
        dimension 'env'
        applicationId "com.fake.package"
        signingConfig signingConfigs.release
        manifestPlaceholders=[google_api:"beta_key"]
    }
    prod {
        dimension 'env'
        applicationId "com.fake.package"
        signingConfig signingConfigs.release
        manifestPlaceholders=[google_api:"prod_key"]
    }
}

It works well to override value in the manifest but I don't know how to change the Gradle.properties value.

Please let me know what I can do!

Instead of writing in Gradle.properties

Try to use it as below,

flavorDimensions 'env'
productFlavors {
    beta {
        dimension 'env'
        applicationId "com.fake.package"
        signingConfig signingConfigs.release
        manifestPlaceholders=[google_api:"beta_key"]
        buildConfigField 'String', 'RNGP_ANDROID_API_KEY', '"mysupersecretkey"'
    }
    prod {
        dimension 'env'
        applicationId "com.fake.package"
        signingConfig signingConfigs.release
        manifestPlaceholders=[google_api:"prod_key"]
        buildConfigField 'String', 'RNGP_ANDROID_API_KEY', '"mysupersecretkey"'
    }
}

You can access this variable by BuildConfig.RNGP_ANDROID_API_KEY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM