简体   繁体   English

如何在我的android库中包含依赖项

[英]How do I include dependencies in my android library

I am writing an android sdk and I now want to distribute it but I am having problems with its dependencies. 我正在编写一个android sdk,我现在想分发它,但我的依赖项存在问题。 I am using gradle and android studio. 我正在使用gradle和android studio。

My sdk has a dependency on volley and gson and I have them added as jars with in my sdk. 我的sdk依赖于volley和gson,我将它们添加为我的sdk中的jar。 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. 当我尝试在单独的客户端应用程序中创建aar或jar时,我总是会遇到崩溃,因为我的sdk试图引用齐射,因为它没有包含在aar或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. 我也尝试过使用下面的远程依赖项,但即使在客户端应用程序中进行gradle构建之后,仍然不包括volley和gson。 This is currently what I have in my sdk build.gradle 这是我目前在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 然后在客户端构建gradle我有

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? 我正在使用./gradlew clean build来构建我的sdk的aar和jar任何人都可以告诉我在我自己的lib中包含依赖项的正确方法吗?

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. 如果您希望在客户端应用程序中包含库的传递依赖项,则需要创建包含pom.xml的正确maven兼容包。 This pom will describe the dependencies of your lib, so that client can pull those deps transitively when they include your lib. 这个pom将描述你的lib的依赖关系,这样客户端可以在它们包含你的lib时传递这些deps。

See this tutorial: http://blog.blundell-apps.com/locally-release-an-android-library-for-jcenter-or-maven-central-inclusion/ 请参阅本教程: 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. 您不必在开发过程中的任何位置上传工件,只需使用本地maven存储库作为目标,并将mavenLocal()作为客户端应用程序的存储库。 This way, you'll be able to refer to your lib as a standard maven dependency using 这样,您就可以将lib作为标准maven依赖项引用
compile 'com.mysdk:mysdk:1.0.0'

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. 这也应该吸引你的传递依赖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM