How to configure Android app via Gradle Android 08.01.2018

How to configure Android app via Gradle

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:

  • Declarative builds and build-by-convention. Based on a rich, extensible Domain Specific Language (DSL) based on Groovy, Gradle provides declarative language elements that the user can assemble as desired. Those elements also provide build-by-convention support for Java, Groovy, OSGi, Web, and Scala projects. The declarative language is extensible, allowing you to add new language elements or enhance the existing ones.
  • Language for dependency-based programming. The declarative language lies on top of a general purpose task graph. It provides utmost flexibility to adapt Gradle to specific or unique development needs.
  • Custom-structured builds. Gradle allows you to apply common design principles to a build. For example, it’s very easy to compose a build from reusable pieces of build logic, creating a well-structured and easily maintained build.
  • Multiproject builds. Gradle allows users to model project relationships in a multi-project according to the user’s layout. Also, it provides partial builds, so that when a single subproject is built, Gradle takes care of building all subprojects that subproject depends on. The user can choose to rebuild the subprojects that depend on a specific subproject.
  • Groovy. Build scripts are written in Groovy. This is intended to orientate Gradle to be used as a language, not as a framework.
  • Free and open source. Gradle is an open-source project, licensed under the ASL (Apache Software License), which can be viewed here.

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