Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do library dependency increase size of APK?

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.

like image 834
Anuj Sharma Avatar asked Dec 17 '15 04:12

Anuj Sharma


2 Answers

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.

like image 65
Karakuri Avatar answered Oct 09 '22 22:10

Karakuri


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

like image 35
Niko Adrianus Yuwono Avatar answered Oct 09 '22 22:10

Niko Adrianus Yuwono