I am running into difficulties getting the apk below 4mb. after inspecting the generated instant app APK(s) i see that com.google.android.gms.internal is almost 1.4mb. i just cannot find the cause of this large chunk. Must be somekind of dependency.
My base manifest file looks like:
dependencies {
api "com.android.support:design:$rootProject.supportLib"
api "com.android.support:support-fragment:$rootProject.supportLib"
api "com.android.support:appcompat-v7:$rootProject.supportLib"
api "com.android.support:recyclerview-v7:$rootProject.supportLib"
api "com.android.support:cardview-v7:$rootProject.supportLib"
api 'com.android.support.constraint:constraint-layout:1.0.2'
api "com.google.code.gson:gson:$rootProject.gson"
api "com.google.firebase:firebase-core:$rootProject.googleLibs"
api "com.google.firebase:firebase-ads:$rootProject.googleLibs"
api "com.google.firebase:firebase-appindexing:$rootProject.googleLibs"
api "com.google.android.gms:play-services-auth:$rootProject.googleLibs"
api "com.android.support:multidex:$rootProject.multidex"
api "com.github.bumptech.glide:glide:$rootProject.glide"
annotationProcessor "com.github.bumptech.glide:compiler:$rootProject.glide"
api "com.loopj.android:android-async-http:$rootProject.asyncHttp"
api "org.greenrobot:eventbus:$rootProject.greenRobotEventBus"
api "com.vincentbrison.openlibraries.android:dualcache:$rootProject.dualcache"
api('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
transitive = true;
}
I just finished doing the same thing and bringing the Instant Apps size from 13MB to 4MB.
After so much headache and countless hours, I've figured out that size of following packages depends on what dependencies you have included.
com.google.android.gms.internal
android.support.v4.internal
android.support.v7.internal
For example, if you exclude Ads dependency the internal size will be reduced at least 500kb. Same goes for Android support libraries, exclude the CardView and see the support's internal size shrink.
I'll list down all the trouble I went through doing a multi-feature Instant Apps modules of a very large scale app step by step:
Move Dependencies that only gets used in Main App
In your case following 2 items are absolutely useless for Instant App and only get's utilized in Main App:
api "com.google.firebase:firebase-appindexing:$rootProject.googleLibs"
api "com.android.support:multidex:$rootProject.multidex"
Move them to main app, moving Appindexing could be tough if the code is coupled with your views but that you'd have to fix it. In my case, I had to instantiate AppIndexing from a view in Base Module so I did that using Otto (EventBus). Created an event in Base Module, fired it and caught in Main App's AppIndexing helper class.
Exclude google support group explicitly from all packages
It may look absurd but the size speaks for itself, change all Google/Android support dependencies with explicit exclusions:
api("com.android.support:design:$rootProject.supportLib") {
exclude group: 'com.android.support'
}
api("com.android.support:support-fragment:$rootProject.supportLib") {
exclude group: 'com.android.support'
}
api("com.android.support:appcompat-v7:$rootProject.supportLib") {
exclude group: 'com.android.support'
}
api("com.android.support:recyclerview-v7:$rootProject.supportLib") {
exclude group: 'com.android.support'
}
api("com.android.support:cardview-v7:$rootProject.supportLib") {
exclude group: 'com.android.support'
}
api('com.android.support.constraint:constraint-layout:1.0.2') {
exclude group: 'com.android.support'
}
api("com.google.firebase:firebase-core:$rootProject.googleLibs") {
exclude group: 'com.android.support'
}
api("com.google.firebase:firebase-ads:$rootProject.googleLibs") {
exclude group: 'com.android.support'
}
api("com.google.firebase:firebase-appindexing:$rootProject.googleLibs") {
exclude group: 'com.android.support'
}
api("com.google.android.gms:play-services-auth:$rootProject.googleLibs") {
exclude group: 'com.android.support'
}
Enable Proguard for each module
As others have mentioned, enable proguard for each module. About 25% of our original 13MB was reduced by proguard.
Exclude Features that won't be used in Instant Apps
Login:
For us, the Login functionality didn't need to be in Instant App. So I moved everything related to Login/Auth to main app and used Event Bus to fire actions where needed from Base Module. For example Login/Logout actions in navigation drawer (hidden in Instant App but visible in Main app) are present in Base Module. So I fire an Event for each and catch it in Main App Module to show Login Screen or process logout.
This allowed me to exclude com.google.android.gms:play-services-auth
dependency from Base Module which reduced the gms internals size even further.
Ads:
We had to move Ads out of our Base Module to main application solely for the size purpose. We did this using the similar technique (Event Bus) to make a call to render Ads from Base Module and doing actual rendering in Main App through a helper class.
This allowed us to remove com.google.firebase:firebase-core
and com.google.firebase:firebase-ads
P.S: The features exclusion totally depends on your needs/want/compromises. We took these decisions as we wanted all of our revenue generating features to be in Instant App. So as long as we can make money we don't care about Ads or Login.
Hope this helps.
It looks like this is the dependency tree for com.google.firebase:firebase-ads:
\--- com.google.firebase:firebase-ads:11.8.0
+--- com.google.android.gms:play-services-ads:11.8.0
| +--- com.google.android.gms:play-services-ads-lite:11.8.0
play-services-ads is a big library (contributing to a lot of code in "com.google.android.gms.internal"), and you might have success by swapping it out for a smaller library. (Of course, I'd recommend filing an issue to get an officially-supported "lite" version of "firebase-ads".)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With