How do I include dependencies in my android librar

2019-06-20 10:00发布

I am writing an android sdk and I now want to distribute it but I am having problems with its dependencies. I am using gradle and android studio.

My sdk has a dependency on volley and gson and I have them added as jars with in my sdk. When ever I try to create an aar or a jar to use within a separate client app I always get a crash when ever my sdk tries to reference volley as it wasnt included in either the aar or jar. Any ideas on how I should be doing this? I have looked into creating fat jars with out much success.

I have also tried using remote dependencies like below but even after a gradle build in the client app both volley and gson are still not included. This is currently what I have in my sdk build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    compile 'com.google.code.gson:gson:2.3'
}

then with in the client build gradle I have

repositories {
    flatDir {
        dirs 'libs'
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile(name:'mysdk-1.0.0', ext:'aar')
}

I am using ./gradlew clean build to build the aar and jar of my sdk Can anyone tell me the correct way to include dependencies in my own lib?

2条回答
Fickle 薄情
2楼-- · 2019-06-20 10:18

In your client, change your dependencies block like so:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile(name:'mysdk-1.0.0', ext:'aar') {
        transitive = true
    }
}

This should pull in your transitive dependencies as well.

查看更多
闹够了就滚
3楼-- · 2019-06-20 10:34

If you wish to include the transitive dependencies of your library in your client app, you'll need to create a proper maven compatible package that contains a pom.xml. This pom will describe the dependencies of your lib, so that client can pull those deps transitively when they include your lib.

See this tutorial: http://blog.blundell-apps.com/locally-release-an-android-library-for-jcenter-or-maven-central-inclusion/

You don't have to upload the artifact anywhere during development, just use your local maven repo as a target, and have mavenLocal() as a repository for your client app. This way, you'll be able to refer to your lib as a standard maven dependency using
compile 'com.mysdk:mysdk:1.0.0'

查看更多
登录 后发表回答