Gradle

Table of Contents


Tips

How to stop Gradle builds for real

Don’t try to stop them using Android Studio. Instead, do it using the command line tool, running ./gradlew stop.

Optimize multidex development builds

Creating multidex output for your project typically adds a significant amount of time to your build due to the merging process. You can mitigate this by setting a minimum SDK version of 21, which allows the Android plugin to perform more efficient dexing.

Here is a partial sample configuration with two different flavors:

android {
    productFlavors {
        dev {
            minSdkVersion 21
        }
        prod {
            minSdkVersion 14
        }
    }
}

Source

How declare screen resource folder?

android {
  sourceSets {
    main {
        res.srcDirs = [
                'src/main/res-main',
                'src/main/res-screen/about',
                'src/main/res-screen/chat',
                'src/main/res-screen/event-detail',
                'src/main/res-screen/event-list',
                'src/main/res-screen/home',
                'src/main/res-screen/login',
        ]
    }
  }
}

or

android {
  sourceSets {
      main {
          file('src/main/res-screen')
                  .listFiles()
                  .each { res.srcDirs += it.path }
      }
  }
}

Note: above works only if you are in Project view

Before decompose.

res/
  layout/
    chat_activity.xml
    chat_toolbar.xml
    chat_item.xml
    chat_share_view.xml
    home_activity.xml
    home_toolbar.xml
    home_fragment_sign_in.xml
    home_fragment_sign_up.xml

After decompose.

res/
  layout/
    chat/
      chat_activity.xml
      chat_toolbar.xml
      chat_item.xml
      chat_share_view.xml
    home/
      home_activity.xml
      home_toolbar.xml
      home_fragment_sign_in.xml
      home_fragment_sign_up.xml

Use prefixes for your resources

Declare resourcePrefix in your build in order to use them in libraries

android {
  resourcePrefix 'prefix_'
}

More info here

Offline mode

You can also run gradle or gradle wrapper with the flag enabled, like:

./gradlew assembleDebug --offline

Creating multidex output for your project typically adds a significant amount of time to your build due to the merging process. You can mitigate this by setting a minimum SDK version of 21, which allows the Android plugin to perform more efficient dexing.