Changing application package name in custom Ant build step

本秂侑毒 提交于 2019-11-27 07:42:31

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.

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).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!