I want to work with Maven dependencies in an IntelliJ Android project. Has anybody successfully done this?
I use IntelliJ IDEA (currently v11.0.2, build 111.277) on both Ubuntu 11.10 and Mac OS X 10.7.3 for working on Maven-based Android projects, and for the most part it works well. A nice thing about using IntelliJ with Android and Maven is that all support is already built-in – there are no extra IDE plugins to install.
As an example of a good working Android Maven project that can be opened with IntelliJ, you could use the source code of the open-source Gaug.es for Android app by GitHub (not all developers at GitHub use IntelliJ, personally I do – Eclipse is also used to work on this project).
The first step for smooth running is obviously to ensure that your project builds correctly using only plain Maven at the command line – although the question doesn’t directly ask about this, I’d advise these pre-requisites as a bare minimum:
- Java 6
- Maven v3.0.3
- android-maven-plugin v3.1.1
- Android SDK r16 (if you downloaded your SDK before r15, you’re probably better off nuking the entire thing and downloading it all again, as the directory layout has changed)
If you can
mvn install the parent pom of your project, you’re good to move on to actually working with it with IntelliJ.
- If this is the first time opening the project with IntelliJ, you can still open it as a project, you just need to select the parent pom.xml file in the ‘Open Project’ dialog.
- Ensure IntelliJ knows where the Java SDK is. IntelliJ, even though it’s running on Java, doesn’t auto-detect the location of the SDKs. You have to tell it. Go
SDKsand then edit the Java SDK if it’s showing as red, giving it the path of your Java 6 SDK.
- In the same
Project Structuredialog window, set the
Project SDKto the appropriate Android version, and under
Project compiler outputto whatever directory name you like – this value is required for doing a ‘Make’, but also overridden to by your submodules to sensiibly point to the Maven ‘target’ directories.
- Double-check that all the Maven information has been parsed by IntelliJ. You should have a
Maven Projectstab. Hit the
Reimport All Maven Projectsbutton (looks like two arrows chasing each other). If IntelliJ prompts you to enable Auto-Import, go for it. You should now have at least one module listed under the
Maven Projectstab, more if you have an integration test project as well.
At this point, you should have a fairly meaningful IDE experience. Stuff that should work:
- There should be no code underlined in red for no readily apparent reason.
- All imports (whether from the Android SDK, Java, your own code or from apklibs) should be recognised. Note that apklib support only came in with late IntelliJ IDEA v10.5.
- Refactor operations.
Force regenerate R.java fileshould correctly refresh the file under
target/generated-sources/r/...– but this only seems to work if
target/generated-sources/ris already set as a source path, ie has been generated by Maven, and imported by IntelliJ. The minimal command line alternative is to execute
mvn android:generate-sourcesfrom the command line in the folder of the affected module.
- Doing a ‘Make’ of the project within the IDE technically works, but again might fail for the same reason. I invoke all my builds using the mvn
installat the command line.
Stuff that might not work:
- Running individual integration tests from the IDE (dies with
If IntelliJ gets confused, the following steps will normally get you back on track:
mvn clean installon the command line
- Hit the
Reimport All Maven Projectsbutton
- Your Maven pom.xml files should be the source of truth for your project configuration. Minimise IntelliJ-specific configuration you do in the project settings- some of it will be liable to get wiped the next time settings are re-imported from the Maven POM – and try to avoid committing IntelliJ project files into source-control.
Hope that helps!
Here the command you can use to create an sampe working template with Maven structure:
mvn archetype:generate \ -DarchetypeArtifactId=android-with-test \ -DarchetypeGroupId=de.akquinet.android.archetypes \ -DarchetypeVersion=1.1.0 \ -DgroupId=com.foo.bar \ -DartifactId=my-android-project \ -Dpackage=com.foo.bar.android
BTW, IntelliJ has built in support for maven, so just open pom.xml from it – and everything will be ok.