Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for putting classes under package names in Android

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,

like image 631
ant2009 Avatar asked Oct 07 '14 17:10

ant2009


People also ask

What is the purpose of package name in Android?

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.

What is the right way to declare package in Android is?

If you develop an Android library, you can declare your package visibility needs by adding a <queries> element in your AAR manifest file.

How do I use classes in the same package?

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 .

What is the package name of class?

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.


2 Answers

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.

like image 111
Simulant Avatar answered Oct 23 '22 08:10

Simulant


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.

like image 28
Filipe Bezerra de Sousa Avatar answered Oct 23 '22 10:10

Filipe Bezerra de Sousa