26

I'm kind of new to 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). What is the most adequate way of making it work with 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.

Any options to combine maven/gradle in Jenkins are also acceptable.

inteloid
  • 747
  • 1
  • 7
  • 19

6 Answers6

54

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.

The rational behind this is that, Google has already release the extra addons in a maven repository compatible directory layout. 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.

<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:

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] ------------------------------------------------------------------------
Jerry Tian
  • 3,439
  • 4
  • 27
  • 29
  • You sir, saved my day! I wonder why this isn't the top answer. Many thanks. – Vidhi Mar 30 '14 at 19:08
  • This is perfect. But I can't seem to replicate the success with appcompat-v7 though – twig Oct 09 '14 at 08:25
  • Someone would know where to find the appcompat-v7 of 22.2.0 version ? I'm having trouble finding the jar archive. http://stackoverflow.com/questions/31943802/is-the-maven-repository-holding-the-same-android-dependencies-as-the-gradle-one – Stephane Aug 15 '15 at 12:39
  • I get this error now: ArtifactTransferException: Failure to transfer com.android.support:support-v4:jar:23.0.1 from file://${env.ANDROID_HOME}/extras/android/m2repository was cached in the local repository, resolution will not be reattempted until the update interval of android-support has elapsed or updates are forced. Original error: Could not transfer artifact com.android.support:support-v4:jar:23.0.1 from/to android-support (file://${env.ANDROID_HOME}/extras/android/m2repository): Repository path /extras/android/m2repository does not exist, and cannot be created. – dessalines Sep 23 '15 at 23:46
  • How come my m2repository is totally empty ? – Stephane Sep 26 '15 at 17:24
  • I could fill up the m2repository directory again by running the android manager and selecting the Android Support Repository. – Stephane Sep 26 '15 at 18:36
  • I also cannot get it to work with appcompat-v7. Actually i cannot get appcompat-v7 to work with Maven any way whatsoever. Has anyone been successful? – MuayThai Oct 28 '15 at 03:28
  • great answer. how would you specify the repository in gradle? – Julian A. Nov 19 '15 at 22:31
  • Can't see any directory named, m2repository in the mentioned path. There is, for example, a annotations directory at the path; but no m2repository dir. Any suggestion would be appreciated. – Amin Jan 23 '16 at 14:12
12

unfortunately those libraries are not in the central maven repository. so you have to use the maven-android-sdk-deployer (link) to install the libraries into your maven repository. then add the needed libraries to your pom.xml file as described in the readme.

sebastian
  • 2,610
  • 2
  • 17
  • 28
  • 2
    I confirm, and have to add this is very annoying that Google doesn't update android library and the support-v4 lib on maven repo. Does google consider that maven and eclipse maven plugin is not adapted to android dev or now? – L. G. Aug 22 '13 at 15:37
10

I've just run into the same problem and ended up deploying in a public gitgub repository.

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:

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

[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:

<dependency>
  <groupId>com.android.support</groupId>
  <artifactId>gridlayout-v7</artifactId>
  <version>18.0.0</version>
  <scope>compile</scope>
</dependency>
kmchugh12
  • 684
  • 6
  • 7
  • I see that you also uploaded gridlayout-v7 but I cannot find a way to add it to my project. Can you please post the dependency I should use? – mariosangiorgio Oct 28 '13 at 15:43
  • 1
    @mariosangiorgio, I've updated the answer to reflect your comment. – kmchugh12 Oct 31 '13 at 14:24
  • The raw github url gives a 400 – Stephane Sep 26 '15 at 17:26
  • The repositories are now bundled with the android tools, much better to use the Android SDK Manager and make sure that you have the support repository installed, then use the example given by @bsorrentino – kmchugh12 Sep 28 '15 at 14:16
10

I've simply used the system scope feature provided by 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>
bsorrentino
  • 1,413
  • 11
  • 19
  • 1
    this must be the top answer – RusAlex Feb 11 '15 at 19:07
  • I get a No resource found that matches the given name 'Theme.AppCompat.Light.NoActionBar'. I have in my pom.xml files the 3 dependencies with the following system paths ${env.ANDROID_HOME}/extras/android/support/design/libs/android-support-design.jar ${env.ANDROID_HOME}/extras/android/support/v7/appcompat/libs/android-support-v7-appcompat.jar ${env.ANDROID_HOME}/extras/android/support/v4/android-support-v4.jar – Stephane Sep 26 '15 at 17:38
  • the SystemPath seems correct, check out the answers [here](http://stackoverflow.com/questions/17870881/cant-find-theme-appcompat-light-for-new-android-actionbar-support) that could help you – bsorrentino Sep 28 '15 at 08:37
2

I would recommend keep Android artefacts as part of your major MAVEN repository. 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.

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.

Sergio Kosik
  • 192
  • 2
  • 6
2

Thanks to Google. You can now do this

  • Make sure that the repositories section includes a 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"
}
Qamar
  • 4,959
  • 1
  • 30
  • 49