Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping java classes in packages convention in Android

Currently I have Android Studio project with 13 classes in main application package. I decided that it's time to group classes in separate folders (java packages?) like Activities, Fragments, Models, Utils, etc. How to do it correctly following some kind of convention (if there is any) especially for Android Studio project?

What I should select here when I want to create new folder for class group? "Package"? How to name it (following convention) after creation?

enter image description here

like image 465
user2999943 Avatar asked Feb 12 '26 10:02

user2999943


1 Answers

Don't know if there are some "official" convention.

My current package structure I use:
"root" (which is often equals to appId) package for single application class. If app consists from one activity, one services, then they can be put here as well.

Then, "root".component_name package if you app consist of a lot of separated heavy components (usually it doesn't) which hold own activities, models and etc.

"root" package or each component's package contains:

  • activity for activities and fragments
  • app. Also, sometimes, I create app package which can contain base classes for activities, fragments, services and other android app components.
  • view for views and adapters
  • model contains business logic. Contains interfaces, entities, DTOs.
  • model.impl: from time to time, I use it. It contains model implementation(s)(package visibility)
  • helper or util.
    • helper contains domain specific things. As side note: usually I try to avoid using them by encapsulating of helper logic right into domain data type.
    • Package util usually goes to "root" package. app.

Last time I started to use async or model.async packages for classes which describe asynchronous computation, background jobs etc.


I'm not always happy with such structure. So, I look to how classes are grouped inside android api. Also I like to discover structure of my favorite libraries and frameworks.


Example:

org.android.exampleapp.app

          BaseActivity.class   // provide access to MyService
          BaseFragment.class  

org.android.exampleapp.activity

          LaunherActivity.class  // extend BaseActivity and BaseFragment
          MainActivity.class
          MyDetailsFragment.class
          MyListFragment.class

org.android.exampleapp.model

          SuperItem.class
          MyService.class

org.android.exampleapp.model.impl

          MyServiceOfflineImpl.class
          MyServiceOnlineImpl.class

org.android.exampleapp.async

          GetSuperItemsTask.class
like image 182
aeracode Avatar answered Feb 14 '26 00:02

aeracode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!