在 Eclipse 中构建多个(测试/生产)版本的 Android APK
我正在寻求优化同一 Android 应用程序稍微不同的 APK 的生成,唯一的区别是它使用的 http API 服务器(dev/staging/prod)。
理想情况下,我只希望我的 Eclipse 构建 2 个 APK,一个包含产品服务器,另一个包含开发服务器。
我什至可以接受 2 个运行配置,但我无法弄清楚如何将参数传递给应用程序并从代码中读取它们。
顺便说一句,我想以 1.5 为目标,并且我想使用 Eclipse 自动构建工具,所以我正在寻找最通用的解决方案。
谢谢。
I'm looking to optimize generating of slightly different APKs of the same Android app, the only difference being the http API server it's using (dev/staging/prod).
Ideally, I'd just want my Eclipse to build 2 APKs, one with the prod server and one with the dev one.
I'm even OK with having 2 Run configurations, but I haven't been able to figure out how to pass parameters to the app and read them from the code.
I want to target 1.5, BTW, and I'd like to use Eclipse auto-build tools, so I'm looking for the most generic solution.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为使用 ant 构建脚本将是最简单的解决方案。 Eclipse支持ant构建,因此您可以在eclipse中运行ant命令。
你可以像这样用 ant 解决你的问题。
xml 将如下所示:
资源 #1:
资源 #2:
和 ant脚本会是这样的:
I think using ant build script would be the easiest solution. Eclipse supports ant build, so you can run ant command in eclipse.
You can solve your problem with ant like this.
xml would be like this:
resource #1:
resource #2:
and ant script would be like this:
将所有代码移至库项目,请参阅
http://developer.android.com/guide/developing/projects /projects-eclipse.html#SettingUpLibraryProject
然后在 eclipse 中创建单独的项目用于测试和生产,每个项目都有唯一的包名称。然后您可以使用包名称来区分版本。
类似这样的:
这对于管理不同的 http 端点来说似乎有点矫枉过正,但它会让代码更易于管理。您还可以做一些有用的事情,例如:
这一切都可以在 eclipse 中完成,无需使用第三方工具。
Move all you code to a library project see
http://developer.android.com/guide/developing/projects/projects-eclipse.html#SettingUpLibraryProject
Then create separate projects in eclipse for test and production each with a unique package name. You can then use the package name to distinguish between versions.
Something like:
This may seem like overkill for managing different http end points but it will make the code more manageable. You can also do useful things like:
This can all be done in eclipse without using and third party tools.
这并不是您真正想要的:
如果应用程序使用调试密钥进行签名,并使用发布证书进行生产,那么您可以访问开发/登台服务器。
Its not really what you want:
You could then hit a dev/staging server if the app is signed with a debug key, and production with a release certificate.
为了传递参数,您始终可以在 android 的目录系统中创建一个文件,并让您的代码从中读取它。
For passing parameters, you could always create a file in android's directory system and have your code read it from it.
就我而言,我只想在不同版本之间更改
strings.xml
中的一些值。首先,我必须加载 ant-contrib 库,以定义 for 循环任务:
我放置了配置列表 config.names,在
properties
文件中:并定义一个
build-all
目标,该目标循环遍历config.names
:定义自定义
resources< /code> 目录,将目录名称保存在 config.resources
属性中:删除它,并将全局资源从
res
复制到其中:更改
- 将配置名称中的
替换为/
,使其成为 URL 参数中的路径:运行 XSLT 转换以修改
strings.xml
文件:这是我使用的 XSLT 样式表:
然后返回
custom_rules.xml
,然后从原始(未修改的)res/values/strings.xml< 中提取
app_version
/code>:并使用
antcall
任务调用debug
构建:使用两个更改的属性值:
resource.absolute.dir
告诉 < code>debug 目标使用我修改后的res
目录,该目录在上面的config.resources
属性中定义;out.final.file
告诉它生成一个具有不同名称的 APK,包括配置名称(例如student-testing
)和从字符串中提取的版本号.xml
。最后,我可以从命令行运行 ant build-all 并构建所有四个目标。在
build-all
目标结束之前,再多一点脚本,列出已编译的 APK 文件以供参考:In my case I just wanted to change a few values in
strings.xml
between different versions.First I have to load the
ant-contrib
library, to define thefor
loop task:I put my list of configurations,
config.names
, in aproperties
file:And define a
build-all
target, that loops over theconfig.names
:Defining a custom
resources
directory for each one, saving the directory name in theconfig.resources
property:Delete it, and copy the global resources from
res
into it:Change
-
to/
in the config name, to make it a path in the URL parameter:Run an XSLT transform to modify the
strings.xml
file:This is the XSLT stylesheet that I use:
And back to
custom_rules.xml
where I then extract theapp_version
from the original (unmodified)res/values/strings.xml
:And use the
antcall
task to call thedebug
build:with two changed property values:
resource.absolute.dir
tells thedebug
target to use my modifiedres
directory, defined in theconfig.resources
property above;out.final.file
tells it to produce an APK with a different name, including the configuration name (e.g.student-testing
) and the version number extracted fromstrings.xml
.And then, finally, I can run
ant build-all
from the command line and build all four targets. A little bit more script, just before the end of thebuild-all
target, lists the compiled APK files together for reference: