问题 如何在android ant发布和调试版本中附加不同的字符串资源文件?


我想在android ant发布和调试版本中附加不同的字符串资源文件。我需要这个有地图api密钥,远程主机等的单独配置


12577
2017-11-26 12:03


起源



答案:


我使用Eclipse进行日常调试构建,然后使用Ant进行发布构建。我有包含Google Maps API密钥的文件的调试版和发行版。我修改了build.xml(我在SDK工具R15上),在构建之前和之后复制相应的文件。我已经改变了-pre-build和release目标,如下所示:

    <target name="-pre-build">
        <echo message="In pre build-----------------------------------" />
        <echo message="build target          ${build.target}" />
        <if condition="${build.is.packaging.debug}">
            <then>
                <echo>Copying debug api key************************************</echo>
                <copy file="${layout.dir}/googlemapdebug.xml" tofile="${layout.dir}/googlemap.xml" overwrite="true" />
            </then>
            <else>
                <echo>Copying RELEASE api key************************************</echo>
                <copy file="${layout.dir}/googlemaprelease.xml" tofile="${layout.dir}/googlemap.xml" overwrite="true" />
            </else>
        </if>
    </target>




<target name="release"
            depends="clean, -set-release-mode, -release-obfuscation-check, -package, -release-prompt-for-password, -release-nosign"
        ............. LINES OMITTED
        ............. 
            <!-- Zip aligns the APK -->
            <zipalign-helper in.package="${out.unaligned.file}"
                                       out.package="${out.final.file}" />
            <echo>Release Package: ${out.final.file}</echo>

            <echo>Always copy DEBUG MAPS API file back for Eclipse   **********************</echo>
            <copy file="${layout.dir}/googlemapdebug.xml" tofile="${layout.dir}/googlemap.xml" overwrite="true" />
        </sequential>
    </do-only-if-not-library>
    <record-build-info />
</target>

我在ant.properties(build.properties发布SDK Tools 14的新名称)文件中定义layout.dir:

projectname=MyProject
workspace.dir=/dev/projects/EclipseIndigo/AndroidWorkTwo
base.dir=${workspace.dir}/${projectname}
layout.dir=${base.dir}/res/layout

您可以根据自己的需要进行调整,假设您没有太多文件可以进行交换。我想你可以为持有strings.xml的目录添加一个属性


6
2017-11-26 13:05



尼克,我不明白你为什么还要重写发布任务。我只添加了prebuild,它对我有用 - Alexey Zakharov
那是因为我通常每天都在使用Eclipse。所以下次我使用Eclipse构建时,我希望它使用maps API xml文件的调试版本。如果我没有改变发布目标,那么将使用该文件的发行版本,因为我主要使用Ant构建来发布版本。 - NickT


答案:


我使用Eclipse进行日常调试构建,然后使用Ant进行发布构建。我有包含Google Maps API密钥的文件的调试版和发行版。我修改了build.xml(我在SDK工具R15上),在构建之前和之后复制相应的文件。我已经改变了-pre-build和release目标,如下所示:

    <target name="-pre-build">
        <echo message="In pre build-----------------------------------" />
        <echo message="build target          ${build.target}" />
        <if condition="${build.is.packaging.debug}">
            <then>
                <echo>Copying debug api key************************************</echo>
                <copy file="${layout.dir}/googlemapdebug.xml" tofile="${layout.dir}/googlemap.xml" overwrite="true" />
            </then>
            <else>
                <echo>Copying RELEASE api key************************************</echo>
                <copy file="${layout.dir}/googlemaprelease.xml" tofile="${layout.dir}/googlemap.xml" overwrite="true" />
            </else>
        </if>
    </target>




<target name="release"
            depends="clean, -set-release-mode, -release-obfuscation-check, -package, -release-prompt-for-password, -release-nosign"
        ............. LINES OMITTED
        ............. 
            <!-- Zip aligns the APK -->
            <zipalign-helper in.package="${out.unaligned.file}"
                                       out.package="${out.final.file}" />
            <echo>Release Package: ${out.final.file}</echo>

            <echo>Always copy DEBUG MAPS API file back for Eclipse   **********************</echo>
            <copy file="${layout.dir}/googlemapdebug.xml" tofile="${layout.dir}/googlemap.xml" overwrite="true" />
        </sequential>
    </do-only-if-not-library>
    <record-build-info />
</target>

我在ant.properties(build.properties发布SDK Tools 14的新名称)文件中定义layout.dir:

projectname=MyProject
workspace.dir=/dev/projects/EclipseIndigo/AndroidWorkTwo
base.dir=${workspace.dir}/${projectname}
layout.dir=${base.dir}/res/layout

您可以根据自己的需要进行调整,假设您没有太多文件可以进行交换。我想你可以为持有strings.xml的目录添加一个属性


6
2017-11-26 13:05



尼克,我不明白你为什么还要重写发布任务。我只添加了prebuild,它对我有用 - Alexey Zakharov
那是因为我通常每天都在使用Eclipse。所以下次我使用Eclipse构建时,我希望它使用maps API xml文件的调试版本。如果我没有改变发布目标,那么将使用该文件的发行版本,因为我主要使用Ant构建来发布版本。 - NickT


尼克,我不明白你为什么还要重写发布任务。我只添加了prebuild,它对我有用

<target name="-pre-build">
    <if condition="${build.is.packaging.debug}">
        <then>
            <echo>Copying debug config</echo>
            <copy file="./config/debug.xml" tofile="./res/values/config.xml" overwrite="true"/>
        </then>
        <else>
            <echo>Copying release config</echo>
            <copy file="./config/release.xml" tofile="./res/values/config.xml" overwrite="true"/>
        </else>
    </if>
</target>

3
2017-11-27 03:21





这两个答案都是基于 Eclipse ADT。现在 Android Studio 是比较流行的IDE,你可以利用它 gradle 构建系统。

在你的 build.gradle 文件,添加以下代码 android 标签:

buildTypes {
        release {
            buildConfigField "boolean", "LOG_DEBUG", "false"
            resValue "string", "some_key", "AAAAAAA"
        }
        debug {
            buildConfigField "boolean", "LOG_DEBUG", "true"
            resValue "string", "some_key", "BBBBBBB"

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
                }
            }
        }
    }

您可以使用 BuildConfig.LOG_DEBUG 作为一个 boolean Java代码中的值。请记住 import your.package.name.BuildConfig;

你也可以使用 some_key 作为字符串资源。使用它的Java代码是: R.string.some_key


2
2017-08-17 08:51



是否可以拥有两个不同的资源文件?我想拥有 release.preferences.xml 和 debug.preferences.xml。 - Paweł Szczur
@orian我没有尝试过两个res文件。我认为使用gradle脚本非常简单。 - Wesley