11

I'm trying to use a custom build to repackage my Android app. I want to create an internal beta version which I can install side-by-side with my production app.

This answer looks like exactly what I need, however it doesn't seem to work.

Here's the update to my build.xml:

<target
    name="-package-resources"
    depends="-crunch" >

    <echo>Repackaging AndroidManifest.xml to ${package.manifest.name} ${out.absolute.dir}/${resource.package.file.name}</echo>

    <exec executable="${aapt}" failonerror="true">
        <arg value="package" />
        <arg value="-f" />
        <arg value="--auto-add-overlay" />
        <arg value="-M" />
        <arg path="AndroidManifest.xml" />
        <arg value="-S" />
        <arg path="${resource.absolute.dir}" />
        <arg value="-S" />
        <arg path="${android.library.reference.1}/res" />
        <arg value="-A" />
        <arg path="${asset.absolute.dir}" />
        <arg value="-I" />
        <arg path="${project.target.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>

Running it seems to successfully run my new code:

ant debug -Dpackage.manifest.name=com.example.test    
...    
 -package-resources:    
      [echo] Repackaging AndroidManifest.xml to com.example.test /<mypath>/bin/<appname>.ap_
...    
BUILD SUCCESSFUL

However, when I use APKTool to inspect the APK, the package name in my AndroidManifest has not been changed to the new value. All that seems to have happened is that my relative activity paths ".MyActivity" have been expanded to my original package name.

apktool d --force bin/<appname>-debug.apk

Does anybody know what I'm doing wrong? I've looked at all the other Stackoverflow answers and most seem a little out of date. I am building with Android SDK Tools Revision 21.1.0, for minSdkVersion 8.

Update: As @athor comments below his answer, my assumption about inspecting the AndroidManifest.xml is wrong. To test this you actually need to try installing it rather than viewing the decompiled XML!

Community
  • 1
  • 1
Dan J
  • 25,433
  • 17
  • 100
  • 173

2 Answers2

16

This is the way I do it (working)

 <target
        name="-package-resources"
        depends="-crunch" >

        <!-- only package resources if *not* a library project -->

        <echo message="Current Package name: ${app.custompackagename}" />

        <do-only-if-not-library elseText="Library project: do not package resources..." >

            <aapt
                androidjar="${project.target.android.jar}"
                apkfolder="${out.absolute.dir}"
                assets="${asset.absolute.dir}"
                buildType="${build.target}"
                command="package"
                debug="${build.is.packaging.debug}"
                executable="${aapt}"
                ignoreAssets="${aapt.ignore.assets}"
                libraryPackagesRefid="project.library.packages"
                libraryRFileRefid="project.library.bin.r.file.path"
                libraryResFolderPathRefid="project.library.res.folder.path"
                manifest="${out.manifest.abs.file}"
                manifestpackage="${app.custompackagename}"
                nocrunch="${build.packaging.nocrunch}"
                previousBuildType="${build.last.target}"
                resourcefilename="${resource.package.file.name}"
                resourcefilter="${aapt.resource.filter}"
                versioncode="${version.code}"
                versionname="${version.name}" >

                <res path="${out.res.absolute.dir}" />

                <res path="${resource.absolute.dir}" />
                <!-- <nocompress /> forces no compression on any files in assets or res/raw -->
                <!-- <nocompress extension="xml" /> forces no compression on specific file extensions in assets and res/raw -->
            </aapt>
        </do-only-if-not-library>
    </target>

The line manifestpackage="${app.custompackagename}" is the key.

athor
  • 6,848
  • 2
  • 34
  • 37
  • Weird, this doesn't seem to work for me either :-( Same symptoms, the APK gets built, but unpacking it shows the AndroidManifest.xml doesn't have the new package. I tried running "ant debug -Dapp.custompackagename=com.example.test", and I tried a release build too. This is driving me crazy... – Dan J Jun 25 '13 at 00:48
  • 1
    I've just decompilied my app. The packagename in the manifest doesn't change. I think ant changes it in a way that does't affect all the imports etc in your app. So the packagename in the manifest stays the same, but the actual packagename is changed. Have you tried installing both versions of the app side by side? If the package name hasn't changed, you won't be able to. – athor Jun 25 '13 at 01:02
  • Oh wow, you're right - it does install fine alongside the other APK! I'm rather surprised it behaves this way... Thanks! – Dan J Jun 25 '13 at 02:06
  • @athor, this did the trick at first, but my app ran into crash because there were getResources().getIdentifier(...) calls somewhere in the 3rd party libs – fifth Apr 24 '14 at 01:58
  • Hi, can you explain why we need all the lines if we want just to rename package name, and what to enter in other lines if we want just the to rename package?! Thanks :) – Jovan May 06 '14 at 15:06
  • There Nice guys!!! I want to rename my application package for same purpose and I have changed my build.xml according to the given answer and ran this command `ant debug -Dapp.custompackagename=com.example.testpackageOne` `ant debug -Dapp.custompackagename=com.example.testpackageTwo` on cmd but when I installed these two apps side by side then there was only one application on my device couldn't achieve can any friend help me ? – Muzammil Hussnain Mar 24 '15 at 07:02
1

If you really want your changes to reflect in your AndroidManifest.xml, you could create a custom target that's something like this (I use a similar target to this myself):

<target name="repackage">
    <!-- Replaces all of the references to the old package name in files in the "src" directory -->
    <replace dir="src" value="${new.package}" token="${old.package}" summary="true"/>

    <!-- renames the src folders -->
    <move toDir="${new.package.dir}">
        <fileset dir="${old.package.dir}"/>
    </move>

    <!-- replaces the package name in the manifest -->
    <replace file="AndroidManifest.xml" value="${new.package}" token="${old.package}" summary="true"/>
</target>

You could do an ant build that repackaged to a debug package, deployed, and then repackaged back to the production package (or vice versa).

Doppelganger
  • 125
  • 1
  • 7