Shahbaz Hussain
Shahbaz Hussain

Reputation: 620

How can I add an external library dependency into my Library?

I made a library with another external library as it's dependency,

eg: I used Picasso in my library for image loading, it works fine if I include that in my sample project, but when I distribute it through jitpack, jcenter or maven, the external dependencies do not get included it with.

(i.e.) My library gets imported, but Picasso which my library depends upon do not get included, hence I get a crash as ClassNotFoundException, Didn't find class on path: DexPathList

Error when I include my library as a dependency on some other project

Caused by: android.view.InflateException: Binary XML file line #14: 
Binary 
XML file line #14: Error inflating class com.rd.PageIndicatorView
Caused by: android.view.InflateException: Binary XML file line #14: 
Error 
inflating class com.rd.PageIndicatorView
E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: 
Didn't find 
class "com.rd.PageIndicatorView" on path: DexPathList

Project level build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
    }

Library build.gradle (app leevel build.gradle)

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.technolifestyle'

ext {
    bintrayRepo = 'AutoImageFlipper'
    bintrayName = 'AutoImageFlipper'

    publishedGroupId = 'com.github.technolifestyle'
    libraryName = 'AutoImageFlipper'
    artifact = 'imageslider'

    libraryDescription = 'A carousel like implementation for Android with many functionalities'

    siteUrl = 'https://github.com/therealshabi/AutoImageFlipper/'
    gitUrl = 'https://github.com/therealshabi/AutoImageFlipper.git'

    libraryVersion = '1.5.3-beta.5'

    developerId = 'therealshabi'
    developerName = 'Shahbaz Hussain'
    developerEmail = '[email protected]'

    licenseName = 'The Apache Software License, Version 2.0'
    licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
    allLicenses = ["Apache-2.0"]
}

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'

    defaultConfig {
        multiDexEnabled true
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    api fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
    implementation 'com.romandanylyk:pageindicatorview:1.0.3@aar'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'androidx.multidex:multidex:2.0.1'
    testImplementation 'junit:junit:4.12'
}

apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'

Can anyone help me with where I am going wrong?

Upvotes: 4

Views: 1788

Answers (1)

shizhen
shizhen

Reputation: 12573

Can anyone help me with where I am going wrong?

I would like to say, you are NOT wrong!

I used Picasso in my library for image loading, it works fine if I include that in my sample project, but when I distribute it through jitpack, jcenter or maven, the external dependencies do not get included it with.

Basically, in my humble opinion, I would like to consider this in TWO kind of scenarios.

  • New .aar format library.

    This is a designed and expected library behaviour. This is because, each library should try its best only include its own source code and resources if possible, and it should not include/package its depending libraries, or else, your library users will have class conflict issues, version issues or even resource issues if your library is in .aar format. And, currently, aar library is NOT possible to merge elegantly (maybe you have seen some gradle plugin to merge AAR, but that is not officially supported due to the issue of resource merging, manifest merging, etc).

  • Typical .jar format library

    If your dependent library is in .jar format, I would like to say, YES, it is possible and can be managed elegantly to include it into your library. But, this still need some workaround. I.e. you need unzip those dependent .jar files and repackage them together with your own java classes. In this way, in order for your library users not to have "class conflict" issue, you'd better obfuscate those dependent library packages/classes using obfuscation tools, e.g. progurard or dexguard.

This is a link which may be useful for your case: https://stackoverflow.com/a/51131236/8034839

Upvotes: 1

Related Questions