Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unused resources using Android Studio?

I want to remove unused resources from my project to reduce the app size. Is there any way to do it by using Android Studio IDE efficiently?

like image 382
Stella Avatar asked Apr 13 '16 07:04

Stella


People also ask

How do I find unused strings in Android?

Check File mask(s) checkbox and put strings. xml in the text field. Then you can define Custom scope , choosing Production classes -> app -> values to find unused strings, colors ...

Does ProGuard remove unused resources?

It will help you not only to remove unused resources, but also to find potential bugs. The ProGuard tool shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes, fields, and methods with semantically obscure names.


2 Answers

The Gradle build system for Android supports Resource Shrinking : the automatic removal of resources that are unused, at build time, in the packaged app. In addition to removing resources in your project that are not actually needed at runtime, this also removes resources from libraries you are depending on if they are not actually needed by your application.

For example, your application is using Google Play Services to for example access Google Drive functionality, and you are not currently using Google Sign In, then this would remove the various drawable assets for the Sign In buttons.

Note: Resource Shrinking only works in conjunction with code shrinking (such as ProGuard). That's how it can remove unused resources from libraries; normally, all resources in a library are used, and it is only when we remove unused code that it becomes apparent which resources are referenced from the remaining code.

To enable resource shrinking, update your build type as follows:

android {
    ...

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

And google recently launched Android Studio 2.0 officially, Now they are giving an option in the IDE itself.

Right click on app --> Refactor --> Remove Unused Resources

It will prompt

enter image description here

Check the box prior confirm action so that you can get rid of unused @id declarations too.

  • In terms of APK optimization consider Selecting a Format fact as well.
  • Use WebP Images provide better compression than either JPEG or PNG. Lossy WebP images are supported in Android 4.0 (API level 14) and higher, and lossless and transparent WebP images are supported in Android 4.3 (API level 18) and higher.
like image 158
Anoop M Maddasseri Avatar answered Oct 23 '22 02:10

Anoop M Maddasseri


In android studio. You can use Android Lint. It will show " Strings, Resource, import.." not use

Analyze -> Inspect Code -> Whole Project -> OK
like image 38
Nguyễn Trung Hiếu Avatar answered Oct 23 '22 02:10

Nguyễn Trung Hiếu