build.gradle
Overview
The `build.gradle` file is a Gradle build configuration script for an **Android library module**. It manages the build lifecycle, dependencies, versioning, and publishing configurations of the `tubi-player` library, which is a media streaming video player built on top of Google's ExoPlayer.
This file applies necessary Gradle plugins, defines Android SDK versions, configures build types, sets up dependencies, and configures the Bintray release plugin to enable easy artifact publishing. It also customizes output artifact naming and configures Java compatibility and testing options.
Detailed Explanation
Plugins
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release' // applied after artifact generating plugins
com.android.library: Indicates this module is an Android library rather than an application.
com.novoda.bintray-release: Gradle plugin to automate publishing artifacts to Bintray. Must be applied after the Android plugin.
Buildscript Configuration
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.novoda:bintray-release:0.8.0'
}
}
Defines repositories and classpath dependencies needed for build scripts, specifically the
bintray-releaseplugin.
Repositories
repositories {
flatDir {
dirs 'libs'
}
}
flatDirrepository points to the locallibsdirectory to resolve binary dependencies (e.g.,.aaror.jarfiles).
Version Definition
def libVersion = "v2.2"
Defines the library version used consistently across the build and publishing configuration.
Android Configuration Block
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
dataBinding {
enabled = true
}
testOptions {
unitTests.returnDefaultValues = true
}
defaultConfig {
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName libVersion
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
libraryVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "tubiplayer-${libVersion}.aar"
}
}
}
compileSdkVersion/buildToolsVersion: Specifies SDK and build tools versions.
dataBinding.enabled: Enables Android Data Binding feature.
testOptions.unitTests.returnDefaultValues: Makes unit tests return default values for unmocked methods.
defaultConfig: Sets minimum and target SDK versions, version code and name, and test runner.
buildTypes: Defines
releaseanddebugbuild types, disabling code minification (minifyEnabled false) but including proguard config files.compileOptions: Java 8 compatibility.
libraryVariants.all: Customizes output
.aarfilename totubiplayer-v2.2.aarfor all build variants.
Dependencies
dependencies {
def supportLibraryVersion = '27.1.1'
def playServiceVersion = '16.0.3'
configurations {
all*.exclude group: 'com.android.support', module: 'support-v13'
}
api fileTree(dir: 'libs', include: ['*.jar'])
api(name: 'vaud-text-view-0.0.2', ext: 'aar')
api(name: 'tubi-loading-view-0.0.5', ext: 'aar')
api 'com.afollestad.material-dialogs:core:0.9.4.5'
api 'com.squareup.picasso:picasso:2.5.2'
api 'com.google.android.exoplayer:exoplayer:2.8.4'
api "com.android.support:appcompat-v7:${supportLibraryVersion}"
api 'com.android.support.constraint:constraint-layout:1.0.2'
api "com.android.support:design:${supportLibraryVersion}"
// Chromecast support
api "com.android.support:mediarouter-v7:${supportLibraryVersion}"
api "com.google.android.gms:play-services-cast-framework:${playServiceVersion}"
// Dagger 2 dependency injection
api "com.google.dagger:dagger:2.9"
annotationProcessor "com.google.dagger:dagger-compiler:2.9"
compileOnly 'javax.annotation:jsr250-api:1.0'
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
api "android.arch.lifecycle:runtime:1.0.3"
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'
}
Defines external libraries the project depends on, including:
Android support libraries (
appcompat-v7,design,constraint-layout).Google ExoPlayer for media playback.
Material dialogs and Picasso for UI and image loading.
Chromecast support libraries.
Dagger 2 for dependency injection.
Testing frameworks (
JUnit,Mockito,Espresso).
Excludes the
support-v13module globally to avoid conflicts.Loads local
.aarlibraries fromlibsdirectory.
Publishing Configuration
publish {
userOrg = 'tubitv'
repoName = 'Media'
groupId = 'com.tubitv.media'
artifactId = 'tubi-player'
publishVersion = libVersion
licences = ['MIT']
desc = 'A media streaming video player based on ExoPlayer from google, with convenience methods for ad supported content'
website = 'https://github.com/Tubitv/TubiPlayer'
// Uncomment and configure for real publishing
// dryRun = true
// bintrayUser = properties.getProperty("bintray.user")
// bintrayKey = properties.getProperty("bintray.apikey")
}
Configures Bintray publishing plugin parameters:
userOrg: Organization on Bintray.
repoName: Repository name.
groupId/artifactId/publishVersion: Maven coordinates.
licenses, desc, website: Metadata for the published artifact.
Comments show how to enable dry runs and set authentication keys for publishing.
Important Implementation Details
Output Filename Customization: The
libraryVariants.all { variant -> variant.outputs.all { ... } }block ensures that every build variant produces an.aarnamed consistently astubiplayer-v2.2.aar, simplifying artifact management.Gradle Plugin Ordering: The
bintray-releaseplugin must be applied after the Android plugin to ensure it correctly hooks into artifact generation tasks.Data Binding: Enabled explicitly for this library, enabling usage of Android Data Binding within the module.
Dependency Exclusions: The exclusion of
support-v13prevents conflicts with different versions of Android support libraries.
Interaction With Other Parts of the System
Local AAR Dependencies: The library depends on local
.aarfiles (vaud-text-viewandtubi-loading-view) located in thelibsdirectory, indicating modularization and reuse across the project.Publishing Pipeline: The publishing configuration integrates with Bintray, enabling seamless deployment of this library artifact to a remote Maven repository, which can be consumed by other projects or modules.
Testing Integration: The test dependencies and instrumentation runner ensure this library can be properly unit and UI tested in isolation or as part of the larger application.
Third-party Libraries: The dependencies include core media playback, UI components, dependency injection, and Chromecast support, indicating this library's role in media streaming and playback features within the application ecosystem.
Usage Examples
Building the Library
Run the following command to build the `.aar` artifact:
./gradlew clean assembleRelease
This produces an `.aar` named `tubiplayer-v2.2.aar` under `build/outputs/aar/`.
Publishing the Library
After setting your Bintray credentials, publish to Bintray with:
./gradlew bintrayUpload -PbintrayUser=yourUser -PbintrayKey=yourApiKey
Add `-PdryRun=false` to actually upload rather than simulate.
Visual Diagram
flowchart TD
A[build.gradle] --> B[Apply Plugins]
B --> C[Android Library Plugin]
B --> D[Bintray Release Plugin]
A --> E[Buildscript]
E --> F[Repositories: jcenter, google]
E --> G[Classpath: bintray-release]
A --> H[Repositories]
H --> I[Local flatDir 'libs']
A --> J[Android Configuration]
J --> K[SDK & Build Tools Versions]
J --> L[Data Binding Enabled]
J --> M[Unit Test Options]
J --> N[Default Config: minSdkVersion, targetSdkVersion, versionName]
J --> O[Build Types: release, debug]
J --> P[Java 8 Compatibility]
J --> Q[Output Filename Customization]
A --> R[Dependencies]
R --> S[Local AARs]
R --> T[Android Support Libraries]
R --> U[ExoPlayer, Chromecast]
R --> V[Dagger 2]
R --> W[Testing Frameworks]
A --> X[Publishing Configuration]
X --> Y[Bintray Metadata]
X --> Z[Credentials (commented)]
Summary
This `build.gradle` file is a well-structured Gradle build script tailored for an Android library project that provides a media player. It configures Android build parameters, manages dependencies including local and remote libraries, integrates testing frameworks, customizes output artifacts, and configures automated publishing to Bintray. The file ensures the library is built consistently and can be shared efficiently across different projects within the Tubi ecosystem.