开发者

Custom Android build.xml for rename manifest package

开发者 https://www.devze.com 2023-01-09 03:16 出处:网络
I wanna build one app into 2 version, one paid, one free, and I know aapt have a option \"--rename-manifest-package\" should help, but I don\'t know How to use it in build.xml.

I wanna build one app into 2 version, one paid, one free, and I know aapt have a option "--rename-manifest-package" should help, but I don't know How to use it in build.xml. I find 2 place I may modify:

 <!-- first -->
 <target name="-resource-src" depends="-dirs">
    <echo>Generating R.java / Manifest.java from the resources...</echo>
    <exec executab开发者_如何学Pythonle="${aapt}" failonerror="true">
        <arg value="package" />
        <arg line="${v.option}" />
        <arg value="-m" />
        <arg value="-J" />
        <arg path="${gen.absolute.dir}" />
        <arg value="-M" />
        <arg path="AndroidManifest.xml" />
        <arg value="-S" />
        <arg path="${resource.absolute.dir}" />
        <arg value="-I" />
        <arg path="${android.jar}" />       
    </exec>
</target>



 <!-- sencod -->
 <target name="-package-resources">
    <echo>Packaging resources</echo>
     <aaptexec executable="${aapt}"
            command="package"
            manifest="AndroidManifest.xml"
            resources="${resource.absolute.dir}"
            assets="${asset.absolute.dir}"
            androidjar="${android.jar}"
            outfolder="${out.absolute.dir}"
    basename="${ant.project.name}" >
</aaptexec>
</target>

this article( http://blog.uncommons.org/2010/07/19/building-two-versions-of-the-same-android-app/ ) said I should adding "--rename-manifest-package" at the second place, but How?


The option --rename-manifest-package cannot be used with the aaptexec ant task. It is an option that needs to go straight to the aapt executable, like this;

<exec executable="${aapt}" failonerror="true">
  <arg value="package" />
  <arg value="-f" />
  <arg value="-v" />
  <arg value="--version-code" />
  <arg value="${version.code}" />
  <arg value="--debug-mode" />
  <arg value="-M" />
  <arg path="AndroidManifest.xml" />
  <arg value="-A" />
  <arg path="${asset.absolute.dir}" />
  <arg value="-I" />
  <arg path="${android.jar}" />
  <arg value="-F" />
  <arg path="${out.absolute.dir}/${resource.package.file.name}" />
  <arg value="-S" />
  <arg path="${resource.absolute.dir}" />
  <arg value="--rename-manifest-package" />
  <arg value="com.example.pro" />
</exec>

I found no way of editing a file that actually changes the way Eclipse builds. So I put that piece of code in the build.xml, put it in the project root, and built from the console by typing;

ant debug

And if you depend on any libraries, they need to go into the packaging as well. Look into your default.properties file and you'll see the reference to the lib. Then add that to the rule above, like this;

<arg value="-S" />
<arg path="${android.library.reference.1}/res" />

It starts to feel more and more like a hack that can break with any platform SDK update.


manifestpackage is the arg value for option "--rename-manifest-package" for ant task. Use this to create different packages for your app. override

target name="-package-resources"

from ant script


I recently needed to be able to change the package name of an app at build time. This is a common need when you have a paid and a free version of an app. It's also useful if you want to be able to install multiple versions of an app on your phone, such as a "dev" and a "stable" build.

One way to do it is to transform the whole project as a library project, and then create one final project for each version, that depends on the library project.

Aapt magic

There is another way: aapt has a --rename-manifest-package parameter that rewrites the package name of the binary AndroidManifest.xml file in the final APK.

Here is what aapt help says:

--rename-manifest-package
       Rewrite the manifest so that its package name is the package name
       given here.  Relative class names (for example .Foo) will be
       changed to absolute names with the old package so that the code
       does not need to change.

The great advantage is that your code won't change, the R class stays identical.

Ant

Since r17, this option is available in the aapt Ant task, through the manifestpackage attribute. You'll need to override the -package-resources target, copied from the SDK build.xml:

<target name="-package-resources" depends="-crunch">
  <do-only-if-not-library elseText="Library project: do not package resources..." >
    <aapt executable="${aapt}"
     manifestpackage="com.my.package"
    >
...
    </aapt>
  </do-only-if-not-library>
</target>

Maven

The android:apk goal of the android-maven-plugin has a renameManifestPackage parameter.

One last thing

If you load some resource ids at runtime, you may need to update your code.

I used to do this:

String packageName = context.getPackageName();
Resources res = context.getResources();
int id = res.getIdentifier("my_drawable", "drawable", packageName);

This usually works great, especially in library projects where you do not know the package name.

However, the problem here is that the resources were processed before the package name was finally updated. So packageName is wrong.

It's easy to fix though, by retrieving the package name of another resource with Resources.getResourcePackageName().

Let's create a resource id dedicated to that purpose, for example in res/values/ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <item name="used_for_package_name_retrieval" type="id"/>
</resources>

And now we get the right package:

Resources res = context.getResources();
String packageName = res.getResourcePackageName(R.id.used_for_package_name_retrieval);
int id = res.getIdentifier("some_drawable", "drawable", packageName);

Conclusion

This tip helps creating different versions of the same app.


This tool may help you rename android project package:

https://github.com/lijunjieone/RenameAndroidPackage

0

精彩评论

暂无评论...
验证码 换一张
取 消