12
votes

I'm working on a project which includes two Android library projects. When I upload these libraries to my Maven repo (Nexus), the generated pom doesn't include a <type>aar</type> element in the dependency.

Here's my dependency tree

* App1
 \
  * lib1
  |\
  | * lib2
  * Other libs

You can see in this diagram that my app depends on lib1, which depends on lib2. Both of the libraries are Android library projects, thus AARs.

Lib1/build.gradle

apply plugin: 'com.android.library'
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    provided project(':lib2')

    compile 'com.squareup.picasso:picasso:2.3.3'
}

Lib2/build.gradle

apply plugin: 'com.android.library'
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion 19
        versionCode 1
        versionName '1.0'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.squareup.retrofit:retrofit:1.6.1'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
}

Notice how Lib1 includes Lib2 as a project() dependency.

Now, when I deploy this using Chris Banes' excellent gradle-mvn-push plugin, everything works as expected. There is only one oddity that I notice in the generated pom.xml.

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>lib2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    ...
</dependencies>

Notice that the mavenDeployer plugin left out the <type>aar</type> element in the dependency when it generated the pom.

In my app project (which is a maven project), I have this dependency listed:

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>lib1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>aar</type>
    </dependency>
    ...
</dependencies>

When trying to build this with Maven, I get the following error.

Could not resolve dependencies for project com.example:app:apk:1.0.0-SNAPSHOT: The following artifacts could not be resolved: com.example:lib2:jar:0.0.1-SNAPSHOT

Notice how it's looking for a jar version of the library. I believe, since the mavenDeployer plugin isn't generated the <type>aar</type> in the dependencies list of the generated pom, maven is defaulting to looking for a jar.

Does anyone know how to build Android library projects that include transient dependencies with Gradle?

2

2 Answers

0
votes

Just add the aar to the pom with any text editor. After that, open a console into the project's root (where the pom is located) and run "mvn clean install". The aar artifact will be generated and deployed to your maven repo.

In any case, it's far easier to create aar libraries with Maven. Just create a Maven Project using any android library archetype (The de.akquinet ones are pretty good, but you must use a plugin version between 3.8.2 and 3.9, http://mvnrepository.com/artifact/de.akquinet.android.archetypes). Just create the project, add the libraries and then run the command "mvn clean install" to install them into your local maven directory. After that, just use them as normal aar dependencies.

0
votes

Try gradle-fury. It fixes all this...ehm... stuff. Specifically the invalid and/or inaccurate pom's that are generated by gradle. Confirmed working solution to publish to sonatype oss/maven central. No manual pom edits, pgp signature support and much much more.

Site: https://github.com/gradle-fury/gradle-fury

Specifically, you need the maven-support gradle script which does all the work for you. All the stuff that's normally in the pom is stored in gradle.properties. Support android variants too, war files, (soon fixes for distZip), javadocs and source jars and more.

Disclaimer, i work on it

Edit: one more tidbit of information. Although fury does inject dependencies and correct typing for AAR libraries, the gradle-android plugin (the stuff google makes) does not honor the dependency section and ignores it. Basically transitive dependencies of AAR projects isn't supported. No idea why