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.+'
}
}
repositories: Defines where Gradle looks for buildscript dependencies.
jcenter()- Popular Maven repository for Java/Kotlin libraries.maven { url 'https://maven.fabric.io/public' }- Repository for Fabric SDK.google()- Google's Maven repository for Android dependencies.
dependencies:
classpath 'io.fabric.tools:gradle:1.+'- Fabric Gradle plugin for integrating Crashlytics.
**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')
}
}
maven { url 'https://maven.fabric.io/public' }: Again includes Fabric’s Maven repository.flatDir { dirs project(':lib').file('libs') }: Includes local.jarfiles located inside thelibsdirectory of the:libproject module.
3. apply plugin
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
com.android.application: Marks this module as an Android application.io.fabric: Applies the Fabric plugin for Crashlytics integration.
*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"
}
}
}
compileSdkVersion: API level used to compile the app (27).
buildToolsVersion: Version of Android build tools.
dataBinding.enabled: Enables Android Data Binding.
defaultConfig:
applicationId: Unique app identifier.minSdkVersion: Minimum supported Android API level.targetSdkVersion: API level targeted by the app.versionCode&versionName: Versioning info.testInstrumentationRunner: Specifies the runner for Android instrumentation tests.
buildTypes:
release: Configuration for release builds.minifyEnabled false: Disables code shrinking.proguardFiles: Defines Proguard config files for code obfuscation and optimization.
compileOptions:
Sets Java compatibility to version 1.8.
applicationVariants.all:
Customizes APK output file name to use the version name (e.g.,
1.0.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'
}
implementation fileTree: Includes all.jarfiles inside thelibsdirectory.implementation project(':lib'): Adds a dependency on the local:libmodule.implementation "com.android.support:support-core-utils:27.1.1": Android Support Library utility classes.androidTestImplementation: Espresso UI testing framework.testImplementation: JUnit for unit tests.implementation('com.crashlytics.sdk.android:crashlytics:2.6.7@aar'): Crashlytics SDK for crash reporting.annotationProcessor: Dagger 2 compiler for dependency injection code generation.compileOnly: Includes javax.annotation API for annotations used only at compile time.
Important Implementation Details
Fabric Integration: The
io.fabricplugin and dependency on Crashlytics SDK enable crash reporting within the app. The Fabric Maven repository is required to resolve these dependencies.Data Binding Enabled: Allows the use of Android Data Binding in layouts, improving UI code maintainability.
Java 8 Compatibility: Enables use of Java 8 language features.
Custom APK Naming: By overriding
outputFileNameinapplicationVariants, APK files are generated with the version name as the filename for easier identification.Dagger 2: Dependency injection is set up with annotation processing, improving modularity and testability.
Local Library Module: The
:libproject module is included as a dependency, indicating modular project structure.
Interaction with Other Parts of the System
Project Modules: Depends on the
:libmodule, meaning this app module likely uses functionality or shared code defined there.External Services: Uses Fabric's Crashlytics for crash reporting, which requires the Fabric Maven repo and the Fabric Gradle plugin.
Testing Frameworks: Integrates Android Espresso for UI testing and JUnit for unit testing.
Build System: This Gradle script configures how the Android build system compiles, packages, and signs the app APK, interacting with Android SDK tools and Gradle plugins.
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:
Android SDK and build tools setup.
Data binding support.
Release build type with Proguard (disabled shrinking).
Java 8 compatibility.
Fabric Crashlytics integration for crash reporting.
Dependency injection via Dagger 2.
Local and external dependencies including support libraries and testing frameworks.
Custom APK naming based on version.
It plays a central role in orchestrating the build process, managing dependencies, and enabling key features for the app's development lifecycle.