build.gradle


Overview

This `build.gradle` file is the primary Gradle build configuration for an Android application module in a multi-module project. It defines the build environment, dependencies, and Android-specific build options necessary to compile, package, and test the application. The file integrates third-party tools such as Fabric (Crashlytics SDK for crash reporting) and configures Android build settings including SDK versions, build types, data binding, and Java compatibility.

The file also references a local library module (`:lib`) and external Maven repositories, ensuring all required dependencies and plugins are resolved during the build process.


Detailed Explanation

1. buildscript Block

Configures the repositories and dependencies required for the Gradle build itself, particularly for plugins.

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
        google()
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

**Usage**: This block ensures that the Fabric plugin is available to the build system.


2. repositories Block (Project-level)

Specifies repositories to resolve the app module's dependencies.

repositories {
    maven { url 'https://maven.fabric.io/public' }
    flatDir {
        dirs project(':lib').file('libs')
    }
}

3. apply plugin

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

*Note:* There is commented out code that shows previous or alternative plugin and repository configurations, including the `dexcount-gradle-plugin` which is currently not active.


4. android Block

Configures Android-specific build settings:

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    dataBinding {
        enabled = true
    }

    defaultConfig {
        applicationId "com.tubitv.demo"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = "${variant.versionName}.apk"
        }
    }
}

5. dependencies Block

Lists all external libraries and project dependencies required to build and run the app.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation project(':lib')

    implementation "com.android.support:support-core-utils:27.1.1"

    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    testImplementation 'junit:junit:4.12'

    implementation('com.crashlytics.sdk.android:crashlytics:2.6.7@aar') {
        transitive = true;
    }

    //dagger 2
    annotationProcessor "com.google.dagger:dagger-compiler:2.9"
    compileOnly 'javax.annotation:jsr250-api:1.0'
}

Important Implementation Details


Interaction with Other Parts of the System


Usage Examples

Building the APK

Run from the project root directory:

./gradlew assembleRelease

This will produce a release APK named `1.0.apk` (from `versionName`) in `app/build/outputs/apk/release/`.

Running Unit Tests:

./gradlew test

Running Android Instrumentation Tests:

./gradlew connectedAndroidTest

Visual Diagram

The following Mermaid flowchart illustrates the main configuration sections of this `build.gradle` and their relationships:

flowchart TD
    A[buildscript] --> B[repositories]
    A --> C[dependencies]
    D[repositories] --> E[maven.fabric.io]
    D --> F[flatDir libs in :lib]
    G[apply plugin] --> H[com.android.application]
    G --> I[io.fabric]
    J[android]
    J --> K[compileSdkVersion & buildToolsVersion]
    J --> L[dataBinding enabled]
    J --> M[defaultConfig]
    J --> N[buildTypes]
    J --> O[compileOptions]
    J --> P[applicationVariants.all]
    Q[dependencies]
    Q --> R[fileTree libs]
    Q --> S[project(':lib')]
    Q --> T[Android Support Libraries]
    Q --> U[Crashlytics SDK]
    Q --> V[Dagger 2]
    Q --> W[Testing Libraries]

    A --> J
    D --> Q
    G --> J

Summary

This `build.gradle` file configures an Android application module with:

It plays a central role in orchestrating the build process, managing dependencies, and enabling key features for the app's development lifecycle.