Gradle is the build tool which is used by android studio. When I was started developing android application I does not try to change anything inside build.gradle file and I kept as it is. But now I know that can be use to improve our development life.

1.Build Types

When we are developing application we have to manage development app, release app , beta release app separately. May be each of these app have different configuration. In early day what I was did is I maintained a config file and when we do different release I change the config variable based on the release. Actually we do not need to do that kind of changes to config file when we are building dev or release app. What we can do is we can define all config in the build.gradle file itself.

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

buildConfigField "String", "S3_STORAGE", "\"https://s3-us-west-2.amazonaws.com/ourstoragerelease/\""

}
debug {
minifyEnabled false
debuggable true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

buildConfigField "String", "S3_STORAGE", "\"https://s3-us-west-2.amazonaws.com/ourstoragedev/\""

}

In this we have defined different config for release and debug version. If we want to use this S3_STORAGE variable inside code we can use it like this

BuildConfig.S3_STORAGE

2. Custom Fields

As I mentioned in above we can use buildConfigField to define custom fields.

buildConfigField "String", "S3_STORAGE", "\"https://s3-us-west-2.amazonaws.com/ourstoragedev/\""

In this String is a data type and “S3_STORAGE” is the field name

3. Resource Values

Same as the custom field values we can define resource values inside build.gradle file. I used this lot when I was integrating Admob to the app. In Admob we cannot use our real adsId to testing purposes. In that case Admob provide testing AdsId. Therefore we can define two resValue inside release and debug config. Inside release config we can add real AdsId and inside debug config we can add test id.

release{
resValue "string", "ABMOB", "728345782357"
}
debug{
resValue "string", "ABMOB", "772322222"
}

we can use this config inside AdView Like this

<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/ABMOB">
</com.google.android.gms.ads.AdView>

4. Extra properties

When we are using support design library in android it’s good to use same library version. If not there can be some error occur if there is incompatibility happen. Also when I am developing a app I will define app version as a ext and It will easy to update the app version in the single place.

android {
ext {
versionMajor = 1
versionMinor = 2
versionPatch = 2
supportDesign = "25.0.0"
annotation = "25.0.0"
}
 defaultConfig {
applicationId "com.proj.proj"
minSdkVersion 16
targetSdkVersion 26
versionCode 16
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
}
}
dependencies {
compile "com.android.support:appcompat-v7:${supportDesign}"
compile "com.android.support:design:${supportDesign}"
}
x