【问题标题】:How to reduce Android Apk size如何减小 Android Apk 大小
【发布时间】:2016-10-03 07:41:05
【问题描述】:

我正在开发包含以下依赖项的应用程序

模块级

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.google.android.gms:play-services:9.0.0' **// for GCM**
compile 'com.android.support:cardview-v7:23.3.0'
compile 'com.squareup.picasso:picasso:2.5.2' **//Rounding and displaying image from URL**
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.github.lecho:hellocharts-library:1.5.8@aar' **// For bar graph** 
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
    transitive = true;
}
compile 'com.google.firebase:firebase-core:9.0.0' **// Analytics**

}

项目级别

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.2'
    classpath 'com.google.gms:google-services:3.0.0'
    classpath 'io.realm:realm-gradle-plugin:1.0.1' // Database
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

我的

/Assets /150kb 使用字体真棒文件。

/java - 644 KB

/res - 740 KB(使用高分辨率图像 1920*1280 大小 220kb 用 tinypng 压缩)

已应用 ProGuard。 删除未使用的文件和代码。

问题

我的应用程序大小仍然是 7.03MB

如何减小应用程序大小?我不知道为什么这是 7.03MB

是否有任何计算可以给出apk中依赖项使用的大小?

就像 google play 服务在 apk 中占用了大约 200kb。

【问题讨论】:

标签: android apk


【解决方案1】:

无需编译完整的 google services 包,您只需调用编译所需的包即可。下面有一个链接,请检查相同 https://developers.google.com/android/guides/setup

在使用 GCM 之前,我建议您使用 Firebase Cloud Messaging,因为 google 正在将其基础从 gcm 转移到 firebase。

Firebase 云消息传递链接:-

https://firebase.google.com/docs/cloud-messaging/android/client

对你有帮助的要点:-

您可以在设计中使用 FontAwesome 图标而不是图像,这对应用程序大小有很大影响。为每个结果在 dimen.xml 中为每个布局提供特定大小。

如果您使用的是图像,那么您必须确保您没有为不同的屏幕插入相同的图像。尽量使用android的内置图标

【讨论】:

  • @RRR_1173 我已经编辑了我的答案,请检查它是否可以帮助你
【解决方案2】:

在 6.5 之前的 Google Play 服务版本中,您必须编译 整个 API 包到您的应用程序中。在某些情况下,这样做会使 在您的应用程序中保持方法的数量变得更加困难(包括 65,536 下的框架 API、库方法和您自己的代码) 限制。

从 6.5 版开始,您可以选择性地编译 Google Play 服务 API 到您的应用程序中。例如,仅包含 Google Fit 和 Android Wear API,在您的 build.gradle 文件:

替换

compile 'com.google.android.gms:play-services:9.6.1' //or the version you are using

您需要特定的 api:

com.google.android.gms:play-services-gcm:9.6.1
// and other if you need

特定 GMS api 的依赖列表可以在 here 找到。

【讨论】:

  • 这将有助于减少 - 我使用了 shrinkResources 的 0.5 mb 它将减少 -0.3 Mb 仍然应用程序大小为 6.40 MB
【解决方案3】:

有两个有用的技巧你可能会错过

  1. 与 proguard 一起优化 Dex 和资源

    这会导致代码更小,因此只能在发布版本中使用。否则会造成调试困难。可以这样实现:

    安卓{

        buildTypes {
    
            release {
                minifyEnabled true
                shrinkResources true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    

您还需要专注于从依赖的 SDK 中删除未使用的字符串。

  1. 使用 ResConfigs 删除未使用的配置

支持/Google play 服务等许多库都带有翻译成太多语言的字符串资源。您可能不打算支持所有这些。使用以下方式,您可以控制大小:

android {

    defaultConfig {
    ...
        resConfigs "en"
    }
}

这将删除所有非英语资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多