是否可以设置一个IntelliJ Android项目以与Maven一起使用? [英] Is it possible to set up an IntelliJ Android project to work with Maven?

查看:115
本文介绍了是否可以设置一个IntelliJ Android项目以与Maven一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在IntelliJ Android项目中使用Maven依赖项.有人成功做到了吗?

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

推荐答案

我在Ubuntu 11.10和Mac OS X 10.7.3上均使用IntelliJ IDEA(当前为v11.0.2,内部版本111.277)来处理基于Maven的Android项目,在大多数情况下,它运作良好.将IntelliJ与Android和Maven结合使用的好处是,所有支持都已内置-无需安装额外的IDE插件.

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.

作为可以使用IntelliJ打开的运行良好的Android Maven项目的示例,您可以使用开源 Gaug.es for Android 应用程序(不是GitHub上的所有开发人员都使用IntelliJ,我个人是这样做的-Eclipse也用于该项目).

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).

平稳运行的第一步显然是确保在命令行上仅使用普通Maven即可正确构建项目-尽管问题并没有直接询问,我建议这些先决条件是最低限度的要求:

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(如果您在r15之前下载了SDK,则最好更改整个内容,然后重新下载所有内容,因为目录布局已更改)
  • 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)

如果您可以mvn install项目的父pom,则最好继续使用IntelliJ进行实际工作.

If you can mvn install the parent pom of your project, you're good to move on to actually working with it with IntelliJ.

  • 如果这是第一次使用IntelliJ打开项目,您仍然可以将其作为项目打开,只需在打开项目"对话框中选择父pom.xml文件.
  • 确保IntelliJ知道Java SDK在哪里.即使IntelliJ在Java上运行,它也不会自动检测SDK的位置.你必须告诉它.转到Project StructurePlatform SettingsSDKs,然后编辑Java SDK(如果显示为红色),并为其提供Java 6 SDK的路径.
  • 在同一Project Structure对话框窗口中,将Project SDK设置为适当的Android版本,并在Project下将Project compiler output设置为所需的任何目录名称-进行制作"时需要此值,但也被子模块覆盖以明智地指向Maven的目标"目录.
  • 再次检查IntelliJ是否已解析所有Maven信息.您应该有一个Maven Projects选项卡.按下Reimport All Maven Projects按钮(看起来像两个箭头互相追逐).如果IntelliJ提示您启用自动导入,请继续.现在,您应该在Maven Projects选项卡下至少列出一个模块,如果还有集成测试项目,则应该更多.
  • 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.

在这一点上,您应该具有相当有意义的IDE体验.应该起作用的东西:

At this point, you should have a fairly meaningful IDE experience. Stuff that should work:

  • 没有明显原因,不应用红色下划线标记代码.
  • 应识别所有导入(无论是从Android SDK,Java,您自己的代码还是从apklibs导入).请注意,apklib支持仅在较晚的IntelliJ IDEA v10.5中提供.
  • 重构操作.
  • Force regenerate R.java file应该正确刷新target/generated-sources/r/...下的文件-但这仅在将target/generated-sources/r设置为源路径(即已由Maven生成并由IntelliJ导入)时才起作用.最小的命令行替代方法是从受影响模块的文件夹中的命令行执行mvn android:generate-sources.
  • 从技术上讲,在IDE中对项目进行制作"是可行的,但由于相同的原因,可能再次失败.我在命令行上使用mvn packageinstall调用所有构建.
  • 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.

可能无效的东西:

  • 在IDE中运行单个集成测试(死于ClassNotFoundException: junit.textui.ResultPrinter)

如果IntelliJ感到困惑,通常以下步骤将使您重回正轨:

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

    命令行上的
  • mvn clean install
  • 点击Reimport All Maven Projects按钮
  • mvn clean install on the command line
  • Hit the Reimport All Maven Projects button

一些建议:

  • 您的Maven pom.xml文件应该是项目配置的真实来源.最小化您在项目设置中执行的特定于IntelliJ的配置-在下次从Maven POM重新导入设置时,其中某些内容可能会被删除-并尝试避免将IntelliJ项目文件提交到源代码管理中.

希望有帮助!

这篇关于是否可以设置一个IntelliJ Android项目以与Maven一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆