Android Studio: transfer complex build.xml to build.gradle

I successfully migrated my project from eclipse to android studio and the default build.gradle file was created. The project is building correctly, and I can deploy it for debugging, etc.

The real problem is creating the release APK files (from the command line) for which I had custom ant -build (called via the command line, not from eclipse).

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

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

So, all I had to do in my build.xml file was to redefine the goals or connect to them through the "depends".

Override Example:

 <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 of adding dependencies:

 <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 the common thing was really simple using ant. But now, when it comes to switching to gradle, I am in a complete loss of how to accomplish what I did earlier in ant, in particular:

  • There is a default build.xml file in the build.xml file that I imported in my ant assembly, somewhere there is a standard build.gradle file, so can I check for specific tasks?
  • Is it possible to override gradle tasks, and if so: how?
  • I'm pretty sure that depend = "..." from ant can be mimicked in gradle, but I have no idea how to do this, I also need the answer to question 1.

I googled a bunch of tutorials, as well as migration tutorials, sort of (unfortunately not affecting my problems):

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 gradle docs that really didn't help at all, because it doesn't answer question 1 or 2 or 3:

http://www.gradle.org/docs/current/userguide/ant.html

Below are some interesting pointers:

http://toastdroid.com/2014/03/28/customizing-your-build-with-gradle/

I also tried just copying the default build.xml file from the sdk folder to my own build.xml file and then just import this ant build file into gradle and execute the ant task that starts 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 began to run into problems that suggested that this was no longer an option, because by default build.xml started throwing errors that seemed to be related to the SDK version. And I had to mess around a lot with the original sdk build.xml due to the new structure of the Android studio project, so the files were not where they were expected, etc ....

+7
android android-studio android-gradle ant gradle
source share
2 answers
  • First I suggest you import the old project into android studio. It will create a suitable build.gradle script for you.
  • You can then list the list of tasks by running gradlew tasks at the root of your project.
  • To depend on some tasks, you can use a command like init.dependsOn anotherTask .
+2
source share

Just for the record: I gave up the idea of ​​using my previous ant -build and completely ported to gradle. Most of the material I need to do can only be achieved by directory structure. You can find detailed information on how I did this in my other question:

Android Studio: Gradle Product Flavors: Defining Custom Properties

Now the second part was to call my Java code, which did some things that I really did not want to port to groovy / gradle. So I figured out how to execute my java class (although I really had to make a jar file from it) from the Gradle script:

Android Studio Gradle: execute static Java method (transition from ANT to Gradle)

If anyone has questions, I will be happy to try and answer them.

0
source share

All Articles