AnuthaDev
AnuthaDev

Reputation: 33

How to remove unused items from Android app's all file?

I built a simple flashlight app with just one switch and it worked fine. I decided to look what was in the apk generated, so I decompiled my app and after looking in each folder I found very unnecessary files in the res/ folder like anim , values-sr, values-uk, values-ur, color, drawable-v21 and many more(around 90) which contained a 2 kB XML each. I don't use all this in my app as I have only one switch and all this unnecessary stuff increases my apk size and due to this Android Studio also generates lots of unnecessary entries in the R.java file. Can anyone help me to prevent all this from being generated in Android Studio, so that my apk becomes smaller in size.

Edit : Here is the gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.camera2"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        resConfigs "en"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    sourceSets{
        res {
            resources{
                exclude {'**/drawable-ldrtl-hdpi-v17/*'}
                exclude 'drawable-ldrtl-hdpi-v17'
            }
        }
    }

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

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:25.2.0'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
}

Upvotes: 2

Views: 1355

Answers (2)

Abhi
Abhi

Reputation: 3511

The files generated after de-compilation are by default for apps as those files are required by the app to run and display. If the Refactor method does not work, you can go in the AndroidStudioProjects folder, search for your app and manually delete files which are not required. You can add the following code to the build.gradle

defaultConfig {
// ...
resConfigs "en"
resConfigs "nodpi", "hdpi", "xhdpi"
}

You can also use shrinkResources true to reduce app size.

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

Upvotes: 1

배준모
배준모

Reputation: 601

In AndroidStudio

Refactor -> Remove Unused Resoursces -> Refactor

Upvotes: 0

Related Questions