Setup Gradle for your Android project like a boss – part 2

Gradle is a native build system supported by Android StudioIn the previous post I covered some basics and then focused on generating version code and name, which was especially helpful while building apk. Time to move on to the other fancy tricks.

Shared dependency version

Some libraries are meant to be split into modules. Usually, if we use couple of them, we should use all the modules in the same version. An example of such approach could be Google Play Services, AppCompat and design libraries or Retrofit. You should ensure that you’re using the same version of those library modules, otherwise you’ll get in trouble. There is a trick that will help you maintain that.

In the project build.gradle define section like this:

“ext” is not an obligatory name. It’s just a name of the section I came up with. You could use any. Define this section in the root of project’s build. gradle.

Now in your app build. gradle you could add your dependencies like this:

That’s it. Simple, right? Now you can easily change your library versions without missing any and getting yourself in troubles.

SDK and build tools

The same way we just defined shared library versions we could setup values like:

  • compileSdkVersion
  • buildToolsVersion
  • minSdkVersion
  • targetSdkVersion

Just add them to the ext section we defined earlier in the project’s build.gradle:

Then use it in your app’s build.gradle:



Flavors are a useful way to differentiate built types. You could use separate flavor if there is production and stage (dev) backend available. Right now I’m gonna assume you already know what the flavors are and skip the basics. Let’s take the example with an URL to production and stage API. Instead doing some conditions in the code you setup buildConfig field in your app build.gradle. Let’s have a look at some example of flavors:

You should specify that. This way your prod and stage app could exist on the device in parallel not overwriting themselves.

It can be some simple constant that you want to access from java code like:

It’s enclosed with escaped “, otherwise currently it might not be treated as a String later. You could make numeric fields without them though. You can now use this String field to send requests to the api.

Shared BuildConfig fields

If for some reason you want to have a buildConfig field that has the same value for every flavor, you don’t have to keep it in each of it. You could do it like this:



With all the pros and cons of Gradle, I’d vote for it. Surely the cases presented here are not everything you could do to improve your work process.

You know some other cool use cases? Don’t hesitate to share it in the comments section. I’d love to see them all!

Subscribe to our newsletter