Android项目包结构 [英] Android project package structure

查看:225
本文介绍了Android项目包结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如何为Android应用程序创建灵活的封装结构,使得它很容易扩展和管理。我的第一个想法是把每个应用程序组件在不同的包装,如:

I am wondering, how to create flexible package structure for an Android application, such that it'll be easy to extend and manage. My first idea is to put each application component in separate package, such as:

spk.myapp.main。(在主要活动中使用的所有类)
spk.myapp.processor。(由处理器供应商使用的所有类)

spk.myapp.main.(all classes used in Main activity) spk.myapp.processor.(all classes used by Processor provider)

...等等。但是,我不喜欢的方面是,类和包命名约定可能很快就与其他完全合格的名称,如供应商当局不一致(在这种情况下,我宁愿命名这些 spk.myapp.processor ​​ spk.myapp.processor.processor ​​的作为类包路径建议)。

...and so on. However, the aspect I don't like is, that the class and package naming convention may quickly became inconsistent with other fully qualified names, such as provider authorities (in this case I would rather name these spk.myapp.processor than spk.myapp.processor.processor as the class package path would suggest).

我做了一些研究,但大多数页面说明最初的项目目录结构,而不是提出一个更大的项目。

I've done some research, but most pages explain the initial project directory structure, rather than suggest one for bigger projects.

我的问题可能听起来很傻,但我喜欢在我的项目从一开始,这样的进一步管理和扩大它们不涉及不必要的重构或清洁工作秩序。另外,我没有在Java中很多经验,我想从一开始学习的好习惯。

My problem might sound silly, but I like to have order in my projects from the beginning, such that further managing and expanding them doesn't involve unnecessary refactorings or cleanups. Also, I do not have much experience in Java and I wish to learn good habits from the beginning.

做一件有Android的项目,一个良好的和可靠的项目包结构和命名约定?

Does one have a good and reliable project package structure and naming conventions for Android projects?

推荐答案

维基百科有一些有用的说明在Java包。包是有两个原因主要是有用的:

Wikipedia has useful notes on Java packages. Packages are mainly useful for two reasons:


  1. 系统软件包提供了它所包含的类型唯一的命名空间。

  2. 在同一个包
  3. 类可以访问对方的包访问权限的成员。

第一点意味着你可以用逻辑功能组项目。活动可以驻留的活动包下,你的服务的服务包下。

The first point means that you can group items by logical functionality. Activities could reside under an activity package, and your services under a service package.

第二点是非常重要的,常常被忽视。包访问允许你做一些聪明的事情。例如,你可以有,可以构建并填充具有包访问性能车型,而无需添加大量的setter方法​​或使用公共性质的'建设者'类。这可以使对象的创建非常简单和直观,而对象保持不变外面包。

The second point is quite important and often overlooked. Package access allows you to do some clever things. For example, you can have a 'builder' class which can build and populate models which have package access properties, without adding lots of setter methods or using public properties. This can make object creation really simple and intuitive, while objects remain immutable outside the package.

这个原则一个非常好的例子可以在罗曼盖伊的<一个找到href=\"http://$c$c.google.com/p/shelves/source/browse/#svn/trunk/Shelves/src/org/curiouscreature/android/shelves/provider\"相对=nofollow>货架应用程序。在<一个href=\"http://$c$c.google.com/p/shelves/source/browse/trunk/Shelves/src/org/curiouscreature/android/shelves/provider/BooksStore.java\"相对=nofollow>图书城类可以创建图书对象和修改他们的会员,不暴露这些领域的其他类(其他包)。

A really good example of this principle can be found in Romain Guy's Shelves app. The BookStore class can create Book objects and modify their members, without exposing these fields to other classes (in other packages).

这篇关于Android项目包结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆