I'm familiar with C++ but never used Java. I've just started learning Android development. In most of the basic tutorials I've gone through mentioned pack开发者_如何学Goages names like org.example.hello. I know they are some sort of unique identifiers but
- How should I choose these packages names? Are there any guidelines?
- Is there anything special I need to keep in mind if I'm developing a commercial application?
- How can I make separate re-usable libraries and pack them in different packages/applications?
thanks.
Packages in Java serve the same purpose as namespaces in C++. See the Java tutorial for some guidelines.
The most important thing to keep in mind is that your fully qualified class names (package + class) should not be the same as anyone else's. If you make a class that seems like it would provide a really common set of functions, like FileUtils
for example, then you want it to be in a unique package so its name doesn't conflict with anyone else's class of the same name. The general guideline is to use your reversed domain name, so you'll see packages like org.example.hello
.
Package naming is largely irrelevant, except there are some major conventions. For example. the Java API is largely under java.
. The real purpose is to avoid conflict with other code.
The primary impact package naming has is on visibility. Protected members, for example, are available to the same package (and subclasses), but not to code in other packages.
For example, on my phone, code seems to be primarily in com
:
com.areacode.drop7
com.google.android.apps.
com.meebo.
but some is in org
(mostly open source stuff):
org.connectbot
org.npr.android.news
Some discussion on Wikipedia:
http://en.wikipedia.org/wiki/Java_package#Package_naming_conventions
As long as your company name (and app name) is in the package name, you should be fine.
Eclipse has provisions for linking multiple projects together as libraries. If you are using ant for your build process, you can do the inclusion there. There's not a real concept of a "library" (a JAR file in J2SE) that I'm aware of. Actual JAR files (as compiled for J2SE) need to be cross-compiled to the Android format (DEX).
Using the android file browser ASTRO allows you to see which package names the application on your phone have.
com.CompanyName.ProgramName seems to be the standard.
精彩评论