我已经解决了将 api 密钥错误集成到构建过程和源代码控制中的问题,方法是将其作为存储在local.properties
. 我必须将以下内容添加到build.xml
:
<property name="mapviewxml" value="res/layout/mapview.xml" />
<target name="-pre-build">
<fail unless="mapsApiKey">You need to add mapsApiKey=... to local.properties</fail>
<copy file="mapview.xml.tpl" tofile="${mapviewxml}" overwrite="true">
<filterchain>
<replacetokens>
<token key="apiKey" value="${mapsApiKey}"/>
</replacetokens>
</filterchain>
</copy>
</target>
现在,当然我必须mapview.xml.tpl
在我的项目根目录中创建(它不能去,res/layout
因为它会破坏构建过程):
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="@apiKey@"
/>
在预编译期间,模板被复制到正确的位置,@apiKey@ 被替换为真实的密钥。不幸的是,在这个阶段我还没有找到区分调试和发布版本的方法,所以要编译发布,我只需将发布 apiKey 添加到 ant 参数中:
ant -DmapsApiKey=.... release
这种方法可以很好地与 SCM 集成(我不需要签入密钥),并且可以与构建过程集成。