воскресенье, 10 июля 2016 г.

How to publish library on Android Studio to AAR with dependencies through local repository.

On your library build.gradle put this:
publishing {
    publications {
        libary(MavenPublication) {
            groupId 'com.libary.api.android'
            artifactId 'libary'
            version '0.1'            
            artifact("$buildDir/outputs/aar/library-release.aar")

            pom.withXml {           
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.compile.allDependencies.each { dp->
                    if (dp instanceof ModuleDependency) {
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', dp.group)
                        dependencyNode.appendNode('artifactId', dp.name)
                        dependencyNode.appendNode('version', dp.version)
                    }
                }
            }
        }
    }
    repositories {
        maven {
            url "c:/Bit/AndroidExample/build/repo"        }
    }
}
Execute this on the folder where project with library is located:
gradlew clean build publish
Now folder repo is created with our library in .aar format where our class builded and packed on .zip format. Also there is .pom file with our dependencies.

On your application that should use this library put this to the project build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'  }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "c:/path_to_local_repo/repo"        }
    }
}

On the app build.gradle put this:
allprojects {
    repositories {
        jcenter()
        flatDir {
            dirs 'libs'        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'    compile 'com.android.support:appcompat-v7:23.4.0'  
    compile ('com.libary.api.android:libary:0.1@aar'){transitive=true}
}

Useful links: