本文介绍了即时应用程序遇到4mb以下的困难.如何减少apk大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使apk低于4mb.检查生成的即时应用APK之后我看到com.google.android.gms.internal几乎是1.4mb.我只是无法找到这么大块的原因.必须是某种依赖.

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;
}

推荐答案

我刚刚完成了同样的事情,并将Instant Apps的大小从13MB增加到了4MB.

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.

  1. com.google.android.gms.internal
  2. android.support.v4.internal
  3. android.support.v7.internal
  1. com.google.android.gms.internal
  2. android.support.v4.internal
  3. android.support.v7.internal

例如,如果排除广告依赖关系,内部大小将减少至少500kb. Android支持库也是如此,不包括CardView,而是看到支持的内部尺寸缩小.

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.

我将逐步列出我在大型应用程序的多功能Instant Apps模块中遇到的所有麻烦:

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:

仅在主应用程序中使用的移动依赖项

在您的情况下,以下两个项目对于Instant App绝对没有用,只能在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"

将它们移动到主应用程序,如果代码与您的视图耦合在一起,但必须修复它,则移动Appindexing可能会很困难.就我而言,我必须从基本模块中的视图实例化AppIndexing,因此我使用Otto(EventBus)进行了此操作.在基本模块中创建了一个事件,将其触发并捕获到Main App的AppIndexing帮助器类中.

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.

从所有程序包中明确排除Google支持组

它看起来很荒谬,但它的大小可以说明问题,请使用明确的排除项更改所有Google/Android支持的依赖项:

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'
}

为每个模块启用Proguard

正如其他人提到的那样,启用proguard每个模块. proguard减少了我们原来的13MB的大约25%.

As others have mentioned, enable proguard for each module. About 25% of our original 13MB was reduced by proguard.

排除即时应用中不会使用的功能

登录:

对于我们来说,Instant App不需要登录功能.因此,我将与登录/身份验证相关的所有内容移到了主应用程序,并使用事件总线在基本模块中需要的地方触发了操作.例如,基本模块中包含导航抽屉中的登录/注销"操作(在Instant App中隐藏但在Main应用程序中可见).因此,我为每个事件触发一个事件,并在主应用程序模块"中捕获该事件以显示登录屏幕"或进程注销.

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.

这使我从基本模块中排除了com.google.android.gms:play-services-auth依赖关系,这进一步降低了gms内部结构的大小.

This allowed me to exclude com.google.android.gms:play-services-auth dependency from Base Module which reduced the gms internals size even further.

广告:

我们仅出于尺寸目的将广告从基本模块移至主应用程序.我们使用类似的技术(事件总线)进行了此操作,以调用从基本模块渲染广告,然后通过帮助程序类在Main App中进行实际渲染.

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.

这使我们可以删除com.google.firebase:firebase-corecom.google.firebase:firebase-ads

P.S:排除的功能完全取决于您的需求/想要/妥协.我们做出这些决定是因为我们希望我们所有的创收功能都可以在Instant App中使用.因此,只要我们能赚钱,我们就不在乎广告或登录.

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.

希望这会有所帮助.

这篇关于即时应用程序遇到4mb以下的困难.如何减少apk大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 12:51