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?
-
Possible duplicate of [How can I find all unused methods of my project in the Android Studio IDEA?](http://stackoverflow.com/questions/33674592/how-can-i-find-all-unused-methods-of-my-project-in-the-android-studio-idea) – Guillaume Barré May 06 '16 at 07:21
-
@Nirekin My question is to "Remove Unused classes and methods" not "find unused methods" – bilal May 06 '16 at 07:27
-
I believe it is not automated. you have to do it by hands – Eugen Martynov May 06 '16 at 08:06
3 Answers
This can be achieved by using the built-in inspection Java | Declaration redundancy | Unused declaration
.
To run it on whole project go to Code -> 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
.
-
Hi, I can not see Kotlin | Redundant constructs | Unused symbol option. Could you help – Mr Mike Nov 14 '18 at 07:53
-
7@MrMike inspections in `Analyze -> Run inspection by name...` can't be found by full names, you can try just `Unused symbol`. https://i.stack.imgur.com/lwTwx.png – KursoR Nov 14 '18 at 14:23
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.

- 239,200
- 50
- 490
- 574
-
11I believe topic starter is bothered with Android Studio findings and not about cleaning apk from unused methods – Eugen Martynov May 06 '16 at 08:07
-
1@EugenMartynov Maybe, but it is not clearly stated in the question. Maybe OP can shed some light on it. – May 06 '16 at 08:12
-
2You are telling the solution for removing unused methods at runtime. Need to remove the unused methods while developing. – Madhan Feb 28 '17 at 10:00
Step 1
Generate usage.txt and mapping.txt with Proguard or R8
Add
-printusage
to your proguard.pro file Run./gradlew app:minifyReleaseWithProguard
or./gradlew app:minifyReleaseWithR8
Step 2
Find class name records that is in usage.txt but not in mapping.txt, those are the unused classes that are removed by Proguard/ R8 It's not hard to write such algorithm but you can consider using HashTable or Binary Tree.
I've elaborated more here

- 12,476
- 16
- 84
- 127