首页 > 解决方案 > 如何将外部库依赖项添加到我的库中?

问题描述

我用另一个外部库创建了一个库,因为它是依赖项,

例如:我Picasso在我的库中使用了图像加载,如果我将它包含在我的示例项目中,它可以正常工作,但是当我通过或分发它时jitpack,外部依赖项不会包含它。jcentermaven

(即)我的库被导入,但Picasso我的库所依赖的没有被包括在内,因此我遇到了崩溃ClassNotFoundException,在路径上找不到类:DexPathList

当我将我的库作为对其他项目的依赖项时出错

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

项目级 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
    }

库 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 = 'shahbaz.h96@gmail.com'

    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'

谁能帮我解决我哪里出错了?

标签: androidgradle

解决方案


谁能帮我解决我哪里出错了?

我想说,你没有错!

我在我的库中使用 Picasso 进行图像加载,如果我将它包含在我的示例项目中,它工作正常,但是当我通过 jitpack、jcenter 或 maven 分发它时,外部依赖项不会包含它。

基本上,以我的拙见,我想在两种情况下考虑这一点。

  • .aar格式库。

    这是设计和预期的库行为。这是因为,每个库都应该尽可能只包含自己的源代码和资源,并且不应该包含/打包其依赖的库,否则您的库用户将遇到类冲突问题、版本问题甚至资源问题如果您的图书馆是.aar格式。而且,目前,aar 库无法优雅地合并(也许您已经看到一些 gradle 插件来合并 AAR,但由于资源合并、清单合并等问题,官方不支持)。

  • 典型.jar格式库

    如果您的依赖库是.jar格式的,我想说,是的,有可能并且可以优雅地管理以将其包含到您的库中。但是,这仍然需要一些解决方法。即您需要解压缩这些依赖.jar文件并将它们与您自己的java 类一起重新打包。这样,为了让您的库用户不会出现“类冲突”问题,您最好使用混淆工具(例如 progurard 或 dexguard)混淆那些依赖的库包/类。

这是一个可能对您的案例有用的链接:https ://stackoverflow.com/a/51131236/8034839


推荐阅读