是否可以设置一个 IntelliJ Android 项目来与 Maven 一起使用?

发布于 2024-12-05 02:40:34 字数 55 浏览 0 评论 0原文

我想在 IntelliJ Android 项目中使用 Maven 依赖项。有人成功做到这一点吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赢得她心 2024-12-12 02:40:34

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

作为可以使用 IntelliJ 打开的良好运行的 Android Maven 项目的示例,您可以使用开源 Gaug.xml 的源代码。 GitHub 上的 es for Android 应用程序(并非 GitHub 上的所有开发人员都使用 IntelliJ,我个人也使用 IntelliJ - Eclipse 也用于处理此项目)。

平稳运行的第一步显然是确保您的项目在命令行中仅使用普通 Maven 正确构建 - 尽管问题没有直接询问这一点,但我建议至少具备这些先决条件:

  • Java 6
  • Maven v3.0.3
  • android-maven-plugin v3.1.1
  • Android SDK r16(如果您下载了 SDK在r15之前,你可能最好把整个东西都删除然后再次下载,因为目录布局已经改变了)

如果你可以mvn install你的项目的父pom,你就可以继续前进了实际上与 IntelliJ 一起使用它。

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

此时,您应该拥有相当有意义的 IDE 体验。应该有效的东西:

  • 不应该有任何代码无缘无故地用红色下划线。
  • 所有导入(无论是来自 Android SDK、Java、您自己的代码还是来自 apklib)都应该被识别。请注意,apklib 支持仅在最新的 IntelliJ IDEA v10.5 中提供。
  • 重构操作。
  • 强制重新生成 R.java 文件 应该正确刷新 target/ generated-sources/r/... 下的文件 - 但这似乎只有在 target/ 时才有效generated-sources/r 已设置为源路径,即已由 Maven 生成,并由 IntelliJ 导入。最小的命令行替代方法是从受影响模块的文件夹中的命令行执行mvn android:generate-sources
  • 从技术上讲,在 IDE 中对项目进行“Make”是可行的,但由于同样的原因可能会再次失败。我在命令行中使用 mvn packageinstall 调用所有构建。

可能不起作用的东西:

  • 从 IDE 运行单独的集成测试(因 ClassNotFoundException: junit.textui.ResultPrinter 而终止)

如果 IntelliJ 感到困惑,以下步骤通常会让您回到正轨:

  • 在命令行上 mvn clean install
  • 点击重新导入所有 Maven 项目 按钮

一些建议:

  • 您的 Maven pom.xml 文件应该是项目配置的真实来源。最大限度地减少您在项目设置中进行的 IntelliJ 特定配置 - 下次从 Maven POM 重新导入设置时,其中一些配置可能会被擦除 - 并尽量避免将 IntelliJ 项目文件提交到源代码管理中。

希望有帮助!

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!

绅刃 2024-12-12 02:40:34

请参阅maven-android-plugin 入门

您可以使用这里的命令来创建具有 Maven 结构的示例工作模板:

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

顺便说一句,IntelliJ 内置了对 Maven 的支持,因此只需从中打开 pom.xml - 一切都会好起来的。

See Getting Started with the maven-android-plugin.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文