库依赖增加APK的大小?

时间:2015-12-17 04:25:45

标签: android android-gradle apk android-library android-proguard

我的项目中有多个库,如

dependencies {
    compile files('libs/universalloaderlibrary.jar')
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile 'com.android.support:cardview-v7:22.2.1'
    compile 'de.hdodenhof:circleimageview:1.3.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    //noinspection GradleCompatible
    compile 'com.google.android.gms:play-services-gcm:7.3.0'
    compile 'com.github.castorflex.smoothprogressbar:library:1.1.0'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.loopj.android:android-async-http:1.4.8'
    compile 'com.android.support:multidex:1.0.1'
}

和其他图书馆。 它们是否会增加应用程序的大小。我的项目中有超过25个库。现在APK大小是11 MB,我必须添加更多的功能。 可能是什么原因?

我对此有一些疑问。

  

什么需要更多记忆?

  1. 项目中添加的模块。
  2. 文件已添加为JAR文件。
  3. Gradle Dependency我们添加就像compile 'com.android.support:appcompat-v7:22.2.1'
  4. 我已阅读通过启用Proguard,设置minifyEnabled为true可以减少应用程序大小。

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

    他们如何在图片后面工作?

    我们应该避免在项目中使用多个库吗?

    我想到了很多减少APK尺寸的问题。 任何建议和帮助都会很明显。 在此先感谢。

2 个答案:

答案 0 :(得分:8)

所有这三种方法都会增加APK的大小。它们之间的区别在于源代码所在的位置:

  • 模块依赖项是本地计算机上的源代码。在构建应用程序时,它会被编译为字节码。
  • JAR文件是预编译的字节码。它也在你的本地机器上,但它不是真正的源代码。在构建时,字节码只是添加到您自己的字节码中。
  • Gradle依赖项基本上与使用JAR文件相同,不同之处在于Gradle将下载预编译的工件,而不是将其作为文件添加到本地计算机上。

无论如何,依赖关系都会将其类贡献给构建,并且它们将出现在最终输出(APK)中。

Proguard做了一些可以减小APK大小的东西。它可以静态分析所有字节码,并删除从未使用过的类和方法。它还可以将类,字段和方法重命名为较小的标识符,如“abc”,这可能会稍微缩小字节码的大小。

答案 1 :(得分:2)

是的,依赖项加上项目自己的源代码编译的输出,被发送到dex进行字节码转换并包含在最终的APK中。

使用proguard,可以系统地删除未使用的类