[英]Android Studio: Migrate complex build.xml to build.gradle
I have migrated my project from eclipse to android studio successfully and a default build.gradle file has been generated. 我已成功将我的项目从eclipse迁移到android studio,并生成了一个默认的build.gradle文件。 The project builds correctly and I can deploy to debug etc.
项目构建正确,我可以部署到调试等。
The real problem though is building release APK files (from the command line) for which I used to have a custom ant-build (called via command line, not out of eclipse). 真正的问题是构建发布APK文件(从命令行)我曾经有一个自定义的ant-build(通过命令行调用,而不是eclipse)。
My custom build.xml imported the standard sdk build.xml file from the SDK folder via: 我的自定义build.xml通过以下方式从SDK文件夹导入标准sdk build.xml文件:
<import file="${sdk.dir}/tools/ant/build.xml" />
So all I had to do in my build.xml was to override targets or hook into them via "depends". 所以我在build.xml中所做的就是覆盖目标或通过“depends”挂钩它们。
An example for an override: 覆盖的示例:
<target name="-set-release-mode" depends="-set-mode-check">
<!--Here I rename my output apk files to something like: myapp-versionFromAndroidManifest.apk -->
</target
And an example for adding dependency: 以及添加依赖项的示例:
<target name="-pre-build" depends="clean">
<!-- my custom code where I copy resource files, process command line arguments
use xpath to extract things like the version from the AndroidManifest.xml and
write them into custom xml files etc... -->
</target
So overall the whole affair was really simple using ant. 总的来说整个事件使用蚂蚁非常简单。 But now when it comes to migrating to gradle I am at a total loss, how to accomplish the same that I previously did in ant, specifically:
但是现在谈到迁移到gradle我完全失去了,如何完成我之前在ant中所做的那样,具体来说:
I googled a bunch of tutorials, also migration tutorials like (which unfortunately didn't address my issues): 我搜索了一堆教程,还有迁移教程(很遗憾没有解决我的问题):
http://keepsafe-engineering.tumblr.com/post/87818767721/migrating-android-app-from-ant-to-gradle http://keepsafe-engineering.tumblr.com/post/87818767721/migrating-android-app-from-ant-to-gradle
http://ryanharter.com/blog/2013/07/17/migrating-android-projects-to-gradle/ http://ryanharter.com/blog/2013/07/17/migrating-android-projects-to-gradle/
I also read this chapter from the gradle docs which didn't really help at all, because it does not answer question 1 or 2 or 3 for that matter: 我还从gradle文档中读到了本章,这些文档根本没有帮助,因为它没有回答问题1或2或3:
http://www.gradle.org/docs/current/userguide/ant.html http://www.gradle.org/docs/current/userguide/ant.html
The following gave some interesting pointers though: 以下给出了一些有趣的指示:
http://toastdroid.com/2014/03/28/customizing-your-build-with-gradle/ http://toastdroid.com/2014/03/28/customizing-your-build-with-gradle/
I also tried simply copying the default build.xml from the sdk folder into my own build.xml and then simply importing that ant build file in gradle and executing the ant task that kicks of the build: 我还尝试简单地将sdk文件夹中的默认build.xml复制到我自己的build.xml中,然后简单地在gradle中导入该ant构建文件并执行构建踢的ant任务:
<target name="release"
depends="-set-release-mode, -release-obfuscation-check, -package, -post-package, -release-prompt-for-password, -release-nosign, -release-sign, -post-build"
description="Builds the application in release mode.">
</target>
but I started running into problems that suggested this is not an option anymore, since the default build.xml started throwing errors, that seemed related to the SDK version. 但是我开始遇到问题,这表明这不再是一个选项,因为默认的build.xml开始抛出错误,这似乎与SDK版本有关。 And I had to mess around a lot with the original sdk build.xml because of the new android studio project structure, so files were not where they were expected etc...
由于新的android工作室项目结构,我不得不使用原始的sdk build.xml乱用很多东西,所以文件不是他们期望的地方等等......
gradlew tasks
at the root of your project. gradlew tasks
来列出任务列表。 init.dependsOn anotherTask
. init.dependsOn anotherTask
等命令。 Just for the record: I dumped the idea of using my previous ant-build and completely migrated to gradle. 只是为了记录:我抛弃了使用我之前的ant-build并完全迁移到gradle的想法。 Most of the stuff I needed to do could be achieved just by the directory structure.
我需要做的大部分工作都可以通过目录结构来实现。 You can find details on how I did that in my other question:
你可以在我的另一个问题中找到我如何做到这一点的细节:
Android Studio: Gradle Product Flavors: Define custom properties Android Studio:Gradle Product Flavors:定义自定义属性
Now the second part was to call my java code that did some stuff I really did not want to migrate to groovy/gradle. 现在第二部分是调用我的java代码做了一些我真的不想迁移到groovy / gradle的东西。 Thus I figured out how to execute my java class (albeit I actually had to make a jar file out of it) from the gradle script:
因此,我想出了如何从gradle脚本执行我的java类(尽管我实际上必须从中创建一个jar文件):
Android Studio Gradle: Execute static Java Method (Migration from ANT to Gradle) Android Studio Gradle:执行静态Java方法(从ANT迁移到Gradle)
If anyone has questions, I'll be happy to try my best and answer them. 如果有人有疑问,我会很乐意尽我所能并回答他们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.