Android Project Structure – Best Practices for Application Development

androidproject-structure

I am starting a new Android application. What is the best structure to use?

I am planning to make it a multi-package design as follows:

  1. Main package, including the Activity

  2. Service and data layer

  3. Entity package, including entity class.

    Any advice ?

Best Answer

First, it depends on what application you are doing.

you should do a textual or schematic description on how a user will work with application. Fix down every possible scenario. Put down examples that will be used later for tests.

Decide what belongs to the functionality and what - to the changeable configuration. Extract functionalities and data entities from scenarios.

From scenarios make decision what your app will be. Is it service, activity, widget , even a content provider or a complex system, including some different components. Test your decision against scenarios.

In the case of the complex system, distribute functionalities and data entities among application components. Make a list of components and what they are (activities or smth else).

Make the list of UI components with description what they do (not HOW yet) These will be widgets and activities or fragments or layouts later.

Make draft layouts for UI components. Make simple passes from one to another. Look at the UI. Return to scenarios and play all of them them with your draft UI. All UI components and classes put into one hierarchy of packages or package.

Make a list of data entities. Decide what will be in what. Plan them as collections or tables in DB or different DBs. Make them as classes, put them into another hierarchy of packages or another package. Here also put DB helpers - classes that talk with DB by SQL.

Make a testing classes(JUNITs or better TestNG) for filling UI and data entities with test data and launching them.

Adapters needn't be public, for they are used in their parent GroupView only. So, no files for adapters usually.

Do not put all globals into special static classes - it is a bad practice. You are mixing so the code and the configuration. Use this very interesting solution. For now, it is the best I know for Android.

Configuration data should be put into resources. If some of them are complex, use XML sources and parser(s). Make readers of resource data into global variables. Not all of them will be static! They could belong to the main Activity instance, for example.

Do not use unconfigurable constants in the code! May be, your name only :-). Every constant becomes non-constant sometimes.

On the other side, if some of you code is not normal java, but scripts - a mix of data and language, then you can and must mix data and code.

Always do so:write something - connect something to a bulk - add test(s) for this new thing - test this new one - test the bulk - repeat. Small steps only!

Edit. You can also use the test driven development - write tests before appropriate code. This way, running tests before the code is ready, you have double testing - thus you check if the tests really react to incorrect code.

Related Topic