Is there an Android tool that will change a project's package name via command line. I don't want to use Eclipse's Android Tools -> Rename Application Package.
1 Answers
If you want to do it as a one time change to the source you can use the Android command line tool android update project
Documentation is in the [developer docs][1].
If you want to do it dynamically during every build you will need a custom ant build script to pass arguments into aapt
Unfortunately you can not use the default aapt task because it doesn't support setting the option --rename-manifest-package.
You need to add the following target to the default build.xml
file android
command line creates; add it prior to <import file="${sdk.dir}/tools/ant/build.xml" />
<!-- Replaces the target in tools\ant\build.xml -->
<target name="-package-resources" depends="-crunch">
<!-- this exec replicates the output of the aapt task in build.xml
-package-resources with the addition of a rename-manifest-package option. -->
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-f" />
<arg value="--auto-add-overlay" />
<arg value="-M" />
<arg path="${basedir}/AndroidManifest.xml" />
<arg value="-S" />
<arg path="${resource.absolute.dir}" />
<arg value="-S" />
<arg path="${basedir}/${android.library.reference.1}/${resource.dir}" />
<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="--rename-manifest-package" />
<arg value="${package.manifest.name}" />
</exec>
</target>
The property package.manifest.name
will contains the name of the package. You can set that in a properties file or as a command line option -Dpackage.manifest.name=value
You can also extend this to rename the output apk based on a property as well.
This question covers getting version info from the manifest to add to the apk output name: How to include version string in the filename when building Android apk with ant?
You will want to look at the build.xml file that is imported to get an idea of what you will need to override.
You can keep adding rule overrides to the local build.xml file, but due to the level of customization you need to do, I'd think about directly copy the build.xml file from the android tools and modify it rather than using it as an import. This does mean more work making sure it still works across every update to the android tool chain.
-
Thank you for your answer! As I've mentioned when replying to @edthethird, I need to update the package name, version number and version name. I would really like to use Ant but am at a real loss. Any good resources you could link me to? – OrhanC1 Mar 25 '12 at 17:43
-
The ant documentation is a good place to start, also just looking at the android build.xml file in {sdk.dir}/tools/ant/build.xml should give you a good idea how most of it works. The aapt target is really the only special sauce because of limitations on the default aapt java task that comes with the tools. The rest is just about getting property values set correctly. – cistearns Mar 25 '12 at 18:23
-
After a lot of rooting around and attempts at various answers, this answer is what worked for me, lifesaver! – Guykun Nov 05 '12 at 09:39
-
1FYI, you can now use the aapt task directly instead of exec. Also, be aware that inspecting the XML in the decompiled APK doesn't make it obvious if this package rename has actually worked or not! See http://stackoverflow.com/a/17287287/112705 – Dan J Jun 25 '13 at 02:22
-
Hi. This was what I was struggling for. How can I set the values from properties file? – LoveMeSomeFood Dec 31 '13 at 20:16