How to delete an unused string resource for all configurations in Android Studio?

后端 未结 8 985
小鲜肉
小鲜肉 2020-12-09 01:41

I\'ve found an unused string resource, like:

my name is null!

However it is in tens of

相关标签:
8条回答
  • 2020-12-09 02:18

    It is now possible inside Android Studio. After Lint checks you see an option on the right Remove All Unused Resources!

    To Delete a single string resource across all locale files, you can use the "Translation Editor". 1. Right click on the res directory to open the translation editor. 2. Select "Show All Keys" selector, and choose "Filter by Text". Supply the name of the resource that you want to delete. 3. Select the resource, and click on the "-" button

    0 讨论(0)
  • 2020-12-09 02:19

    To identify all unused resources:

    • Open Menu > Analyze > Run Inspection by Name...
    • Select "Unused Resources"
    • Make sure Whole project is checked, then press OK.
    • Look through the list. You can always rerun the Inspection with the Rerun button.

    There is no really easy way in Android Studio (v 1.0) to remove a resource string for all locales. However, you can search and replace in files. Fortunately, the translation files use only a single line in most cases so this works pretty well.

    In Android Studio:

    • Start with no changes pending in your source control (makes it way easier to check and back out if this goes wrong).
    • Open Menu > Edit > Find > Replace in Path...
    • In Text to find: .*name="obsoletestring".*\n
    • In Replace with: (empty)
    • Check Regular expression
    • Press the Find button.
    • Inspect the results. If okay, press the "All files" button.
    • You will probably have to remove the comment in res/values/strings.xml manually.
    • Make sure your project still builds.
    • Take a look at the diff of your project in your source control to ensure the change is really what you intended...
    0 讨论(0)
  • 2020-12-09 02:31
    1. Menu -> Analyze -> Run Inspection by Name -> Unused resources
    2. From the results select all string resources that are unused.

      • Right-click highlighted rows and choose "Suppress with @SuppressLint(Java) or tools:ignore(XML)". This will add the attribute tools:ignore to all strings in all string files.
    3. Menu -> Find -> Replace in Path

      • Text to find: ^.*?tools:ignore="UnusedResources".*?\n
      • Tick regular expression box
      • Use Scope: Custom
        • Open custom scope editor and add pattern: file[app]:src/main/res//strings.xml
      • Find
      • Etc.
    0 讨论(0)
  • 2020-12-09 02:34

    Until IDE support comes along, something along these lines will work:

    find -name strings.xml|xargs -rd\\n  sed -ri '/"string_to_delete"/d'
    
    0 讨论(0)
  • 2020-12-09 02:34

    Beware that the REMOVE UNUSED RESOURCES command cannot recognize a programmatically accessed resource as a used resource (such as getIdentifier(..) etc.). So, if you do access resources that way, it is highly risky to use that command!!

    0 讨论(0)
  • 2020-12-09 02:38

    Unfortunately, You have to do it manually.

    Check this answer to understand what exactly should you do to get rid of them using Eclipse

    If you are using Android Studio find them in the whole application and also remove manually .. Check this answer

    0 讨论(0)
提交回复
热议问题