Ian Hayward

The blog of a tech lead, equal parts tutorial and archive for things I have worked on.


I don't do adverts and would rather a free crypto viewing model. If you are interested in either how that works or helping me out, click on the Brave or BitTube links below

Android Gradle tips

April 14, 2023

Going over the basics for configuring Gradle on an Android project

Android Gradle is a powerful build tool that allows developers to easily manage dependencies, build multiple variants of an app, and customise build settings. In this blog post, we’ll take a look at some tips and tricks for using Android Gradle to streamline your Android development process.

1. Using the compile vs implementation configuration

When adding dependencies to your build.gradle file, you may have noticed two different configuration options: compile and implementation. Both options serve the same purpose, but implementation is the recommended configuration for new projects. The difference between the two is that compile also includes transitive dependencies, while implementation only includes the specified dependency. This can lead to version conflicts and unexpected behaviour if not managed properly.

2. Managing dependencies with the dependencies block

The dependencies block in the build.gradle file is where you specify all of the dependencies for your project. When adding a new dependency, you can use the compile or implementation configuration and specify the library name, version, and any additional configuration options.

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.google.firebase:firebase-core:16.0.8'
}

3. Using the exclude configuration

If you find yourself in a situation where a transitive dependency is causing conflicts or unexpected behavior, you can use the exclude configuration to remove it. The exclude configuration allows you to specify specific dependencies to be excluded from a transitive dependency.

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation ('com.google.firebase:firebase-core:16.0.8') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
}

4. Using the configurations block

The configurations block allows you to configure various aspects of your build, such as the Java version, packaging options, and signing settings. For example, you can use the configurations block to specify the minimum and target SDK version for your app.

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
    }
}

5. Using the buildTypes block

The buildTypes block allows you to configure different build types for your project, such as debug and release. This is useful for building different versions of your app with different settings, such as signing and obfuscation.

android {
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

6. Using the productFlavors block

The productFlavors block allows you to configure different product flavors for your project, such as free and paid. This is useful for building different versions of your app with different features, such as in-app purchases or ads.

android {
    productFlavors {
        free {
            applicationId "com.example.app.free"
        }
        paid {
            applicationId "com.example.app.paid"
        }
    }
}

7. Using the com.android.application plugin

The com.android.application plugin is the main plugin for building Android applications. It provides a number of tasks for building, testing, and deploying your app. It also provides access to the Android SDK and other dependencies needed for building your app.

apply plugin: 'com.android.application'

8. Using the com.android.library plugin

The com.android.library plugin is used for building Android libraries. It provides a number of tasks for building, testing, and deploying your library. It also provides access to the Android SDK and other dependencies needed for building your library.

apply plugin: 'com.android.library'

9. Using the com.android.test plugin

The com.android.test plugin is used for building and running Android instrumentation tests. It provides a number of tasks for building and running tests, as well as configuring test options such as test runner and test instrumentation.

apply plugin: 'com.android.test'

10. Using the com.android.support library

The com.android.support library is a set of libraries that provide backwards compatibility for older versions of Android. It includes support for various UI components, as well as other features such as appcompat, design, and cardview.

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
}

In conclusion, Android Gradle is a powerful build tool that provides a number of features and options for building, testing, and deploying Android apps and libraries. By using the tips and tricks outlined in this post, you can streamline your Android development process and make the most out of the powerful capabilities of Android Gradle.

Share This Post