I want to work with Maven dependencies in an IntelliJ Android project. Has anybody successfully done this?
In order to use Maven to build an Android application, you will need to configure the Maven Android Plugin within your pom. xml file. Android applications are deployed to the device as an apk file, not a jar. You must specify this in the the packaging configuration.
IntelliJ IDEA supports a fully-functional integration with Maven that helps you automate your building process. You can easily create a new Maven project, open and sync an existing one, add a Maven support to any existing IntelliJ IDEA project, configure and manage a multi-module project.
Add Maven support Open an existing project, for example, a Java project. In the Project tool window, right-click your project and select Add Framework Support. In the dialog that opens, select Maven from the options on the left and click OK.
Setting up Android Studio to work with mavenTo conveniently work on a maven project with Android studio, we have to set it to automatically import Maven projects, so that it notices changes to the pom. xml and updates it's dependencies.
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:
If you can mvn install
the parent pom of your project, you're good to move on to actually working with it with IntelliJ.
Project Structure
, Platform Settings
, SDKs
and then edit the Java SDK if it's showing as red, giving it the path of your Java 6 SDK.Project Structure
dialog window, set the Project SDK
to the appropriate Android version, and under Project
set the Project compiler output
to 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.Maven Projects
tab. Hit the Reimport All Maven Projects
button (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 Projects
tab, 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:
Force regenerate R.java file
should correctly refresh the file under target/generated-sources/r/...
- but this only seems to work if target/generated-sources/r
is 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-sources
from the command line in the folder of the affected module.package
or install
at the command line.Stuff that might not work:
ClassNotFoundException: junit.textui.ResultPrinter
)If IntelliJ gets confused, the following steps will normally get you back on track:
mvn clean install
on the command lineReimport All Maven Projects
buttonSome advice:
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With