简体   繁体   English

使用maven安装Android支持库

[英]Android support library setup with maven

I'm kind of new to Android. 我对Android有点新意。

I'm setting up a maven module for android application (it's inside an existing multi-module project), which uses Google Support Libraries (v4, v7, v13). 我正在为Android应用程序设置一个maven模块(它在一个现有的多模块项目中),它使用Google支持库(v4,v7,v13)。 What is the most adequate way of making it work with maven. 什么是使它与maven一起工作的最合适的方法。

I want my project to be able to build with Jenkins (Maven), so I can't link any IDE specific projects or any means of non-maven dependencies. 我希望我的项目能够使用Jenkins(Maven)构建,因此我无法链接任何IDE特定项目或任何非maven依赖项。

Any options to combine maven/gradle in Jenkins are also acceptable. 在Jenkins中组合maven / gradle的任何选项也是可以接受的。

Besides the maven-android-sdk-deployer , if you always have Android SDK installed with Google extras like me, you can define a local repository in your projects POM file and let maven download the dependencies from this local one. 除了maven-android-sdk-deployer之外 ,如果你总是安装了像我这样的Google附加软件的Android SDK,你可以在你的项目POM文件中定义一个本地存储库,让maven从这个本地存储库下载依赖项。

The rational behind this is that, Google has already release the extra addons in a maven repository compatible directory layout. 这背后的理由是,Google已经在maven存储库兼容的目录布局中发布了额外的插件。 For my own machine, it is like this: 对于我自己的机器,它是这样的:

jerry-mac-mini:android jerry$ pwd
/Users/jerry/adt-bundle-mac-x86_64-20130917/sdk/extras/android
jerry-mac-mini:android jerry$ tree m2repository
m2repository
├── NOTICE.txt
├── com
│   └── android
│       └── support
│           ├── appcompat-v7
│           │   ├── 18.0.0
│           │   │   ├── appcompat-v7-18.0.0.aar
│           │   │   ├── appcompat-v7-18.0.0.aar.md5
│           │   │   ├── appcompat-v7-18.0.0.aar.sha1
│           │   │   ├── appcompat-v7-18.0.0.pom
│           │   │   ├── appcompat-v7-18.0.0.pom.md5
│           │   │   └── appcompat-v7-18.0.0.pom.sha1
│           │   ├── 19.0.0
│           │   │   ├── appcompat-v7-19.0.0.aar
│           │   │   ├── appcompat-v7-19.0.0.aar.md5
.............

So, I just need to put some extra lines in my POM file like this, and the "env.ANDROID_HOME" is the environment variable pointed to Android SDK installation path. 所以,我只需要在我的POM文件中添加一些额外的行,并且“env.ANDROID_HOME”是指向Android SDK安装路径的环境变量。

<repositories>
    <repository>
        <id>android-support</id>
        <url>file://${env.ANDROID_HOME}/extras/android/m2repository</url>
    </repository>
</repositories>
    ......
<dependency>
    <groupId>com.android.support</groupId>
    <artifactId>support-v4</artifactId>
    <version>19.0.1</version>
</dependency>

After these steps, Maven in my ADT enabled Eclipse and console are both happily to resolve the desired dependencies, like this: 完成这些步骤后,我的ADT中的Maven启用了Eclipse和控制台,他们都很乐意解决所需的依赖关系,如下所示:

jerry-mac-mini:android-app-project jerry$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building android-app-project 0.0.5-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: file:///Users/jerry/adt-bundle-mac-x86_64-20130917/sdk/extras/android/m2repository/com/android/support/support-v4/19.0.1/support-v4-19.0.1.pom
Downloaded: file:///Users/jerry/adt-bundle-mac-x86_64-20130917/sdk/extras/android/m2repository/com/android/support/support-v4/19.0.1/support-v4-19.0.1.pom (403 B at 28.1 KB/sec)
Downloading: file:///Users/jerry/adt-bundle-mac-x86_64-20130917/sdk/extras/android/m2repository/com/android/support/support-v4/19.0.1/support-v4-19.0.1.jar
Downloaded: file:///Users/jerry/adt-bundle-mac-x86_64-20130917/sdk/extras/android/m2repository/com/android/support/support-v4/19.0.1/support-v4-19.0.1.jar (621 KB at 16783.8 KB/sec)
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ android-app-project ---
[INFO] team.apollo:android-app-project:apk:0.0.5-SNAPSHOT
[INFO] +- com.google.android:android:jar:2.3.3:provided
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.0.1:provided
[INFO] |  |  +- org.apache.httpcomponents:httpcore:jar:4.0.1:provided
[INFO] |  |  \- commons-codec:commons-codec:jar:1.3:provided
[INFO] |  +- org.khronos:opengl-api:jar:gl1.1-android-2.1_r1:provided
[INFO] |  +- xerces:xmlParserAPIs:jar:2.6.2:provided
[INFO] |  \- org.json:json:jar:20080701:provided
[INFO] +- com.android.support:support-v4:jar:19.0.1:compile
[INFO] +- com.google.code.gson:gson:jar:2.2.2:compile
[INFO] +- de.mindpipe.android:android-logging-log4j:jar:1.0.3:compile
[INFO] \- log4j:log4j:jar:1.2.16:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.339s
[INFO] Finished at: Mon Mar 24 14:32:00 CST 2014
[INFO] Final Memory: 10M/325M
[INFO] ------------------------------------------------------------------------

unfortunately those libraries are not in the central maven repository. 遗憾的是,这些库不在中央maven存储库中。 so you have to use the maven-android-sdk-deployer ( link ) to install the libraries into your maven repository. 所以你必须使用maven-android-sdk-deployer( 链接 )将库安装到你的maven存储库中。 then add the needed libraries to your pom.xml file as described in the readme. 然后按照自述文件中的说明将所需的库添加到pom.xml文件中。

I've just run into the same problem and ended up deploying in a public gitgub repository. 我刚遇到同样的问题,最终部署在公共gitgub存储库中。

If you still need to use it, you can do one of the following: 如果仍需要使用它,则可以执行以下操作之一:

Add the github repository to the repositories section in your pom.xml: 将github存储库添加到pom.xml中的存储库部分:

<repository>
  <id>android.support-mvn-repo</id>
  <url>https://raw.github.com/kmchugh/android.support/mvn-repo</url>
  <snapshots>
    <enabled>true</enabled>
    <updatePolicy>daily</updatePolicy>
  </snapshots>
</repository>

Then you can add the dependency as follows: 然后您可以添加依赖项,如下所示:

<!-- Android Support Libraries -->
<dependency>
  <groupId>uk.co.icatalyst</groupId>
  <artifactId>android-support-v4</artifactId>
  <version>18</version>
  <scope>compile</scope>
</dependency>

Or, fork the repository, then you can use your own URL for the repository URL. 或者,分叉存储库,然后您可以使用自己的URL作为存储库URL。

[Edit] I've updated the libraries, an alternative dependency would be: [编辑]我已经更新了库,另一种依赖是:

<dependency>
  <groupId>com.android.support</groupId>
  <artifactId>support-v4</artifactId>
  <version>18.0.0</version>
  <scope>compile</scope>
</dependency>

And for the gridlayout: 对于gridlayout:

<dependency>
  <groupId>com.android.support</groupId>
  <artifactId>gridlayout-v7</artifactId>
  <version>18.0.0</version>
  <scope>compile</scope>
</dependency>

I've simply used the system scope feature provided by maven 我只是使用了maven提供的系统范围功能

<dependency>
   <groupId>com.google.android</groupId>
   <artifactId>support-v4</artifactId>
   <version>19.0.1</version>
   <scope>system</scope>
   <systemPath>${env.ANDROID_HOME}/extras/android/support/v4/android-support-v4.jar</systemPath>
</dependency>

I would recommend keep Android artefacts as part of your major MAVEN repository. 我建议将Android artefacts保留为主要MAVEN存储库的一部分。 Using such approach you will escape additional configurations and as a result any mishmashes. 使用这种方法,您将逃避其他配置,因此任何混乱。 Then you will be able use simple POM files for all your client side projects. 然后,您将能够为所有客户端项目使用简单的POM文件。

To do this or execute command like this: 要执行此操作或执行如下命令:

mvn install:install-file -DgroupId=com.google.android -Dversion=23.1.1 -Dpackaging=jar -DartifactId=support-v13 -Dfile=support-v13-23.1.1-sources.jar

or just copy ./extras/android/m2repository/com/android/ into ./rootMavenRepo/com/google/android of your repository. 或者只是将./extras/android/m2repository/com/android/复制到存储库的./rootMavenRepo/com/google/android中。

Thanks to Google. 感谢Google。 You can now do this 你现在可以这样做

  • Make sure that the repositories section includes a maven 确保存储库部分包含maven

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}
  • Add the support library to the dependencies section 将支持库添加到依赖项部分

dependencies {
    ...
    compile "com.android.support:support-core-utils:25.3.1"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM