Amit Desale
Amit Desale

Reputation: 1291

How to reduce Apk (.apk) size in android studio

When i run my app size of apk will 21 MB. even i enabled proguard. i use android studio to run project. in project files src folder has 8.62 MB size and lib folder size is 400 KB size. so how i reduce size of .apk file. Actually .apk file is 8 mb before some days with same images but after paypal integration my app size will increases to 21 mb. size of build folder is 127 MB.

build.gradle(module app) is:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.oi.food"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions{
        exclude 'AndroidManifest.xml'
    } }

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile files('libs/httpmime-4.1.1.jar')
    compile files('libs/apache-mime4j-0.6.jar')
    compile 'com.squareup.picasso:picasso:2.3.2'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.paypal.sdk:paypal-android-sdk:2.14.1' }

Upvotes: 4

Views: 55219

Answers (10)

Samudra Ganguly
Samudra Ganguly

Reputation: 844

I had implemented Zoom SDK in my app and the apk size was around 487 MB. I had tried many solutions but none worked. The only solution that worked for me was to use android:extractNativeLibs="true" in the application tag in manifest.

Upvotes: 5

Tombstone
Tombstone

Reputation: 270

android {
    // Other settings

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

Support only specific densities

Android supports a very large set of devices, encompassing a variety of screen densities. In Android 4.4 (API level 19) and higher, the framework supports various densities: ldpi, mdpi, tvdpi, hdpi, xhdpi, xxhdpi and xxxhdpi. Although Android supports all these densities, you don't need to export your rasterized assets to each density.

Remove unnecessary generated code

Avoid extracting native libraries

Upvotes: 0

Tejas Mane
Tejas Mane

Reputation: 151

There are a few techniques you can use to reduce the apk size:

1. Proguard: In build.gradle file for app-level, add the following code

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

2. Unnecessary Resources: Many times unnecessary resources are present in the source code. Inpect the source code by Analyze->Inspect Code. After the inspection ends, delete unnecessary resources.

3. Webp images: Webp images have less size than png files and also it retains the original image quality. More to that webp images are natively supported in Android. Right-click the drawable(/mipmap) folder -> Convert to webp. Choose appropriate options which suits your need.

4. Signed apk: The debug apk size is greater than the signed apk. Generate signed apk by going to Build-> Generate Signed Bundle/Apk->APK. Overall apk size is now reduced.

5. Android App Bundle: If you are going to upload the app on Google Play, generate Android App bundle instead of apk.To generate app bundle go to, Build-> Generate Signed Bundle/Apk-> Android App Bundle. It will reduce your app size for sure. For example, my apk size of 10 MB is reduced to just 6-7 MB using Android App Bundle.

Upvotes: 5

Aakib Jabbar
Aakib Jabbar

Reputation: 119

The Best Way to Reduce The Size of Your APK File is reduce the use of the libaries that are used in build.gradle file . these libaries create a lot of redandancy and in turn increase the size of the project .. enter image description here

Upvotes: -2

ZLabZ Programmer
ZLabZ Programmer

Reputation: 21

I think you should build your APK as (APK bundle), Google promotes that for APK size issues. I've used the option for my projects and it did actually decrease the size of my APK.

Upvotes: 1

Aashish Kumar
Aashish Kumar

Reputation: 2879

These 4 things reduce your apk size almost 50-60%

  • Replace proguard-android.txt with proguard-android-optimize.txt
  • Make minifyEnabled true
  • Add shrinkResources true
  • Add resConfigs

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

Details:

  • proguard-android-optimize.txt is the more aggressive progurard configuration than proguard-android.txt.

  • shrinkResources attribute will remove all the resources, those are not used anywhere in the project.

  • resConfigs attribute will remove all the other localized resources while building the application. Here, it strips other than english resources.

Upvotes: 10

Petrus Jonatan
Petrus Jonatan

Reputation: 21

Before click Finish when open new project wizard, uncheck backward compatibility(appcompat). This is can reduce from 1.5 MB to 180 KB.

Upvotes: 2

Amit Desale
Amit Desale

Reputation: 1291

I solve this issue by adding this code in module app build.gradle file.

 android {
        packagingOptions{
            exclude 'AndroidManifest.xml'
            exclude 'lib/arm64-v8a/libcardioDecider.so'
            exclude 'lib/arm64-v8a/libcardioRecognizer.so'
            exclude 'lib/arm64-v8a/libcardioRecognizer_tegra2.so'
            exclude 'lib/arm64-v8a/libopencv_core.so'
            exclude 'lib/arm64-v8a/libopencv_imgproc.so'
            exclude 'lib/armeabi/libcardioDecider.so'
            exclude 'lib/armeabi-v7a/libcardioDecider.so'
            exclude 'lib/armeabi-v7a/libcardioRecognizer.so'
            exclude 'lib/armeabi-v7a/libcardioRecognizer_tegra2.so'
            exclude 'lib/armeabi-v7a/libopencv_core.so'
            exclude 'lib/armeabi-v7a/libopencv_imgproc.so'
            exclude 'lib/mips/libcardioDecider.so'
            exclude 'lib/x86/libcardioDecider.so'
            exclude 'lib/x86/libcardioRecognizer.so'
            exclude 'lib/x86/libcardioRecognizer_tegra2.so'
            exclude 'lib/x86/libopencv_core.so'
            exclude 'lib/x86/libopencv_imgproc.so'
            exclude 'lib/x86_64/libcardioDecider.so'
            exclude 'lib/x86_64/libcardioRecognizer.so'
            exclude 'lib/x86_64/libcardioRecognizer_tegra2.so'
            exclude 'lib/x86_64/libopencv_core.so'
            exclude 'lib/x86_64/libopencv_imgproc.so'
        }
    }

           dependencies {
            compile fileTree(include: ['*.jar'], dir: 'libs')
            compile 'com.paypal.sdk:paypal-android-sdk:2.14.1'
        }

Upvotes: 11

Natarajan Raman
Natarajan Raman

Reputation: 606

You can easily reduce the size of the app by reducing the image size. Please use https://tinypng.com/ to reduce your image size. This will ensure your app size is also reduced. The image quality will remain the same.

Also in case of libraries, please be sure to include ONLY those components that you really need. Also remove any unwanted and unused imports.

Upvotes: 4

Atiq
Atiq

Reputation: 14835

try

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

shrinkResources true will not include the images from resources which your using in the final apk

hope this helps

Upvotes: 4

Related Questions