Hi, today we’re going to talk about the tool that we use every day, but might not get into details of what it is and how it works. We’re going to talk about what Gradle, basics of syntax and DSL. Let’s get started!
Since Android Studio generates all the boiler plate code for us, we don’t actually need write any gradle scripts. To add dependencies you just add compile line, but after a while you might start asking questions: what those buildscripts, classpaths mean.
Ok, Gradle is a build system, but what is Gradle wrapper? See, the reason we don’t need to download and install Gradle like we do with JDK is because of Gradle Wrapper. It downloads the required Gradle version automatically declared in gradle-wrapper.properties distributionUrl
Gradlew is the program we use, which delegates tasks to the project’s Gradle version. It’s located in the root of the project. All the dowloaded Gradle versions you can see by running this command
cd $HOME/.gradle/wrapper/dists && ls
So wrapper requires quite a few files, Andorid Studio generates them when creating a new project. If you want to create them manually you need to run this command, it requires you to install Gradle
gradle wrapper --gradle-version 2.2.3
And then you can change wrapper version in .properties file
Let’s start with the very first method that you find – buildscript in the project’s build.gradle file. Why do we need it?
First of all, Gradle itself doesn’t know anything about Android. All those android, minSdkVersion come from Android Plugin for Gradle. And obviously we need to have it before building the app. So basically buildscript is like a Gradle configuration. Since it’s distributed on jCenter, we need to include repositories : jCenter. And classpath is dependency just like we declare for app, but it’s a Gradle dependency. Try to remove classpath with plugin and sync your project. This answer explains it pretty well
So we downloaded and applied Android Plugin for Gradle with
apply plugin: 'com.android.application'
Now let’s see at the basic Android specific variables.
Compile Sdk Version
This determines the version of Android Sdk that your app uses. For example if you set it to 15, you might need need to download it. And then you’ll be restrained with version 15 Sdk features and classes. Of course compat libraries are here to help.
Are exactly what it sounds, for building APK’s.
Target Sdk Version
This one is actually for forward compatibility as described in the documentation. To maintain style appearance with newer versions of Android.
By default there’s two build types – debug, release. Debug isn’t shown, it has signing with default keystore and more. You can change default setting in here like min sdk version, but this one isn’t a great fit for build types. And you can create build specific packages by creating it under src directory, for example : src/release, src/debug
This is a perfect place for customizing different app versions. Whether it’s by id, min sdk version etc. And the same package logic goes here, src/free, src/paid etc. By the way, by adding those flavors
And switching to modernDebug during development you’ll have faster builds, but make sure to use legacy release for release builds. And minSdkVersion is up to you of course. It’s the min 21 that makes the difference
Ok, that’s it for the basics, we’ll definitely go more details with Gradle in future. Don’t forget to subscribe, follow me on Twitter, Facebook, G+ and share with friends if you thinkg they will benefit from it!