I know that I had written a month or so ago about using vim a bit more heavily instead of an IDE. That was kind of silly. While I did use vim quite a bit for doing things in JavaScript, it just can’t compete with a proper IDE. Long story short, I switched over to Intellij at work for doing some stuff with AngularJS, and finally got over the hump, and really liked it. So, I figured that it was finally time to use Intellij for Android stuff as well, especially since I want to get a couple new projects started to try out some of the new stuff that’s out there for Android. Before I forget, here’s the Google I/O talk on the new build system:

I had spent quite a bit of time writing up a how-to on using Gradle with an Anroid project in Intellij Ultimate 13 EAP, but after sinking a solid 8 hours into trying to get it to work properly, I gave up. While I was able to add a build.gradle file to the project, and run gradle build successfully, I could not get Intellij to pull dependencies from the Gradle build. I’m sure there’s a config somewhere that I am missing, but I simply ran out of time, I can’t really afford to spend more than 8 hours trying to get my environment set up to start a new project. There were several frustrating things about this. First, we were promised that with the new Android Studio, that the IDE would have deep hooks into the Gradle build system, which would solve the problems that we had seen previously in trying to use Maven for Android projects in Eclipse, for example. Second, the new Android compatibility library is much easier to deal with if you’re using Gradle, and without it, you need to jump through a bunch of extra hoops.

The trouble that I was having was that I had based my efforts on a faulty assumption - that Intellij Ultimate would be very similar in handling Android project creation to Android Studio, and should be able to handle the Gradle stuff in the same way as well. This turns out to have been an incorrect assumption. Android project creation is not the same between the two IDEs, and the key difference is that in Android Studio, when you run through the new project wizard, you end up with a Gradle backed project, that’s actually configured correctly to leverage Gradle as expected. If you create a new Android project with the wizard in Intellij, you end up with a project that is near impossible to set up Gradle IDE integration for.

I had run through a bunch of different ways of starting a new Android project, or importing one to Intellij, and was never able to get the Gradle integration working. I won’t go as far as to say that it is impossible, but again, I spent about 8 hours hammering away at it and wasn’t able to get it all the way there. I only managed to get lots of different states of semi-working, but without that all-important IDE integration, which meant that I probably could have used Gradle for building, but not for resolving dependencies.

I added a comment to this bug report for Intellij, asking for better Gradle support for Android projects.

Resulting project

Here are a few notes that I made about the Gradle build files and file structure that Android Studio generates.

Notice how the file layout has a parent directory, and then src/main/ before you start seeing your project’s code. Then, the packages are under the java dir. There are also two build.gradle files.

This is the build.gradle that belongs to the parent. We want to use the Gradle warpper, so we’ll initialize that here:

You can run gradle wrapper from the parent directory to generate the wrapper script and supporting files.

Here’s the settings.gradle that belongs to the parent, all it does is include your project:

The parent’s Gradle files would look a bit different if we had multiple projects being pulled in and compiled. For example, if you were including the Facebook SDK or ActionBarSherlock, you’d have to specify those projects in settings.gradle.

Now, here’s the project’s build.gradle:

Notice that I’m including the appcompat and support libraries as dependencies. I made sure that Gradle was configured to use the wrapper and that auto-import was enabled. Auto-import might be an annoyance if you touch your build files often, but for me, I wanted any changes to build.gradle to be reflected immediately in my project. E.g., if I were to add a dependent library, I’d want to be able to use it immediately without needing to manually kick off a Gradle command.

In Android Studio, this setup gets me up and running pretty quickly, and I was able to start importing things into my code immediately from those support libraries without doing much else. That’s really the beauty of this whole effort, when you’re using Android Studio, the difficult setup stuff is just kind of handled for you, and you can actually use a complex dependency management and build tool without much headache.

  1. recursiverobot posted this