开发者

Remove all unused resources from an android project

I want to remove all unused layouts, strings, drawables, colors, etc from my Android 开发者_Go百科res directory. Are there any tools that will give me a list of files and I can remove from my repository and elements within specifics files (e.g. unused string entries) that are no longer used?


You can easily search for unused resources from Android Studio. Just press CtrlAltShifti and type "unused resources" (without quotes). That will execute lint. Super easy way to run lint commands (and other stuff from IDE).

OR

In Android Studio Menu > Refactor > Remove Unused Resources...

Select the resources you want to remove. You can exclude resources you want to keep by right-clicking on the resource item.

Use Do Refactor to remove all Resources at once.

Update: use OptionShifti for mac


Since ADT 16 you can use Android Lint. It is really amazing tool.

Android Lint is a new tool for ADT 16 (and Tools 16) which scans Android project sources for potential bugs.

Here are some examples of the types of errors that it looks for:

  • Missing translations (and unused translations)
  • Layout performance problems (all the issues the old layoutopt tool used to find, and more)
  • Unused resources
  • Inconsistent array sizes (when arrays are defined in multiple configurations)
  • Accessibility and internationalization problems (hardcoded strings, missing contentDescription, etc)
  • Icon problems (like missing densities, duplicate icons, wrong sizes, etc)
  • Usability problems (like not specifying an input type on a text field)
  • Manifest errors and many more.

However, it has some issues (don't know if they're already fixed) and if you want to delete hundreds of supposedly unused resources I'd recommend to manually compile project several times during resource removing to be sure that Lint didn't remove something needed.


Since Support for the ADT in Eclipse has ended, we have to use Android Studio.

In Android Studio 2.0+ use Refactor > Remove Unused Resources...

Remove all unused resources from an android project


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.

To enable this add the line shrinkResources true in your gradle file.

   android {
        ...

        buildTypes {
            release {
                minifyEnabled true //Important step
                shrinkResources true
            }
   }
}

Check the official documentation here,

http://tools.android.com/tech-docs/new-build-system/resource-shrinking


Beware if you are using multiple flavours when running lint. Lint may give false unused resources depending on the flavour you have selected.


We open source a tool that removes all the unused resources in your android project based on the lint output. It can be found here: https://github.com/KeepSafe/android-resource-remover


In Android Studio,

Analyze -> Run Inspection by Name

(Shortcut For Mac shift+command+option+i , Ctrl + Shift + A on Windows/Linux).

And type 'unused resources'.

This way you can remove unused resources, variables ,symbols ,localization ,libraries ..etc.

Select the desired inspection from the list, then select inspection scope ->OK

Remove all unused resources from an android project


In Android Studio 2.0 and above in menu select Refactor-->click on Remove Unused Resources...

(or)

shortcut also available

Press Ctlr+Alt+Shift+i one dialog box will apper, then type unused , you will find number of options select and remove unused resources


Attention Android Wear developers: "Remove Unused Resources" will delete the xml file where you declare the capability name (res/values/wear.xml) and the phone won't be able to connect to the watch. I spent hours trying to figure out this bug in my app.


1--> In

Android Studio Menu > Refactor > Remove Unused Resources

As Shown in Image Below:

Remove all unused resources from an android project

**

OR

**

2--> Second Way

-> Ctrl Alt Shift i and type "unused resources" (without quotes) in Windows

-> use ⌘ Option Shift i for mac

or

-> cmd + Alt + Shift for mac


To check string.xml.

It's easy (at least in my version of Eclipse)

In Eclipse for Android (I have version v22.6.2-1085508)

  • Left click on the project name in "Package explorer"
  • Select "Android Tools".
  • Select "Run Lint: Check for common Errors".

Now when you open strings.xml, you will see that unused string are highlighted.

You can fix other potential issues.


Maybe useful Andround Unused Resources is a Java application that will scan your project for unused resources. Unused resources needlessly take up space, increase the build time, and clutter the IDE's autocomplete list.

To use it, ensure your working directory is the root of your Android project, and run:

java -jar AndroidUnusedResources.jar

https://code.google.com/p/android-unused-resources/


When we define shrink resources true that time we can also define which resources we wanna keep and which don't I have added xml file in res/raw folder named keep.xml

before going further generate a single signed build and check in apk analyser tool which will show drawable-xhdpi-v4 has messenger_button_send_round_shadow.png which i want to remove for this test

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
       tools:shrinkMode="strict"
       tools:discard="@drawable/com_facebook_button_icon_blue.png,
       @drawable/com_facebook_button_icon_white.png,
       @drawable/com_facebook_button_like_icon_selected.png,
       @drawable/messenger_button_send_round_shadow.png,
       @drawable/messenger_*"  />

by doing messenger_* all files starting from name messenger in drawable folder will be removed or other way around is i have defines specific file to be removed

so that way you can remove files from library it self you can also remove layouts by @layout/layout name if that drawable has been used by layout and so....


There really excellent answers in here suggesting good tools

But if you are intending to remove png-drawables (or other image files), you should also consider moving all the drawable-xxxx folders out of your project into a temporary folder, then do a rebuild all, and take a look at the build message list which will tell you which ones are missing.

This can be specially useful if you want to get an overview of which resources you are effectively using and maybe replace them with an icon font or svg resources, possibly with the help of the Android Iconics library.


shift double click on Windows then type "unused", you will find an option Remove unused Resources,
also

 android {
        buildTypes {
            release {
                minifyEnabled true
                shrinkResources true
            }
   }
}

when you set these settings on, AS will automatically remove unused resources.


Android Assets Viewer is designed to help visually inspect the graphics packed (as drawables) within your .apk (you can also just upload a zip file of your res folder if you prefer):

http://www.cellebellum.net/AndroidAssetsViewer/

So for removing unused resources, this might work well in tandem with the Lint checks to help make sure everything that should be there is. It also helps you identify a few other potential problems with your drawables: wrong bucket, wrong graphic, etc.


After you run Lint in Android Studio and find all the unused resources, you can click on one of them from the Inspection tab. It provides some detail about the issue and a few options to fix it. One of them is Remove All Unused Resources. Selecting that option deletes all the unused resources.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜