Gradle is an open-source build automation system that was conceived upon a Groovy-based domain-specific language (DSL). Gradle was designed for multi-project builds, which can grow to be quite large. It supports incremental builds by intelligently determining which parts of the project are up-to-date, so that any task dependent upon those parts will not be re-executed.
Gradle is being heralded as more than a build tool, but also as a means for automating the compilation, test, and release process. Qualified by its developers as a quantum leap for building technology in the Java world, some of Gradle’s features are:
Everything in Gradle sits on top of two basic concepts: projects and tasks. A project is a collection of tasks, and each task performs some actions, such as compiling classes, or running unit tests.
How to define constants in build.gradle and use it in Android code
A typical Android studio project has a project level build.gradle file and as many module-level build.gradle as there are modules.
Define in build.gradle
android { buildTypes { debug { buildConfigField "boolean", "VALUE_BOOLEAN", "true" buildConfigField "String", "VALUE_STRING", "debug" } release { buildConfigField "boolean", "VALUE_BOOLEAN", "false" buildConfigField "String", "VALUE_STRING", "release" } } }
Then, in your code, you can write :
if (BuildConfig.VALUE_BOOLEAN) { // statements }
Also we can define string resources and use it value in AndroidManifest.xml. The Google Maps API V2 requires that your Maps API key be placed in your AndroidManifest.xml file, like this:
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="(YOUR MAPS API KEY)"/>
Since this is in the manifest, you don’t have the option to change this API key at runtime. If you wanted to use a different Maps API key for your development and production build versions, you would have to change this entry in the manifest each time you built a release version of your app.
With Gradle however, you can specify different XML string resources for your debug and release builds. In this case, we can make a string resource for our Maps API key in our build.gradle file:
android { ... buildTypes { debug { resValue "string", "GOOGLE_MAPS_ANDROID_API_KEY", "(your development Maps API key)" } release { resValue "string", "GOOGLE_MAPS_ANDROID_API_KEY", "(your production Maps API key)" } } }
The string resource GOOGLE_MAPS_ANDROID_API_KEY
will be generated during a debug or release build. Now you can refer to this string in your manifest:
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="@string/GOOGLE_MAPS_ANDROID_API_KEY"/>
Manage Android dependencies versions
Dependencies are usually managed at the app-module level, and your app-module build.gradle file can quickly get messy from dependencies. It gets even worse, when you have other modules you reference in your app-module, each with its own dependencies.
We can extract some hardcoded text (like versions) in build.gradle into an ext
block. Our build.gradle file will now look like this:
ext { supportLibraryVersion = '27.0.2' playServicesVersion = '11.6.2' firebaseVersion = '11.6.2' } dependencies { ... compile "com.android.support:support-v4:$supportLibraryVersion" compile "com.google.android.gms:play-services-maps:$playServicesVersion" compile "com.google.firebase:firebase-crash:$firebaseVersion" ... }
Useful links