Mobile Development 14 min read

How to Shrink Android APK Size: Proven Strategies and Tools

This article details a comprehensive, step‑by‑step approach to reducing Android APK size—including library pruning, resource compression, class file optimization, and automated monitoring—to keep app packages lightweight and prevent size regression over successive releases.

Dada Group Technology
Dada Group Technology
Dada Group Technology
How to Shrink Android APK Size: Proven Strategies and Tools

Background

Frequent monthly releases of the home‑service Android app caused rapid growth of the APK size, eventually exceeding Google Play's 100 MB upload limit. Large size reduces user install willingness and harms acquisition and retention, making size reduction essential.

Current Situation

Analysis of version 8.11.0 shows that the lib, res, assets directories and the classes.dex file dominate the package, accounting for the majority of the volume.

Slimming Strategies

Library Slimming

The lib directory contains native binaries for several CPU ABIs (armeabi, armeabi‑v7a, arm64‑v8a, x86, x86_64). Market‑dominant devices only require armeabi and arm64‑v8a. Unused .so files are removed by periodic scripts, and the build is configured to generate separate 32‑bit and 64‑bit APKs. ndk { abiFilters "armeabi", "arm64-v8a" } After splitting, the size difference can exceed 20 MB. Distribution methods:

App stores: upload both APKs; the store delivers the matching one to each device.

In‑app update: detect the device ABI at runtime and download the appropriate package.

Resource Slimming

Image resources

Images account for >25 % of the APK. The following measures are applied:

Keep only 1× or 2× density assets; discard 3× sets.

Prefer JPG (no alpha), WebP, or 9‑patch/vector formats; replace solid‑color images with programmatic drawing.

Automated image compression

TinyPNG is used to convert images to 8‑bit WebP/JPG. A custom Gradle script batch‑compresses all assets, ensuring consistent optimisation.

TinyPngInfo{
    api_key=["xxxxxxx","xxxxxxx","xxxxxxx"];
    checkModuleName=['xx'];
    excludeImage=['xx.png'];
    checkResMinSize=0;
    compressImage=[];
    checkResDir=["src${File.separator}main${File.separator}assets",
                 "src${File.separator}main${File.separator}res"];
}

Resource obfuscation (AndResGuard)

AndResGuard shortens resource IDs (e.g., res/drawable/hello.pngr/s/a.png) and outputs a mapping file, reducing APK size and slightly increasing reverse‑engineering difficulty.

APK compression format optimisation

Some file types are stored uncompressed in the default ZIP archive. Switching to 7‑zip yields roughly a 5 % additional reduction.

static const char* kNoCompressExt[] = {
    ".jpg", ".jpeg", ".png", ".gif",
    ".wav", ".mp2", ".mp3", ".ogg", ".aac",
    ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
    ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
    ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
    ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"
};

Applying 7‑zip to a signed 32‑bit APK reduced size from 50.4 MB to 48.2 MB (≈5 %).

Duplicate and unused resource removal (ApkChecker)

ApkChecker detects duplicate files by MD5 comparison and unused resources by parsing R.txt and analysing reference graphs. Using this tool shaved an additional 1.5 MB from the package.

Class File Optimisation

Classes.dex occupies ~30 % of the APK. Strategies include:

Adopt good coding habits and avoid heavyweight third‑party libraries.

Enable ProGuard/R8 for code shrinking, obfuscation, and resource shrinking.

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

Key precautions: do not obfuscate JNI methods, reflection‑used classes, Android components, custom Views, WebView‑JS bridges, annotation classes, data‑bean classes, enum value methods, Parcelable/Serializable implementations, and follow any third‑party SDK obfuscation guidelines.

Preventing Size Rebound

An incremental‑comparison tool monitors file‑level, module‑level, and whole‑APK size changes across versions, presenting results in five dimensions (overall, added, deleted, increased, decreased) for quick regression detection.

allResCheckInfo {
    remoteVersion = '1.1.0'   // version to compare
    versionName   = '1.0.0'   // local version
    warnSize      = 5          // single‑file warning threshold (KB)
    outPutsAddSize = 100       // max incremental size per release (KB)
    outPutsSize    = 50000     // max total size (KB)
    sortAllFileSize = true
    check_projectNames = ['app','jni']
    outPutsFileName = ""
    projectName = ''
    check_userName = '****'
    check_passWord = '****'
    serviceUrl = "http://****"
    isOpenPushRemote = true
}

Results and Future Plans

From version 8.9.0 (48.43 MB) through 24 iterations to 8.21.5 (48 MB), the APK size remained within a controlled range, demonstrating the effectiveness of the combined measures.

Future work includes:

Version 2.0 of the one‑click compression tool to handle third‑party SDK resources and provide fully automated execution.

Dynamic downloading of large Lottie animations and other assets to keep the APK lean.

Inlining R‑file IDs into code and removing the generated resources.arsc file for further size reduction.

References:

AndResGuard – https://github.com/shwenzhang/AndResGuard

Matrix‑Android‑ApkChecker – https://github.com/Tencent/matrix/wiki/Matrix-Android-ApkChecker

Android developer shrink‑resources guide – https://developer.android.google.cn/studio/build/shrink-code#shrink-resources

TinyPNG API – https://tinypng.com/developers

R‑file slimming technique – https://mp.weixin.qq.com/s/JgNugWIyor23J-nrKMC6dg

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Mobile DevelopmentAndroidResource OptimizationAPKProGuardApp Size
Dada Group Technology
Written by

Dada Group Technology

Sharing insights and experiences from Dada Group's R&D department on product refinement and technology advancement, connecting with fellow geeks to exchange ideas and grow together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.