都是年轻气盛的小伙子,难免肝火旺盛,尤其当你碰到平时本来做的很顺手、此时此刻却怎么都搞不定的时候.Michael Del Borrello是多人博客Bonobo的维护者之一,Bonobo专注于商务、开发以及投资.Michael最近因为Android build process的事情搞的很上火:
出现问题
Debug build一个配置文件、Release build一个配置文件,这是常识吧?那为什么Android的build process怎么就TM这么难用呢?我花了两天时间,翻遍了Eclipse doco、Intelli J doco、Android doco、Ant doco、the Android SDK Ant build file、Google、StackOverflow,你别说还真让我找到一个 将将好用 的解决方案,就是用Ant.
我只知道Ant是一种长6条腿的昆虫好吧!就为了搞定这个很普通的任务我花了两天时间搞定了这个Ant!感谢Android团队,你们又让我找回小时 候研究汉诺塔的感觉了.问题出在多名开发者协同搞一个应用时,因为每个开发者都有自己的debug key,同时也需要有自己的Maps key.例如给Facebook新添加一个API时,你的API和其他开发者的API混在一起,结果就是你不得不捋清各种API之间的关系.
解决方案
1.创建一个Ant文件名为build.xml,包含以下代码:
android update --target "Google Inc.:Google APIs:14" --path ./
2.将该文件添加到Ant Buil Tool窗口中
3.添加以下代码到build.xml中
<target name="process-template">
<filterset id="build-tokens">
<filter token="app.debug" value="${app.debug}"/>
<filter token="maps.key" value="${maps.key}"/>
</filterset>
<copy file="./templates/BuildValues.java" todir="./src/com/company_name/app_name" overwrite="true">
<filterset refid="build-tokens" />
</copy>
<copy file="./templates/build_values.xml" todir="./res/values" overwrite="true">
<filterset refid="build-tokens" />
</copy>
<copy file="./templates/AndroidManifest.xml" todir="." overwrite="true">
<filterset refid="build-tokens" />
</copy>
</target>
4.使用以下命令复制你的debug.keystore文件到项目的根目录下
(cp ~/.android/debug.keystore /your/app/).
5.创建一个debug.properties文件,形如
app.debug = true
maps.api.key = YOUR_DEBUG_MAPS_API_KEY
6.创建一个release.properties,形如
app.debug = false
maps.api.key = YOUR_RELEASE_MAPS_API_KEY
7.创建一个templates/BuildValues.java,形如
import com.company_name.app_name
public class BuildValues {
public static final boolean IS_DEBUG = @app.debug@;
public static final String MAPS_KEY = "@maps.api.key@";
}
8.创建一个templates/BuildValues.xml,形如
<resources>
<string name="map_api_key">@maps.key@</string>
</resources>
9.现在可以直接从IntelliJ的Ant插件中运行full-debug的full-release了.
虽然不够完美,但好歹解决问题了。