Abhishek Batra
Abhishek Batra

Reputation: 1599

Android : Best way to add library dependency to Android Library

I am developing a Android Library. Problem is library is itself dependent on many other gradle dependency like Google Analytics and Volley.

I am not aware if that can cause conflict in future when the developer is using this library (as gradle dependency) along with other dependencies.

So is it safe to simply add gradle compile dependency, like we do normally for any android project?

I am not sure about the approach that should be taken for developing the library.

When the library is developed, I will be hosting it on Jcenter/Maven.

I have done my research bur couldn't find anything useful. I guess this is a common issue.

Example build file

apply plugin: 'android-library'

buildscript {
    repositories {
        maven {
            url "http://maven.snplow.com/releases"
        }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}
repositories {
    jcenter()
    maven {
        url "http://maven.snplow.com/releases"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile('com.github.nkzawa:socket.io-client:0.5.+') {
        exclude group: 'org.json', module: 'json'
    }
    compile project(':VolleyLib')
    compile 'com.google.android.gms:play-services-analytics:7.5.0'
    compile files('libs/FlurryAnalytics-5.5.0.jar')
    compile files('libs/sentry-1.1.4.jar')
    compile 'com.snowplowanalytics:snowplow-android-tracker-classic:0.5.0'
}

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc2"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

Upvotes: 1

Views: 368

Answers (1)

John Fontaine
John Fontaine

Reputation: 1019

Library users can import your package with transitive=true and then use gradle dependency management and conflict resolution rules to resolve any conflicts. In your install docs have them use the following compile statement in build.gradle

    compile ('foo-bar:2.0.1+@aar') { transitive = true }

You may occasionally have to make changes to your library to support users if the 3rd party library has some weird version incompatibility.

Upvotes: 1

Related Questions