Remove all unused classes,methods from Android Studio project

杀马特。学长 韩版系。学妹 提交于 2020-05-24 14:32:10

问题


I have used the lint(Analyze->Inspect Code...) and find out unused methods and resources. All the unused resources removed by Refractor->Remove unused Resources but not found any option like this to remove java classes and methods. Is there any feature in the android studio or any Plugin which can remove all the java classes, the methods which are not used in code to save manual refracting?


回答1:


This can be achieved by using the built-in inspection Java | Declaration redundancy | Unused declaration.

To run it on whole project go to Analyze -> Run inspection by name..., type Unused declaration and select desired scope. Then carefully check output and mark some classes as entry points if needed.

Now you can select Unused declaration node in list and perform Safe delete action on all unused declarations at once.

For Kotlin there is similar inspection Kotlin | Redundant constructs | Unused symbol.




回答2:


Android ships with ProGuard and can do what you want. If you are using Gradle as build system then you can add the following lines in your build.gradle file:

android {

    // ... other configurations

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

Your proguard-rules-debug.pro file only needs to contain one line

-dontobfuscate

With this additions your release build will get shrunk and obfuscated, your debug build, however, will get only shrunk, i.e, unnecessary code is removed. Note, that ProGuard operates on the build and not the source code.

The ProGuard FAQ has some more information of what it can do.



来源:https://stackoverflow.com/questions/37066506/remove-all-unused-classes-methods-from-android-studio-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!