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

时间:2011-11-26 12:03:22

标签: java android ant

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

3 个答案:

答案 0 :(得分:6)

我使用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(SDK工具14后的build.properties的新名称)文件中定义了layout.dir:

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

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

答案 1 :(得分:3)

尼克,我不明白你为什么还要重写发布任务。我只添加了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>

答案 2 :(得分:2)

这两个答案都基于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"))
                }
            }
        }
    }

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

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

相关问题