Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set up an IntelliJ Android project to work with Maven?

I want to work with Maven dependencies in an IntelliJ Android project. Has anybody successfully done this?

like image 890
Alexey Zakharov Avatar asked Sep 17 '11 05:09

Alexey Zakharov


People also ask

Can I use Maven for Android?

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.

Does IntelliJ support Maven project?

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.

How do I add Maven to an existing IntelliJ 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.

Can we create Maven project in Android Studio?

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.


1 Answers

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 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.
  • In the same 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.
  • Double-check that all the Maven information has been parsed by IntelliJ. You should have a 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:

  • 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 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.
  • 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 package or install at the command line.

Stuff that might not work:

  • Running individual integration tests from the IDE (dies with ClassNotFoundException: junit.textui.ResultPrinter)

If IntelliJ gets confused, the following steps will normally get you back on track:

  • mvn clean install on the command line
  • Hit the Reimport All Maven Projects button

Some advice:

  • 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!

like image 200
Roberto Tyley Avatar answered Sep 26 '22 02:09

Roberto Tyley