Remove all unused classes,methods from Android Studio project

前端 未结 3 1784
遇见更好的自我
遇见更好的自我 2021-01-31 14:48

I have used the lint(Analyze->Inspect Code...) and find out unused methods and resources. All the unused resources removed by Refractor->Remove unused R

3条回答
  •  臣服心动
    2021-01-31 15:37

    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.

提交回复
热议问题