7

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

AgentKnopf
  • 4,295
  • 7
  • 45
  • 81
  • Sounds painful. For me it was faster just to create a clean project and copy pasta in all classes, then add the external libraries via the dependencies block, but my applications weren't huge. Basically you have two build.gradle files. One is for the entire project, the other is for the "module" which is essentially a project within the project, so you can create multiple "modules" in case you need to compile an external library yourself. You can add one module to another by adding it as a plugin in the build.gradle of the encasing module. Hope this at least gives you something to google! – G_V Jan 12 '15 at 10:45
  • I think I should have mentioned I already imported my project into android studio and it builds fine for debug purposes :) . But I need to do certain things for a release apk like renaming the file etc. So this is the point i am currently stuck in, because I used to do this in ant. – AgentKnopf Jan 12 '15 at 10:46
  • I'm sure it's somewhere in the myriad pages of the official [guide](http://developer.android.com/tools/publishing/publishing_overview.html), because I've read it before. Can't give you an exact page though, but there are several pdfs out there detailing how to do all these things. Your freedom of what is possible is a bit limited though, for security reasons I think. – G_V Jan 12 '15 at 10:49
  • @G_V I should probably mention, that I am not publishing to the appstore. It really seems to be a gradle-migration issue, because all those things I described worked prefectly with ant, now I just got to get them to work with gradle :) – AgentKnopf Jan 12 '15 at 10:52

2 Answers2

2
  • 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.
Better Shao
  • 455
  • 4
  • 10
  • Thank you for your comment. I have already imported my project into android studio. Android Studio created a build.gradle file which only contains a few lines of code though. That gradlew tasks command is really helpful though, I will try this out! I'll also look into init.dependsOn and if this get's me further (or ideally straight to my goal) I'll be happy to accept this answer - cheers! – AgentKnopf Jan 12 '15 at 10:44
  • P.s. I added the info that my project has already been imported and builds correctly, just not the way i want to when building release apks :) – AgentKnopf Jan 12 '15 at 10:50
  • A follow up question: Now I listed the tasks (so I can see which ones are available - such as "init") and I'd like to do something like: init.dependsOn "ant-task-one" "ant-task-two" "ant-task-three" ... Meaning that before init should be executed, I first want to run all of it's dependencies, but my above syntax is wrong. And I am also confused about where to put those things in the build.gradle. Should it go into the android{...} or elsewhere? Sorry for all the questions, this gradle thing is really different from ant :/ – AgentKnopf Jan 12 '15 at 14:18
  • @Zainodis I haven't tried to use ant in Android Studio heavily. Just share a simple hello world snippet for your reference. `task sayHello << { String hello = "hello world" ant.echo(message:hello) }` It would be better to put it outside the android{...} block, in build.gradle in your app module directory (not the root). – Better Shao Jan 13 '15 at 03:38
  • thanks a lot for that snippet and the hint that it shouldn't go into the android block, that already helps a lot :) ! – AgentKnopf Jan 13 '15 at 07:32
  • I'll accept this answer for now, though I will hopefully be able to also post a comprehensive answer, explaining the whole migration once this is done. – AgentKnopf Jan 26 '15 at 10:52
0

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.

Community
  • 1
  • 1
AgentKnopf
  • 4,295
  • 7
  • 45
  • 81