Gradle for Android AAR Depending Upon AAR, Both In The Same Remote Repository?

ε祈祈猫儿з 提交于 2019-11-28 20:15:15

I don't have a public example but I have this scenario successfully setup in a internally-hosted Nexus repository. Here is the setup:

App - Android application project LibraryB - Android library project picasso - Open source library from Square (available on Maven Central) LibraryA - Android library project

App depends on LibraryB and picasso LibraryB depends on LibraryA

Here is the POM for LibraryB (downloaded from Nexus)

<?xml version="1.0" encoding="UTF-8"?>
<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</groupId>
   <artifactId>LibraryB</artifactId>
   <version>0.1</version>
   <packaging>aar</packaging>
   <dependencies>
      <dependency>
         <groupId>com.example</groupId>
         <artifactId>LibraryA</artifactId>
         <version>3.0.1</version>
         <scope>compile</scope>
      </dependency>
      <dependency>
         <groupId>com.squareup.picasso</groupId>
         <artifactId>picasso</artifactId>
         <version>2.1.1</version>
         <scope>compile</scope>
      </dependency>
   </dependencies>
</project>

Here is the build.gradle for LibraryB

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android-library'
apply plugin: 'maven'

version versionProp
group 'com.example'

repositories {
    mavenCentral()
    maven {
        url(exampleReleaseRepoUrl)
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
    }
    release {
        runProguard false
        proguardFile 'proguard-rules.txt'
        proguardFile getDefaultProguardFile('proguard-android.txt')
    }
}

dependencies {
    compile 'com.example:LibraryA:3.0.1'
    compile 'com.squareup.picasso:picasso:2.1.1'
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: uri(exampleReleaseRepoUrl)) {
                authentication(userName: nexusUsername, password: nexusPassword)
            }
            snapshotRepository(url: uri(exampleSnapshotRepoUrl)) {
                authentication(userName: nexusUsername, password: nexusPassword)
            }
        }
    }
}

Here is the POM for LibraryA (downloaded from Nexus)

<?xml version="1.0" encoding="UTF-8"?>
<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</groupId>
   <artifactId>LibraryA</artifactId>
   <version>3.0.1</version>
   <packaging>aar</packaging>
   <dependencies>
      <dependency>
         <groupId>com.android.support</groupId>
         <artifactId>support-v4</artifactId>
         <version>19.0.0</version>
         <scope>compile</scope>
      </dependency>
   </dependencies>
</project>

The build.gradle for LibraryA is very similar to the one for LibraryB above.

The artifacts and POM for LibraryA and LibraryB were uploaded via the following Gradle command

gradle uploadArchives

The build.gradle for App looks like this

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
    maven {
        url(exampleReleaseRepoUrl)
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
    }
    buildTypes {
        release {
            runProguard false
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }
    productFlavors {
        defaultFlavor {
            proguardFile 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.0.0'
    compile 'com.android.support:appcompat-v7:19.0.0'
    compile 'com.example:LibraryB:0.1'
}

If you need any further information let me know.

It appears that my problem has cleared up with Gradle 1.9 and com.android.tools.build:gradle:0.7.+. Leastways, I can no longer reproduce the problem.

Starting from Android Studio 0.4.4 using .AAR dependencies is as straightforward as using .JAR dependencies. Just put it into \libs directory and reference it in build.gradle:

compile files('libs/YOUR_LIBRARY_NAME.aar')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!