In the post on creating an Android library to support multiple application files, I ran into the problem of what the Android SDK considers -- at least internally -- the "manifest package."
That package gets used for too many things, I think. I believe that the application ID, the application's user ID and the resource package should all be independently determinable. Alas they are not. At least not yet.
Some day you might need to control the specific package that R gets generated in. I'm not sayin it's a good idea. But somebody will want to do it. Just sayin...
So here is a hack of a whorkaround [sic] to get R into the fixed package of your nightmarish fancy...
If you're using ANT to build your app, then things are a little easier. If you're using only ADT, there are a couple of extra steps.
First thing to do, if you're using ADT only, is to create ant build files: build.properties and build.xml. Simplest way to do this is to create a dummy Android Ant project and copy those files:
android create project -n duh -p duh -k org.duh -t 2 -a duh
Once you have your and build files, open build.xml. Scroll down. Close to the bottom, you should see a line like this:
<setup />
Just above that line, copy and paste the following xml:
<target name="-dirs"> <echo>Creating output directories if needed...</echo> <mkdir dir="${resource.absolute.dir}" /> <mkdir dir="${jar.libs.absolute.dir}" /> <mkdir dir="${out.absolute.dir}" /> <if condition="${manifest.hasCode}"> <then> <mkdir dir="${gen.absolute.dir}" /> <mkdir dir="${out.classes.absolute.dir}" /> </then> </if> </target> <target name="-resource-src" depends="-dirs"> <if condition="${manifest.hasCode}"> <then> <echo>Generating R.java / Manifest.java from the resources using custom aapt execution...</echo> <exec executable="${aapt}" failonerror="true"> <arg line="package -v --custom-package ${resource.custom.package} -M AndroidManifest.xml -I ${android.jar} -J ${gen.absolute.dir} -S ${resource.absolute.dir}" /> <arg value="-m"/> </exec> </then> <else> <echo>hasCode = false. Skipping...</echo> </else> </if> </target> <target name="resource-src-fix" depends="-resource-src" />
Now, open build.properties and enter this line:
resource.custom.package=org.inanity.domestic
If you are building with ANT, then that's all you have to do. You can now change your package in your manifest without it moving your R.java file.
If you are using ADT, there will be one more hack you'll want to do. Every time the Android Pre Compiler builder runs (which will be frequently if you have automatic builds turned on), it will clobber your beloved R.java.
Here's what you can do about that.
Go to Project... Properties...
- Select Builders and click on the New... button.
- Select "Ant Builder" from the list and click Ok.
- In the Main tab, click the Browse Workspace... button to find your modified build.xml file.
- Switch to the Targets tab and select the "resource-src-fix" target wherever you want it.
- Name your builder something sensible -- like Resource_compensation_builder -- and click Ok.
Disclaimer: I never said that any of this this was a good idea. Just that it can be done. :-)
No comments:
Post a Comment