0

In an Android application, I notice that the package name of a class does not have to match the name that was generated when I created the new project. In other areas, Java is insistent that everything matches. I would like to understand why this is not the case for package names, and what the implications are.

Example: I create a new project named TextToSpeech, using my own reverse domain name. This gives a package name of com.lexogram.texttospeech; the path to the MainActivity.java class file is TextToSpeech/app/src/man/java/com.lexogram.texttospeech/.

I now go to a TextToSpeech tutorial and copy-and-paste the code into my project. This code uses com.example.texttospeech as the package name, both in the MainActivity class and in the AndroidManifest.xml. I run the project, and everything works fine.

Does this mean that each activity can use a different package name, so long as the name is used consistently across all files in the activity?

1 Answer 1

1

What it means is that your project can have multiple packages inside of it.

Java documentation explains them nicely:

To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages.

Sign up to request clarification or add additional context in comments.

2 Comments

Suppose I have a set of files that deal (say) with Text To Speech, and another set of files that deal with Pattern Recognition. These sets will include classes, layouts and resources. If I understand correctly, I should create a separate package for each of them, and save the classes in separate sub-directories of the java/ folder. For example: java/com/example/texttospeech/TTS.java, java/com/example/patternrecognition/PatternRecognition.java, and so on. Is that what your answer implies?
@JamesNewton big projects are split to packages, so it's easier to manage hundreds of files and browse to find something you need. Personally I split my application functionality into sub packages, just like you mentioned in your example of text recognition.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.