Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 997
AgentKnopf Avatar asked Jan 12 '15 10:01

AgentKnopf


1 Answers

  • 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.
like image 75
Better Shao Avatar answered Oct 19 '22 02:10

Better Shao