Android Studio 0.8.11
Hello,
I have just completed a test on Android which was to build an app that takes a live news feed, and display them. However, the instructor was very critical as I put all my classes under one package.
I am just wondering what is the best practice for packaging classes. For my particular test I have the following classes under this package name:
com.viewsys.ncon
My classes were these:
DBHelper <-- database creating and ugprading
DetailActivity <-- activity that add the NconDetailFragment
NconContract <-- properties of the database schema columns, table name
NconDetailFragment <-- detail fragment
NconListFragment <-- list fragment
NconViewPager <-- just the view pager
JsonNewsFeed <-- class that downloads and parses the json format
MainActivity <-- Main activity
NewsFeed <-- class of properties getters/setters for news feed
NewsFeedDB <-- simple array list to store all the object from the sqlite3 DB
SplashActivity <-- activity that add the splashFragment and the NconListFragment
SplashFragment <-- splash fragment
Utilities <-- just some simple utility functions
Many thanks for any suggestions,
The package name of an Android app uniquely identifies your app on the device, in Google Play Store, and in supported third-party Android stores.
If you develop an Android library, you can declare your package visibility needs by adding a <queries> element in your AAR manifest file.
Classes and interfaces in the same package can use each other without prefixing their names with the package name. But to use a class or an interface from another package, you must use its fully qualified name, that is, packageName. anySubpackageName. ClassName .
The package for a class can be obtained using the java. lang. Class. getPackage() method with the help of the class loader of the class.
First you could separate by Model (classes holding your data) and view (everything for the display) and then you could create subpackages for different types of classes.
For Example:
com.viewsys.ncon
Utilities
com.viewsys.ncon.model
NconContract
JsonNewsFeed
NewsFeed
com.viewsys.ncon.model.db
DBHelper
NewsFeedDB
com.viewsys.ncon.view
NconViewPager
com.viewsys.ncon.view.activities
DetailActivity
MainActivity
SplashActivity
com.viewsys.ncon.view.fragments
NconDetailFragment
NconListFragment
SplashFragment
You should try to minimize package dependency cycling. That means one package can depend on an other (or multiple other) package and use their classes but the required package should minimize the dependency to the first package. so the dependeny calls should only go into one direction.
Please take a look in this repository, it presents the best practices for android development, and one of these practices is the java package architecture.
In a nutshell, you can do like this:
com.business.project
├─ network
├─ models
├─ managers
├─ utils
├─ fragments
└─ views
├─ adapters
├─ actionbar
├─ widgets
└─ notifications
Activities and fragments are not classified as views, because they are controllers, but they also handles the view, so put in their own packages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With