Paul
Paul

Reputation: 4530

Android Library - Why aren't my dependencies being downloaded? (Gradle/Maven)

I'm trying to figure out how to distribute an Android Library project to some beta users, but I'm running into a few problems when I distribute it and use it in a sample project. I'm trying to distribute an AAR file.

Everything in my sample project compiles fine, except I get an error: java.lang.NoClassDefFoundError: com.google.gson.Gson when I call a piece of my code that uses GSON.

My build.gradle file for my library looks like this:

apply plugin: 'android-library'
apply plugin: 'maven'

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: exampleMavenUrl ) {
                authentication(userName: exampleMavenUser, password: exampleMavenPassword)
            }
            pom.groupId = 'com.example.android'
            pom.artifactId = 'example-api'
            pom.version = '0.1-SNAPSHOT'
            pom.packaging = 'aar'
        }
    }
}


android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
        versionCode 1
        versionName "0.1"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.squareup.okhttp:okhttp:1.3.0'

    compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
    compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
}

When I run: gradlew clean uploadArchives it successfully uploads to my snapshot nexus repository.

Referencing it in my sample project's build.gradle:

apply plugin: 'android'

repositories {
    mavenCentral()
    maven(){
        url "http://repos.example.com/nexus/content/repositories/android-snapshot"
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        packageName "com.example.sample.app"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:19.+'
    compile('com.example.android:example-api:0.1-SNAPSHOT')
}

Here's the pom file that gradle generates and uploads, which lives at something like:

http://repos.example.com/nexus/content/repositories/android-snapshot/com/example/android/example-api/0.1-SNAPSHOT/example-api-0.1-20140602.145158-1.pom

which sits right next to the aar file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.android</groupId>
    <artifactId>example-api</artifactId>
    <version>0.1-SNAPSHOT</version>
    <packaging>aar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework.android</groupId>
            <artifactId>spring-android-rest-template</artifactId>
            <version>1.0.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp</groupId>
            <artifactId>okhttp</artifactId>
            <version>1.3.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.android</groupId>
            <artifactId>spring-android-core</artifactId>
            <version>1.0.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

When I run gradlew androidDependencies, it only lists the ones I have included locally in my library folder.

:app:androidDependencies
debug
\--- com.example.android:example-api:0.1-SNAPSHOT
     +--- LOCAL: android-logging.jar
     +--- LOCAL: Context-Core.jar
     \--- LOCAL: Context-Location.jar

debugTest
No dependencies

release
\--- com.example.android:example-api:0.1-SNAPSHOT
     +--- LOCAL: android-logging.jar
     +--- LOCAL: Context-Core.jar
     \--- LOCAL: Context-Location.jar

I'm very very new to publishing libraries, so there's a good chance I've done something completely wrong here. Any pointers would be helpful.

Edit: Here's (part of) the output from gradle dependencies -- it shows that the remote dependencies are listed, but I'm still getting the runtime error!

compile - Classpath for compiling the main sources.
+--- com.android.support:support-v4:19.+ -> 19.1.0
\--- com.example.android:example-api:0.1-SNAPSHOT
     +--- org.springframework.android:spring-android-rest-template:1.0.1.RELEASE
     |    \--- org.springframework.android:spring-android-core:1.0.1.RELEASE
     +--- com.google.code.gson:gson:2.2.4
     +--- com.squareup.okhttp:okhttp:1.3.0
     |    \--- com.squareup.okhttp:okhttp-protocols:1.3.0
     \--- org.springframework.android:spring-android-core:1.0.1.RELEASE

Thanks!

Upvotes: 0

Views: 1023

Answers (1)

Paul
Paul

Reputation: 4530

I'm not sure exactly why, but NEW projects created worked like this with no problems:

compile('com.example.android:example-api:0.1-SNAPSHOT')

But when using it an older project, I had to reference it like this in order for the AAR to be downloaded and the references to be pulled.

compile('com.example.android:example-api:0.1-SNAPSHOT'){
    transitive = true
}

Upvotes: 1

Related Questions