Reputation: 8520
I have a legacy app with 168 modules in it and I would like to break it up into several java packages from:
com.mycompany.ediary
to
com.mycompany.ediary.util
com.mycompany.ediary.data
com.mycompany.ediary.services
...etc.
This would be to better organize the code. I read where it is ideal to have less than 30 files per package and as you can see, this is way over this. What is the best way to do this? I am currently running Android Studio 1.5
Upvotes: 0
Views: 903
Reputation: 8520
It wasn't as easy as I thought in Android Studio, yet easier than I thought at the same time.
Adding new packages is basically adding new directories to your project structure, by adding new "packages" and letting Android Studio do the work of refactoring it all in your code. I did have some collisions with the manifest-merger-release-report.txt file, but after manually fixing those and updating my manifest as well, I was able to break up the files into a more organized format. I didn't have to touch the gradle files at all.
Right click on the java folder in your app, select New -> package - then enter in the full package name: com.mycompany.ediary.utils. Then cut and paste the files you want to add to the new directory/package, when prompted for reformatting, hit "reformat", then allow Android studio to do its magic.
Upvotes: 1
Reputation: 29713
As @Antoniossss says "Just do it!".
Following a slight explanation...
In the older Eclipse days there was a link between the Java packages, and the Android manifest package (now referred to as the "application ID") in the app. This was enforced by Eclipse (almost) requiring that you put your code into a Java package that was named the same as the application ID.
Changing the root (Java) package of your code in Eclipse got a bit tricky, and generally you would want to do that by using the Android Tools menu. This made sure that the R
class was correctly imported after the change.
Android Studio makes it much easier to separate the 2 concepts. Plus the terminology makes it easier now, since the accepted term "Android manifest package" is now the "application ID".
This gets set in your gradle build file:
applicationId "mobi.glowworm.demo"
How you organise your Java code does not depend on this value at all anymore. You can use any package structure you want to.
So, yes, "Just do it!"
Upvotes: 1