简体   繁体   中英

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

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

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.

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.

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.

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

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

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.

Use below Steps

  1. Unused Resource
  2. Lint
  3. Proguard
  4. Utilization of ShrinkResources
  5. Limit the use of libraries from other countries
  6. Use your code again
  7. Reduce the size of APK with R8
  8. Reduce the library size
  9. DebugImplementation

Read More On Codeplayon Web Site

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM