Passing Variables from Gradle to Java
In Gradle, you can declare variables that can be accessed within Java code during the build process. Here are two methods you can use:
Generating Java Constants
Configure the buildConfigField property in the buildTypes block:
android { buildTypes { debug { buildConfigField "int", "FOO", "42" buildConfigField "String", "FOO_STRING", "\"foo\"" buildConfigField "boolean", "LOG", "true" } release { buildConfigField "int", "FOO", "52" buildConfigField "String", "FOO_STRING", "\"bar\"" buildConfigField "boolean", "LOG", "false" } } }
You can then access these constants in Java using BuildConfig.FOO.
Generating Android Resources
Use the resValue property within the buildTypes block:
android { buildTypes { debug { resValue "string", "app_name", "My App Name Debug" } release { resValue "string", "app_name", "My App Name" } } }
These resources can be accessed in Java via @string/app_name or R.string.app_name.
The above is the detailed content of How to Pass Gradle Variables to Java Code?. For more information, please follow other related articles on the PHP Chinese website!