I've just upgraded to the latest IntelliJ IDEA 10 preview to check if Android support has improved. One particular issue I had for the last half year was that the Maven features of IntelliJ had no support at all for an Android project facet. Or vice versa; the result was that developing an Android application with Maven and IntelliJ was a huge pain. This is my bug report with Jetbrains that finally triggered some changes and it should be a usable combination now.
There is no documentation how you best start an Android project with Maven and IntelliJ, so I thought I'd write down how I got it working to save you a few hours.
First create a regular Android Maven project, either following
these steps or better
my HelloWorld project skeleton. I've already cleaned up the project root directory and moved the
Android resources/overlays/whatever into the standard Maven project layout under
and configured this in the
pom.xml and also set the
ANDROID_HOME environment variable (if you
haven't already during your SDK install), so you can call Maven from the commandline. You should now already
be able to build and package the project using
mvn clean package. Don't continue if this fails, try to
get the Android & Maven combination working first without the IDE.
Start IntelliJ and create a new project:
Pick your project root directory next and you can also enable automatic import of Maven projects so
changes to your
pom.xml will be automatically detected by IntelliJ.
If you are now trying to compile in IntelliJ with Build > Make Project, you will hit the first problem: "Package R does not exist":
(Rant: All of this is so complex because the Google clowns decided that they need a code
generator in the build lifecycle of an Android application. The reasoning is that it is soooo
much better to write GUI code in XML files, without a schema, compared to
plain type-safe, auto-completed, easily compilable Java. And no, you are too stupid for
setContentView("myLayoutKey"), it has to be
setContentView(R.generated.code). Yes, all of the build and packaging is
so complicated because the average developer can't type a string correctly and oh no! it would
be a runtime error if you don't! We can't have that, everyone has to suffer, on every compile run.
If it would be a string, regular
javac and some post-compile
packaging would be good enough to build a deployable Android app, no code generator needed. Look at
diagram of the build process, do you realize how much simpler this would be without the
crap at the top right? This is by far the most annoying thing about Android and I almost
stopped learning it when I saw this diagram for the first time.)
The problem is that IntelliJ is supposed to run the code generator that produces
you change any of the XML files. I could never get it to do that, no matter what files I edited or buttons
I clicked. Luckily the IntelliJ programmers knew this, they added a setting
that (I'm guessing) runs a Maven goal to generate
R.java, you just have to enable it by
selecting "Copy resources from Maven artifacts before Make", disabling the built-in AAPT generator:
(What does "Copy from Maven artifacts' mean? Should this be 'Running a Maven goal'? Also, it's not a compiler, it only generates a single R.java file with hilarious pointers!)
After that change the "missing import" went away and I could build the project with IntelliJ. Too bad I now have to wait a few seconds on every compile run for the code generator but developer time obviously is cheap.
If you want to execute Maven goals of your project from within IntelliJ you have to set the SDK environment variable in the Maven Runner settings:
Finally, this is a Run Configuration that will build and deploy your application on a device or emulator:
Some of these issues can probably be fixed for the IntelliJ 10 final release. Next on my list are testing and remote debugging, let's see if that works out of the box at least.
Although my setup works with a Maven-generated R.java, waiting for the Maven background process on every compile is quite annoying. Furthermore, although a Make of the project in the IDE works now, I'm still sporadically seeing the R class missing in source code (red font, as shown above in the source). My next attempt is to fix the source paths of the project:
I've included the
target/generated-sources/r as a regular source directory in the
module. So far this seems to fix the problem and I get the green light on my source and Make works
as well. Of course now IntelliJ consumes 100% CPU immediately and forever when I open my project, but
this is probably another new bug in build 98.402. Grrrrrrr....
The bug report with Jetbrains is being tracked here.
Just upgraded to the latest build of IDEA 10, 98.486. This fixes the generated sources path and you can now download my HelloWorld Maven skeleton, create a new project with an "external model" and everything builds and deploys fine. The only thing was that the Logcat window wouldn't be available immediately, I had to close and reopen the new project once for the tab to become visible.
My next problem is attaching Android "Javadoc" to the project module, so I can lookup the API documentation.
I've tried attaching
$ANDROID_HOME/doc/reference/ but so far no luck.