Alexander Farber
Alexander Farber

Reputation: 22978

Multiple APKs for screen densities still include all resources and APK file size stays same

My game app includes 4 large PNG images representing different game boards:

android studio screenshot

In an effort to reduce the APK file size I have followed the Build Multiple APKs document and added the following section to the app/build.gradle file:

splits {
    density {
        enable true
        reset()
        include "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"
    }
}

After that I select Build -> Generate Signed APK... in Android Studio menu.

Surprisingly the APK files still have the same file size:

APK files

And Analyze APK... shows that resources for all screen densities are still included:

analyze screenshot

What have I missed here please, how can I exclude the unneeded resources?

My entire app/build.gradle is below:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        resConfigs "en"
        applicationId "com.example"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    splits {
        density {
            enable true
            reset()
            include "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "com.android.support:appcompat-v7:$supportLibVersion"
    compile "com.android.support:cardview-v7:$supportLibVersion"
    compile "com.android.support:customtabs:$supportLibVersion"
    compile "com.android.support:design:$supportLibVersion"
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile "com.google.android.gms:play-services-auth:$firebaseLibVersion"
    compile "com.google.firebase:firebase-messaging:$firebaseLibVersion"
    compile 'com.android.billingclient:billing:1.0'
    compile 'com.github.bumptech.glide:glide:4.3.1'
    compile 'com.neovisionaries:nv-websocket-client:2.3'
    compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
    compile 'com.facebook.android:facebook-android-sdk:4.28.0'
    testCompile 'junit:junit:4.12'
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

apply plugin: 'com.google.gms.google-services'

Upvotes: 0

Views: 734

Answers (1)

Alexander Farber
Alexander Farber

Reputation: 22978

I've got the answer at /r/androiddev - APK splits only work for drawables, but not for mipmap dirs.

After moving my PNG files, the APK file sizes were reduced:

APK analyze

To move all my resources in a batch I have used the following shell-command in Terminal:

for f in ./app/src/main/res/mipmap-{mdpi,hdpi,xhdpi,xxhdpi,xxxhdpi}/game_board_*.png; do echo git mv -v "$f" "${f/mipmap/drawable}"; done

(remove "echo" to run the actual command).

Upvotes: 1

Related Questions