【发布时间】:2011-11-26 12:03:22
【问题描述】:
我想在 android ant 发布和调试构建期间附加不同的字符串资源文件。我需要这个单独配置地图 api 密钥、远程主机等
【问题讨论】:
我想在 android ant 发布和调试构建期间附加不同的字符串资源文件。我需要这个单独配置地图 api 密钥、远程主机等
【问题讨论】:
我使用 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 Tools 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 的目录添加一个属性
【讨论】:
尼克,我不明白你为什么还要重写发布任务。我只添加了预构建,它对我有用
<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>
【讨论】:
这两个答案都是基于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。
【讨论】:
release.preferences.xml 和debug.preferences.xml。