“Modularize” refactor tool in Android Studio

Ashish Pathak
android-core
Published in
3 min readAug 16, 2022

Some time back, Android Studio added a new refactor tool called “Modularize” to help developers modularize their Android app. Until recently, I was not able to use that tool. What I thought from the experience of using the other built in refactor tools of IntelliJ was it would be sufficient to:

  1. Invoke the “Modularize” refactor tool from the editor.
  2. Select a package from project explorer and invoke the “Modularize” refactor tool.

But both these actions did nothing and I was not able to use this tool. I tried invoking this tool from all actions menu(Cmd+Shift+A) as well as tried to select “Modularize” option from “Refactor” menu. I also noticed that “Modularize” sub menu item was either hidden or disabled all the time. When I invoked “Modularize” from all actions menu, either nothing happened after or following dialog was presented which did nothing. It said, “Move 0 classes and 0 resources to:”.

I found out that to invoke this tool, we need to select one or more files from the project explorer and then invoke this tool. When we do this, following window is presented which tells me how many classes and resources may be moved to destination module:

On playing around more with this, I found out that the “Modularize” refactor only supports the Java files and not the Kotlin files. So, if you intend to use this tool, you must only select the Java files.

When I invoked this tool, selected the destination module and clicked refactor, it identified the classes recursively that were used by the files I had selected and moved all those classes to selected destination module. It did not move the Kotlin files though. Also, it identified and moved the resources to destination module; however it put those resources in the `build/generated` directory for some reason.

I would have really liked it if:

  1. It supported both Kotlin and Java files as more than 90% code base was in Kotlin.
  2. Instead of moving all files recursively, it would just move selected file and associated resources. And then adjust the package name and the imports appropriately. Then, suggest me interfaces that could let me fix compilation errors for the files that the moved file depended on.
  3. The moved resources were properly moved in the `res` directory inside appropriate resource directory.
  4. The dependencies(both project dependencies and maven dependencies) were automatically identified and added to the `build.gradle` along with any `kapt` dependencies.

While for relatively small code bases, this tool could be useful, above functionality will make it more useful in my opinion.

Meanwhile, remember if you want to use the “Modularize” refactor tool from the Android Studio, you need to select Java files only and then invoke the “Modularize” refactor tool.

Let me know how did it work for you team and what more you wish it could have done to aid in your modularization efforts in the comment section.

--

--