Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 625
glogic Avatar asked Sep 02 '14 09:09

glogic


People also ask

What are Android dependencies?

In Android Studio, dependencies allows us to include external library or local jar files or other library modules in our Android project. For example: Suppose I want to show some images in ImageView. But I'm using Glide Library to enhance the smoothness of application.

Where is dependencies file in Android Studio?

Go to File > Project structure in Android Studio. Select the app module in the Modules list on the left. Select the Dependencies tab.

Where are project dependencies declared in an Android project?

The top-level build.gradle file, located in the root project directory, defines dependencies that apply to all modules in your project. By default, the top-level build file uses the plugins block to define the Gradle dependencies that are common to all modules in the project.


2 Answers

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'

like image 180
Balazs Banyai Avatar answered Nov 10 '22 01:11

Balazs Banyai


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.

like image 26
joeblubaugh Avatar answered Nov 10 '22 03:11

joeblubaugh