How can I change the application package name for an Android app via command line

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.

Leave a Comment