Android Project: How best to organize the files [closed]

。_饼干妹妹 提交于 2019-11-28 04:10:22

Good Explain by @Eric Oestrich:

Writing a medium to large Android app requires having code structure. In creating our latest Android development project, I came across a structure that has helped me out.

Java Code :

  • com.example

    • activities

      Contains all the activities. Classes are all named with Activity at the end. That way, you can immediately know what it is when reading Java code that doesn't have its full package name.

    • adapters

    Contains all the adapters.

    • authenticator

    Contains any class related to signing a user in. I create a local account and having all related classes together is very handy.

    • data

    Contains all classes related to data management such as ContentProvider and SQLiteHelper.

    • data.migrations

    Contains all of my SQLite migrations.

    • fragments

    Contains all fragments.

    • helpers

    Contains helper classes. A helper class is a place to put code that is used in more than one place. I have a DateHelper for instance. Most of the methods are static.

    • interfaces

    Contains all interfaces.

    • models

    Contains all local models. When syncing from an HTTP API I parse the JSON into these Java objects using Jackson. I also pull Cursor rows into these models as well.

    • preferences

    Contains all classes for custom preferences. When creating the preferences I required a custom PreferenceDialog as well as a custom PreferenceCategory. They live here.

    • sync

    Contains all classes related to syncing. I use a SyncAdapter to pull data from an HTTP API. In addition to the SyncAdapter a SyncService is required, so I created a package.

Layouts :

  • Activity Layout name start with activity_
  • Adapter Layout row name start with row_
  • Fragment Layout name start with fragment_

As far as i know, there is no convention, but here is an example of how you can put your files in packages :

  • mainPackage
    • LauncherFragment
    • LauncherActivity
    • MyApplication
  • uiPackage
    • DetailsFragment
    • DetailsActivity
    • OtherTabletFragment
  • viewPackage
    • custom views
  • databasePackage
    • MainContentProvider
    • MainDBHelper
    • SecondContentProvider
    • SecondDBHelper
  • dataPackage
    • CustomAdapter
  • utilsPackage
    • xmlUtils
    • textUtils

And many others. You can search for android projects on GitHub and have a look.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!