Do library dependency increase size of APK?

亡梦爱人 提交于 2020-01-13 03:13:10

问题


I have multiple libraries in my project like

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

and other libraries. Do they increase app size too much. I have more than 25 libraries in my project. Right now APK size is 11 MB and i have to add more functionalities in that. What could be the reason?

I have some questions, regarding this.

What takes more memory ?

  1. Module Added in project.
  2. File added as JAR file.
  3. Gradle Dependency we add just like compile 'com.android.support:appcompat-v7:22.2.1'.

I have read that by Enabling Proguard , setting minifyEnabled true can reduce app size.

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

How do they work behind the picture ?

Should we avoid to use multiple libraries in project ?

Lots of questions are in my mind for reducing APK size. Any kind of suggestion and help would be appreciable. Thanks in Advance.


回答1:


All three of those methods will increase the size of your APK. The difference between them is where does the source code reside:

  • A module dependency is source code on your local machine. It is compiled into bytecode when you build the app.
  • A JAR file is pre-compiled bytecode. It also is on your local machine, but it is not really source code. The bytecode is simply added to your own when you build.
  • A Gradle dependency is basically the same as using a JAR file, except that Gradle will download the pre-compiled artifact instead of you adding it as a file on your local machine.

Regardless of the above, the dependency contributes its classes to the build and they will be present in the final output (the APK).

Proguard does a few things that can reduce the size of your APK. It can statically analyze all the bytecode and remove classes and methods that are never used. It also can rename classes, fields, and methods to smaller identifiers like "abc", which may shrink the size of the bytecode somewhat.




回答2:


Yes, the dependencies plus the output of the compilation of the project’s own source code, are sent to dex for bytecode conversion and inclusion in the final APK.

With proguard, the classes that is not being used can be systematically removed



来源:https://stackoverflow.com/questions/34326600/do-library-dependency-increase-size-of-apk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!