I need to create two different Android apps using the same code. The user should be able to install both the apps on the same device.
The differences between the two apps are just a few strings, including the application name. These strings have been isolated to a string resource file called Custom.xml.
My plan to achieve two versions is rather simple. In the nightly build script:
1. Run Ant to create the first application .apk file.
2. Rename the generated .apk file
3. Replace Custom.xml with a different one
4. Run Ant once again
This may work except for one thing that I am not sure about. It is the package name in AndroidManifest.xml->manifest->package attribute. I guess this has to be different if the apps have to coexist. Does changing the package name have any impact on the rest of the code? I hope this package name is not tied to the package name used in java files.
Also, does anyone see any problem with my overall strategy? Thank you in advance for your help.
No, it does not have any impact, unless you use your hardcoded package name in the code.
There is an official example of renaming the package to build a debuggable version of the app.
What is the correct way to use more than one package in one Android application?
If there is more than one package, is it correct to put all the names in the manifest tag, like in the example below?
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.application"
package="com.example.pack2"
package="com.example.pack3"
android:versionCode="1"
android:versionName="1.0" >
The Android package in the manifest and the Java packages of the classes you use are separate concepts. They happen to be the same by default, for convenience. However, they can be completely different and unrelated.
When you declare your activities, services, content provider, and broadcast listener, you can specify the component class name in on of the following three ways (assuming your Android package is org.example.myapp)
Class name, i.e. MyActivity. The OS will assume your class is in Java package that has the same name as your Android package, and will resolve it to a fully qualified class name as org.example.myapp.MyActivity. Note, this form is equivalent to .MainActivity, which is a...
Relative class name, i.e. .ui.MyActivity. The OS will assume your class is in Java package that is inside of a Java package with the same name as your Android package, and will resolve it to a fully qualified class name as org.example.myapp.ui.MyActivity. Note the leading ., it is important! Otherwise you'll fall through to...
Fully qualified, i.e. com.android.example.myapp.MyActivity. The OS will use this as the exact name of the class it needs to load, and will ignore the Android package name.
The confusion between Android package name and Java package name is due to using the same term for two independent concepts. The documentation for ComponentName does not make it any easier, as it calls its two components package and class, whereas they are really Android package and Java class (where any of the three variants above are acceptable).
With that longer preamble, the answer to your question is simple:
You can't declare more than one packages in the manifest. However, you also don't need to. The Android package name is in reality the identity of your application as far as the OS is concerned, and nothing more. You can use any Java packages in your code.
Warning:
The auto-generated R class that contains the identifiers for all your resources is generated by default in a Java package with the same name as your Android package. Therefore, if your Android package name and your Java package name are different, you have two options:
Explicitly import the R class from the Java package with the name of your Android package
Use the appt tool from the command line and the --custom-package option to generate the R file into your main Java package. (I am linking to eLinux, because the Android documentation does not have much about the tool. But it is part of the Android SDK)
While this might seem as a proof that the two package names are relate, it's again mere convenience adopted by the tools. Sadly, while the aapt tool allows explicit setting of the target Java package for the R class, both Eclipse and IntelliJ hide that option, which makes the confusion more complete.
You would just simply import the package.
However, if I remember correctly, the classes in the other packages would not be seen by default. You will have to include
import <app-package>.R;
with app-package being the name of the application package. (without the brackets)
Also make sure that any import statements you have refers to the correct projects R file.
I think it can not be. You can do like this ,if package="com.example.pack2" and you want to declare a Activity which is the package package="com.example.pack3" ,you must use a full path
like com.example.pack3.Activity!
i got an app on the market with 5 packages in it and i only put the package with the activitys in it in the manifest and i havent gotten any problems with it if that helps any
and if your going to put your app on google play it wont accept package names starting with
com.example.
that is a reserved package name
I want to change the package name for an app which is not yet published in the Market. Is there a way to use the new package name as the uniqueId on Market/Play and while still using the old package name in my code?
Changing the package name in the manifest will change how Google Play reads your package name. You don't need to change the package name anywhere else to accomplish that. In Eclipse, the package name for classes can be changed independently (directly under src).
As zapl mentions, you may need to handle a few manual items if your application package name differs from that of the classes within (shortcuts that assume everything is in the same package don't always work in that case). For example, using ".classname" may need to change to a fully qualified classname in the manifest and elsewhere.
If you're looking for a way to change the package name to be different in Google Play than it is in the manifest, then no you can't (it wouldn't be a good idea anyway). You can, however, use library projects to create a new app with the new package name without changing the original code or manifest. Minimal code/xml would need to be written.
Hope the question is clear enough. I don’t find something appropriate here or somewhere in the web which explains me why is it not possible. Maybe some of you can help me out?
It’s a general question with no problem in background (however you can always change the name of the package to avoid this) but I want to understand the idea behind this.
[Update]
Maybe I got a wrong expression about the package name? I think a package name is used to create logical units together. So, e.g. I check a slider and a checkbox alone I create a package name like de.test to put these projects to the same package name. And if I create some customer project I use a package name like de.company for all my projects. But if it’s the package name only for create a unique modifier this though is wrong…
The simple explanation is that the package name represents the unique identifier for the application. Its given in the google documentation for mainfest file
Here is the link and a line from that page. This link will also give you more idea.
It names the Java package for the application. The package name serves
as a unique identifier for the application.
I am looking for ways to release two android apps with same source code without any changes but with two different package name without refactoring.
How do I do this?
You need to:
Refactor code to change package name in a IDE (like a Idea or Eclipse)
Change package name in AndroidManifest.xml
Build an sign the application.
What exactly is causing a problem?
when ever you change package name to Refactor but in R file will not package name so and android market consider R file package name.
Change package name in AndroidManifest.xml file now R package name will automatic change. now change old src package name new one. make sure activity is proper mapping.
What is bothering you? Just go ahead and release it with different package names. Make sure you make suitable refactoring in the code as well.