Android custom gradle properties
This is an example demo for adding gradle properties in a custom gradle properties and for creating multiple gradle property files in Android.
Define some properties in a custom gradle property file, my_credentials.properties at the root directory of the Android project.
#define credential variables here, read it in the project build.gradle, and use it in the app build.gradle apiId=abcdefg apiPassword=123456789
In the project gradle.build file, in the buildscript block, read the properties defined in the my_credentials.properties file.
def credentialsProperties = new Properties() if (file("$rootDir/my_credentials.properties").exists()) { file("$rootDir/my_credentials.properties").withInputStream { credentialsProperties.load(it) } } ext.credentialsProperties = credentialsProperties
In the app gradle.build file, use properties defined in the above. The following will generate two constants in the BuildConfig.kt
buildConfigField "String", "API_ID", "\"${credentialsProperties.apiId}\"" buildConfigField "String", "API_PASSWORD", "\"${credentialsProperties.apiPassword}\""
The final product of the above, BuildConfig.kt
public final class BuildConfig { public static final boolean DEBUG = Boolean.parseBoolean("true"); public static final String APPLICATION_ID = "com.example.gradle_custom_properties"; public static final String BUILD_TYPE = "debug"; public static final String FLAVOR = ""; public static final int VERSION_CODE = 1; public static final String VERSION_NAME = "1.0"; // Fields from default config. public static final String API_ID = "abcdefg"; public static final String API_PASSWORD = "123456789"; }
Complete example in Github
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts