2013-12-13 34 views
10

我的Android清單在調試和發佈時使用不同的值。Android清單中調試或發佈模式的單獨XML行

區分每種構建類型的值的最簡單方法是什麼?

當調試:

<meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="my-lovely-debug-api-key" /> 

當釋放:

<meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="my-lovely-release-api-key" /> 

TIA。

+1

您使用的是什麼樣的構建系統? (maven,ant,gradle ...) – Fredszaq

+2

無論日食使用:) – ericosg

回答

1

請注意,如果您使用默認的Eclipse構建,它可能會在eclipse中使用特殊配置的「Ant」模塊。您可以通過檢查Eclipse/Prefs中的'Window/Preferences/Ant'部分來檢查Eclipse/Ant的協調程度....

一個解決方案是使Eclipse/Ant的協作更加明確, 'build.xml'文件是項目的一部分(項目級構建文件,而不是從$ SDK/ROOT文件夾生成時複製的默認文件)。

請參閱here並閱讀底部here的註釋以更好地瞭解如何使用項目級別「build.xml」文件。

一旦你消化的是,該解決方案如下會更有意義......

--Solution--

修改版本和你「的build.xml」的調試節文件內容如下:

<!-- **************** Debug specific targets *************** --> 
    ... 
    <target name="-set-debug-mode" depends="-setup"> 
    ... 

     <!-- API modify belo for build-typ --> 
     <copy file="config/strings.xml" todir="res/values"> 
     <filterset> 
    <filter token="CONFIG.API" value="${config.db.api}"/> 
    <filter token="CONFIG.REST" value="${config.db.rest}"/> 
    </filterset> 
    </copy> 
    ... 



    <!-- *************** Release specific targets ************** --> 
    ... 
    <target name="-set-release-mode" depends="-set-mode-check"> 
    ... 
    <!-- API modify belo for build-typ --> 
    <copy file="config/strings.xml" todir="res/values"> 
    <filterset> 
    <filter token="CONFIG.API" value="${config.db.api.prod}"/> 
    <filter token="CONFIG.REST" value="${config.db.rest.prod}"/> 
    </filterset> 
    </copy> 

,並在你的根文件夾中的「ant.properties」文件,把屬性值的API密鑰和什麼...

# API condition builds dev|prod in DB @parse.com 
config.db.api=some_key_val 
config.db.rest=some_k2_val 
config.db.api.prod=some_k3_val 
config.db.rest.prod=some_k4_val 

而且在資源XML是的「複製」目標的構建命令綁在一起,它...

'config/strings.xml' 

<string name="default_value_parse_key_appId">@[email protected]</string> 
<string name="default_value_parse_key_rest">@[email protected]</string> 
+1

似乎比我目前的解決方案,它比運行預構建交換值的代碼更好。謝謝 – ericosg