I started a project in Android Studio, with IntelliJ.
The project includes two files called build.gradle
. One is under the folder app
, and one is under the main folder which is my project name, say MyProject
.
Why the need for two? What is the difference between the two build.gradle
s?
"example-multi/foo/bar has the name 'bar' which is the same as a project of the main build."
Projects' build. gradle file is used for common/shared logic. For example, you can define repositories here (Maven, Google, JCenter, and custom) or specify ext {} with shared variables or classpath [About].
Gradle is a build system (open source) that is used to automate building, testing, deployment, etc. “build. gradle” are scripts where one can automate the tasks.
From the official documentation: Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build. gradle file for build settings specific to that module.
Android Studio project consists of modules, libraries, manifest files and Gradle build files.
Each project contains one top-level Gradle build file. This file is named build.gradle and can be found in the top level directory.
This file usually contains common config for all modules, common functions..
Example:
//gradle-plugin for android
buildscript {
repositories {
mavenCentral() //or jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
}
}
// common variables
ext {
compileSdkVersion = 19
buildToolsVersion = "20.0.0"
}
// a custom function
def isReleaseBuild() {
return version.contains("SNAPSHOT") == false
}
//common config for all projects
allprojects {
version = VERSION_NAME
repositories {
mavenCentral()
}
}
All modules have a specific build.gradle
file.
This file contains all info about this module (because a project can contain more modules), as config,build tyoes, info for signing your apk, dependencies....
Example:
apply plugin: 'com.android.application'
android {
//These lines use the constants declared in top file
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionName project.VERSION_NAME //it uses a property declared in gradle.properties
versionCode Integer.parseInt(project.VERSION_CODE)
}
// Info about signing
signingConfigs {
release
}
// Info about your build types
buildTypes {
if (isReleaseBuild()) {
release {
signingConfig signingConfigs.release
}
}
debug {
applicationIdSuffix ".debug"
versionNameSuffix "-debug"
}
}
// lint configuration
lintOptions {
abortOnError false
}
}
//Declare your dependencies
dependencies {
//Local library
compile project(':Mylibrary')
// Support Libraries
compile 'com.android.support:support-v4:20.0.0'
// Picasso
compile 'com.squareup.picasso:picasso:2.3.4'
}
You can find more info here: http://developer.android.com/sdk/installing/studio-build.html
build.gradle (Project:My-app)
Top-level build file where you can add configuration options common to all sub-projects/modules.
Each project contains a top-level Gradle file. It usually contains common configurations for all modules. Whatever is included in this top-level Gradle gile, it will affect all modules.
Example:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
//Maven plugin
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle (Module:app)
Build file of your specific module (where you add your dependencies, signing configurations, build types, flavors, etc.)
All modules have a specific Gradle file. Whatever is included in this gradle file, it will only affect the module that is included on.
Example:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.hrskrs.gesturefun"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
zipAlignEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
zipAlignEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':gesture-fun')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With