简体   繁体   中英

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

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

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

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:

  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?
  2. Is it possible to override gradle tasks and if yes: how?
  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.

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/

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/

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>

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

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

Just for the record: I dumped the idea of using my previous ant-build and completely migrated to 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

Now the second part was to call my java code that did some stuff I really did not want to migrate to 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:

Android Studio Gradle: Execute static Java Method (Migration from ANT to Gradle)

If anyone has questions, I'll be happy to try my best and answer them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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