Android Studio:将复杂的 build.xml 迁移到 build.gradle [英] Android Studio: Migrate complex build.xml to build.gradle

查看:62
本文介绍了Android Studio:将复杂的 build.xml 迁移到 build.gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地将我的项目从 eclipse 迁移到了 android studio,并且生成了一个默认的 build.gradle 文件.项目构建正确,我可以部署到调试等.

I have migrated my project from eclipse to android studio successfully and a default build.gradle file has been generated. The project builds correctly and I can deploy to debug etc.

真正的问题是构建发布 APK 文件(从命令行),我曾经有一个自定义的 ant-build(通过命令行调用,而不是在 eclipse 之外).

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).

我的自定义 build.xml 通过以下方式从 SDK 文件夹导入标准 sdk build.xml 文件:

My custom build.xml imported the standard sdk build.xml file from the SDK folder via:

<import file="${sdk.dir}/tools/ant/build.xml" />

所以我在 build.xml 中要做的就是覆盖目标或通过depends"挂钩到它们.

So all I had to do in my build.xml was to override targets or hook into them via "depends".

覆盖示例:

<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

所以总的来说,使用 ant 整个过程非常简单.但是现在谈到迁移到 gradle 我完全不知所措,如何完成我以前在 ant 中所做的事情,具体来说:

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:

  1. 对于 build.xml,我在 ant 构建中导入了一个默认的 build.xml - 某处是否存在标准 build.gradle 文件以便我可以查看定义的任务?
  2. 是否可以覆盖 gradle 任务,如果是:如何?
  3. 我很确定可以在 gradle 中模仿来自 ant 的 depends="..." ,但我不知道如何并且这也需要回答问题 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://ryanharter.com/blog/2013/07/17/migrating-android-projects-to-gradle/

我还从 gradle docs 中阅读了这一章,它根本没有帮助,因为它没有回答问题 1、2 或 3:

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:

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/

我还尝试简单地将默认 build.xml 从 sdk 文件夹复制到我自己的 build.xml 中,然后简单地在 gradle 中导入该 ant 构建文件并执行启动构建的 ant 任务:

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:

<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>

但我开始遇到问题,这表明这不再是一个选项,因为默认的 build.xml 开始抛出错误,这似乎与 SDK 版本有关.而且由于新的 android studio 项目结构,我不得不对原始 sdk build.xml 进行很多处理,因此文件不在预期的位置等...

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. 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工作室.它将为您创建合适的 build.gradle 脚本.
  • 然后,您可以通过执行 gradlew tasks 在以下位置列出任务列表项目的根.
  • 要依赖某些任务,可以使用诸如init.dependsOn anotherTask之类的命令.
    • First, I would suggest you import the legacy project to android studio. It will create the appropriate build.gradle script for you.
    • And then, you can list the task list by executing gradlew tasks at the root of your project.
    • To depend on some tasks, you can use command such as init.dependsOn anotherTask.
    • 这篇关于Android Studio:将复杂的 build.xml 迁移到 build.gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆