调试和生产资源
问题描述:
某些Android库(如Google Analytics)使用资源进行配置(例如ga_trackingId
)。调试和生产资源
在这些情况下,我有不同的调试和生产值。我目前所做的是在调试时手动评论生产值,反之亦然。它看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- DEBUG -->
<string name="ga_trackingId">UA-12345678-1</string>
<integer name="ga_dispatchPeriod">1</integer>
<bool name="ga_debug">true</bool>
<!-- PRODUCTION -->
<!--string name="ga_trackingId">UA-87654321-1</string>
<integer name="ga_dispatchPeriod">120</integer>
<bool name="ga_debug">false</bool-->
</resources>
的开关配置这种方式是繁琐且容易出错的,如果我不小心产生不必要的资源库的变化。有没有更好的办法?
(例如:在iOS中使用了带有IF DEBUG
宏观条件编译)
答
我曾与他们依赖于签名谷歌地图按键类似的问题。我所做的就是使用ant脚本,该脚本有条件地生成/复制项目资源。您可以在Eclipse下的Project项目>属性>构建器中包含ant脚本
如果您需要在代码中使用DEBUG值,则可以创建一个静态值的java文件,这些静态值也将包含在内。
如果ant环境变量正常工作,请发表评论(您可以在脚本执行后在控制台中看到“Build type:”消息)。
<project name="build-res">
<property name="conditional.resources.dir" value="myresources" />
<property name="keys_file" value="res/values/keys.xml" />
<target name="copy-release" if="${build.mode.release}" >
<property name="build.type" value="Release" />
<echo message="Build type: ${build.type}" />
<property name="google.maps.key" value="nanana-value-for-release" />
<copy file="${conditional.resources.dir}/Release.java" tofile="gen/com/example/project/BuildInfo.java" />
</target>
<target name="copy-debug" if="${build.mode.debug}">
<property name="build.type" value="Debug" />
<echo message="Build type: ${build.type}" />
<property name="google.maps.key" value="lalala-value-for-debug" />
<copy file="${conditional.resources.dir}/Debug.java" tofile="gen/com/example/project/BuildInfo.java" />
</target>
<target name="build-res" depends="copy-debug,copy-release">
<echo file="${keys_file}" message="<?xml version='1.0' encoding='utf-8'?><resources><string name='google_maps_key'>${google.maps.key}</string></resources>" />
</target>
</project>
像res/values-debug这样的东西会很棒。 – hpique 2013-03-06 08:45:39