に格納されているプロパティにすることで、ビルドプロセスとソース管理への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(キーをチェックインする必要はありません)とうまく統合され、ビルドプロセスとうまく統合されます。