0

Is there a way to delete all the code like supporting classes, xmls, methods, imports etc just by passing the parent class say an activity?

I have an old code and want to delete an activity say "Foo.java". How can I delete all the classes, methods, xmls which are just used by this Foo.java and its supporting viewmodel, fragments, dialogs etc.

I want to delete the whole tree of code with Foo.java as its root node.

Foo.java
 |
 |-- VM 
 |   |- sub classes -- sub sub classes ...
 |   |- sub classes -- sub sub classes ...
 |
 |-- Fragment -- sub
 ...
 ...
 |- Styles -- sub
 |- Res -- sub

Any gradle/lint task or plugin?

Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60
  • You generally don't do this because its very hard to write this and not overreach. Just because a fragment/layout/drawable etc is referenced in your Activity doesn't mean it isn't also referenced elsewhere. Especially when you start including what could be passed in via inheritance, it's nearly impossible to do this without either massively under deleting or over deleting. So its up to your to do it. If you want to support this easily, make the activity and all related code a module, and then you can delete the module. – Gabe Sechan Nov 18 '21 at 22:21
  • Can we do it for all files that do not have any other reference? That will reduce a lot of manual efforts – Abhinav Tyagi Nov 18 '21 at 22:22
  • Finding the references is non-trivial. I mean you can try, but that can be hard. Especially if you do any kind of source generation (like dependency injection) in your program, the references might not exist without building and generating that source as well. And that's assuming you don't have multiple targets with different modules being included, in which case you'd need to build all possible targets. Its something that would be easy for a simple project, but really hard to get right for a complex one. – Gabe Sechan Nov 18 '21 at 22:36
  • I think you're looking for this: https://stackoverflow.com/questions/39931519/how-to-clean-up-unused-resource-files-in-android-studio – dominicoder Nov 19 '21 at 01:38
  • No, not the unused part @dominicoder – Abhinav Tyagi Nov 19 '21 at 15:34
  • Don't know what you mean by "not the unused part". That post answers the question of how to get rid of unused classes and resources. That seems to be your question. – dominicoder Nov 19 '21 at 19:33
  • @dominicoder I know how to check for unused code and resources. I am asking to pass a file name and get all the dependencies and their dependencies listed so that I can remove those that are used only for the input file. – Abhinav Tyagi Nov 19 '21 at 20:32
  • I'm not aware of a tool to do this. But again - to remove "those that are used only for the input file", all you'd need to do is 1) delete "input file" 2) run unused resources lint code check, which should catch the files that were previously "used only for the input file". Good luck. – dominicoder Nov 20 '21 at 07:55

0 Answers0