简体   繁体   English

Android Studio:将复杂的build.xml迁移到build.gradle

[英]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中所做的那样,具体来说:

  1. For the build.xml there is a default build.xml that I imported in my ant build - does a standard build.gradle file exist somewhere so I can check out the defined tasks? 对于build.xml,我在我的ant构建中导入了一个默认的build.xml - 标准的build.gradle文件是否存在于某处,以便我可以查看已定义的任务?
  2. Is it possible to override gradle tasks and if yes: how? 是否可以覆盖gradle任务,如果是,如何?
  3. I am pretty certain the depends="..." from ant can be mimicked in gradle but I have no idea how and that would also require an answer to question 1. 我非常肯定来自蚂蚁的depends =“......”可以在gradle中模仿,但我不知道怎么样,这也需要回答问题1。

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乱用很多东西,所以文件不是他们期望的地方等等......

  • First, I would suggest you import the legacy project to android studio. 首先,我建议你将遗留项目导入android studio。 It will create the appropriate build.gradle script for you. 它将为您创建适当的build.gradle脚本。
  • And then, you can list the task list by executing gradlew tasks at the root of your project. 然后,您可以通过在项目的根目录执行gradlew tasks来列出任务列表。
  • To depend on some tasks, you can use command such as 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM