Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Gradle on Steroids

Avatar for Sumit Das Sumit Das
October 15, 2016

Gradle on Steroids

Slides for the talk about optimising Gradle builds, presented at GDG BLRDROID'S DEVFEST 2016.

Avatar for Sumit Das

Sumit Das

October 15, 2016

More Decks by Sumit Das

Other Decks in Programming

Transcript

  1. Measuring the overhead Test Subject: Google IO App - 28

    libraries - 53149 method references
  2. Measuring the Overhead The --dry-run command line flag skips running

    all the tasks $ ./gradlew assembleDebug --dry-run :android:validateDebugSigning SKIPPED :android:packageDebug SKIPPED :android:zipalignDebug SKIPPED :android:assembleDebug SKIPPED BUILD SUCCESSFUL Total time: 4.256 secs
  3. ./gradlew build --dry-run ... BUILD SUCCESSFUL Total time: 4.286 secs

    Without --configure-on-demand ./gradlew build --dry-run --configure-on-demand ... BUILD SUCCESSFUL Total time: 3.574 secs With --configure-on-demand
  4. ./gradlew build --dry-run --configure-on-demand --no-daemon ... BUILD SUCCESSFUL Total time:

    3.63 secs Without --daemon ./gradlew build --dry-run --configure-on-demand --daemon ... BUILD SUCCESSFUL Total time: 1.709 secs With --daemon
  5. ./gradlew build --dry-run --configure-on-demand --daemon ... BUILD SUCCESSFUL Total time:

    1.709 secs Gradle 2.2.1 ./gradlew build --dry-run --configure-on-demand --daemon ... BUILD SUCCESSFUL Total time: 1.104 secs Gradle 2.14
  6. #Tue Dec 02 14:11:25 EST 2014 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists

    distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip Gradle Version Upgrade Gradle Version in gradle/wrapper/gradle-wrapper.properties
  7. productFlavors { dev { minSdkVersion 21 } prod { minSdkVersion

    14 } } dependencies { compile 'com.android.support:multidex:1.0.0' }
  8. public class MainActivity extends AppCompatActivity { private MixpanelAPI mixpanelAPI; private

    Button buttonTrigger; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String token = context.getString(R.string.mixpanel_token) mixpanelAPI = MixpanelAPI.getInstance(this, mixpanelToken); mixpanelAPI.track(“some_event”, new JSONObject()); } }
  9. public class MainActivity extends AppCompatActivity { private MixpanelAPI mixpanelAPI; private

    Button buttonTrigger; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String token = context.getString(R.string.mixpanel_token) mixpanelAPI = MixpanelAPI.getInstance(this, mixpanelToken); mixpanelAPI.track(“some_event”, new JSONObject()); } }
  10. Debug Mixpanel Tracker public class MixpanelAnalyticsTracker extends AnalyticsTracker { public

    MixpanelAnalyticsTracker(Context context) { super(context); } @Override public void track(String eventName, JSONObject jsonObject) { } }
  11. Release Mixpanel Tracker public class MixpanelAnalyticsTracker extends AnalyticsTracker { private

    final MixpanelAPI mixpanelAPI; public MixpanelAnalyticsTracker(Context context) { super(context); String mixpanelToken = context.getString(R.string.mixpanel_name) mixpanelAPI = MixpanelAPI.getInstance(context, mixpanelToken); } @Override public void track(String eventName, JSONObject jsonObject) { mixpanelAPI.track(eventName, jsonObject); } }
  12. Google Play Services : Maps Don’t use com.google.android.gms:play-services:9.4.0 Methods count

    : 74646 Use com.google.android.gms:play-services-maps:9.4.0 Methods count : 15306 Apply Plugins Judiciously
  13. def cmd = 'git rev-list HEAD --count' def gitVersion =

    cmd.execute().text.trim().toInteger() android { defaultConfig { versionCode gitVersion } }
  14. But what happens when you add a dependency to Dev

    gradle file and forget to add it in CI gradle file? Build Fails
  15. dependencies { compile commonLibraries.retrofit compile commonLibraries.dagger compile commonLibraries.timber testCompile testLibraries.junit

    testCompile testLibraries.mockitoCore androidTestCompile testLibraries.junit androidTestCompile testLibraries.mockWebServer androidTestCompile testLibraries.espressoIntents } dependencies.gradle
  16. Is maintaining two gradle files the right approach? - It’s

    a hack - Needs a lot of discipline and communication across the team - Lot of code duplication
  17. Using gradle tasks for: - Handpicking build variant(s) while building

    project, preventing the generation of all possible build variants on dev builds. - Generating Jacoco reports only on CI Merging the gradle files
  18. Configuring dependencies for specific build variant or testing source set.

    debugCompile "com.android.support:multidex:1.0.0" debugCompile "com.squareup.leakcanary:leakcanary-android:1.5" testCompile "junit:junit:4.12" testCompile "org.mockito:mockito-core:1.10.19" Merging the gradle files
  19. Centralising Libraries • In multi-module projects, it is very effective

    in restricting usage to a single version of a given library. • It makes maintaining and upgrading libraries a lot easier.
  20. libraries.gradle ext { supportVersion = "23.4.0" commonLibraries = [ gson

    : "com.google.code.gson:gson:2.6.2", eventbus : "org.greenrobot:eventbus:3.0.0", supportv4 : "com.android.support:support-v4:${supportVersion}", cardView : "com.android.support:cardview-v7:${supportVersion}", design : "com.android.support:design:${supportVersion}" ] testLibraries = [ junit : "junit:junit:4.12", mockitoCore : "org.mockito:mockito-core:1.10.19", dexMakerMockito : "com.google.dexmaker:dexmaker-mockito:1.2" ] }
  21. ext { version = '23.4.0' supportLibraries = [ cardView :

    "cas:cardview-v7:${version}", recyclerView: "cas:recyclerview-v7:${version}", design : "cas:design:${version}" ] } • com.android.support is replaced with cas