Share
Embed
Gradle on Steroids
Slides for the talk about optimising Gradle builds, presented at GDG BLRDROID'S DEVFEST 2016.
Sumit Das
October 15, 2016
Transcript
58 slides
-
Initialization and Configuration The overhead tax you have to pay
whenever you invoke gradle -
Measuring the overhead Test Subject: Google IO App - 28
libraries - 53149 method references -
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 -
./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 -
./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 -
./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 -
#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 -
...but when is it useful? • Multi Module build •
Decoupled Modules -
Test Subject: really-large-android-project (artificial) Info: 150k method references Total Dex
Files: 3 BUILD SUCCESSFUL Total time: 2 mins 14.147 secs -
productFlavors { dev { minSdkVersion 21 } prod { minSdkVersion
14 } } dependencies { compile 'com.android.support:multidex:1.0.0' } -
Test Subject: really-large-android-project (artificial) Info: 150k method references Total Dex
Files: 3 BUILD SUCCESSFUL Total time: 1 min 40.281 secs -
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()); } } -
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()); } } -
Debug Mixpanel Tracker public class MixpanelAnalyticsTracker extends AnalyticsTracker { public
MixpanelAnalyticsTracker(Context context) { super(context); } @Override public void track(String eventName, JSONObject jsonObject) { } } -
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); } } -
settings.gradle: include ‘:app’, ‘:guava1’, ‘:guava2’ app/build.gradle dependencies { compile project(‘:guava1’)
compile project(‘:guava2’) } Utilizing Pre-Dexing -
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 -
def cmd = 'git rev-list HEAD --count' def gitVersion =
cmd.execute().text.trim().toInteger() android { defaultConfig { versionCode gitVersion } } -
Splitting build.gradle CI Gradle File Developer Gradle File CI Gradle
File Developer Gradle File -
CI Gradle Building all flavours and variants. Dev Gradle Generating
test coverage reports. -
But what happens when you add a dependency to Dev
gradle file and forget to add it in CI gradle file? Build Fails -
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 -
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 -
Build-Type specific configurations: Feature debug staging/release Merging the gradle files
Dexguard Shrinking Resources Crashlytics -
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 -
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 -
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. -
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" ] } -
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