Reputation: 10077
I am developing a generic Android game engine that will be used in many of my Android apps as the base system. The problem is that in all of my Java files I currently have to hardcode the package name like this:
package com.example.mygameengine;
But because I want to use the code of my generic game engine in many different apps, I need to find a way to specify the package name for the Java files at compile time because I do not want to keep several copies of my Java sources just because of differences in the package name. I want to have one central source tree and the package name should be dynamically changeable depending on the app I'm about to compile.
So is there a way to do something like this:
package $(PACKAGE_NAME)
In the Java sources where $(PACKAGE_NAME) is to be substituted with the real package name at compile time? Maybe javac has an option that allows me to specify a package name for the file it is passed instead of taking it from the file itself? Note that I'm not using Eclipse but barebones command line tools like ant and make.
EDIT: I do not understand why this is tagged as a duplicate. I've asked a fundamental question about whether the "package" directive in the Java language requires a hard-coded string argument in the source code or whether it is also possible to set this package name at compile time using a compiler directive. That's quite a different question than the one that has been linked here as the presumedly "original" question which is much more closely tied to the Android build system. My question is about the fundamentals of the Java language, not about the Android build system.
Upvotes: 1
Views: 1725
Reputation: 15768
It's not possible, because its part of core meta-data about your class
A package is a collection of related Java entities (such as classes, interfaces, exceptions, errors and enums). Packages are used for:
Resolving naming conflict of classes by prefixing the class name with a package name. For example, com.zzz.Circle and com.yyy.Circle are two distinct classes. Although they share the same class name Circle, but they belong to two different packages: com.zzz and com.yyy. These two classes can be used in the same program and distinguished using the fully-qualified class name - package name plus class name. This mechanism is called Namespace Management.
Access Control: Besides public and private, Java has two access control modifiers – protected and default – that are related to package. A protected entity is accessible by classes in the same package and its subclasses. An entity without access control modifier (i.e., default) is accessible by classes in the same package only.
For distributing a collection of reusable classes, usually in a format known as Java Archive (JAR) file.
Upvotes: 0
Reputation: 597254
You can't. And that's not the proper approach to code reuse.
Simply package your commonly used code in a jar and include that jar in every project you want (in Android you do that by adding the class to the classpath, and then marking it as exported in the "Order & Export" tab)
Upvotes: 1
Reputation: 24344
Can you build your generic game engine code into a stand alone JAR.
Then you can include it as a dependency into each of your Android app that uses it.
With proper versioning you will then have just one central place where this code is stored.
Upvotes: 5