How can I set a global variable that can be accessed from build.gradle and tasks?
相关问题
- Could not read entry … from cache taskArtifacts.bi
- Configure gradle plugin based on future tasks
- How to fix the error Cannot change strategy of con
- Include pom.xml in Jar with gradle
- Gradle vs Compiler
相关文章
- Android BuildConfig Field generating String incorr
- Gradle Could not find method “() for arguments on
- Gradle Custom Plugin: gradleApi() vs Explicit Depe
- Android Studio 3.5 ERROR: Unable to resolve depend
- How to specify @category in test task in gradle?
- How to clean your build with Flutter RP2 in Androi
- Remove transitive classpath dependency in gradle
- Execution failed for task ':bintrayUpload'
To set a global variable
To access it from anywhere in the project:
For instance:
and then...
For more information see: http://www.gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html
EDIT: As commented by Dmitry, In new versions you can use the following shorthand:
The answer from Guy is excellent. I just want to add the practical code.
Example:
Put something like this in the Project build.gradle:
And put something like this in the Module build.gradle to access it:
Additional, for dynamic global variables you can define global functions in the master
build.gradle
file:First, define your function, for example for git branch:
In
allProjects
section set the variable:In your
build.gradle
files of your sub projects or android modules, get this variable like this:Finally, you can use it in your code like this:
you can also do this : lets say you want to add appcompat with the version 25.3.1 you can add a variable version_name in your project level build gradle
now you can add this to your application level build gradle and avoid any conflicts
compile "com.android.support:appcompat-v7:$version_name" compile "com.android.support:recyclerview-v7:$version_name" compile "com.android.support:design:$version_name"