Android库:未在路径上找到DexPathList类

3

我正在开发一个 Android 库,用于在我的应用程序中模块化一些工作,并使用 Retrofit 消费一些 Web API。

当我编译并在同一 Android Studio 项目中的示例应用程序中使用它(编译库模块)时,一切正常,但当我从构件中使用该库时,出现了错误 在路径上未找到类....

我的 gradle.build 文件包含以下内容

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
    compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'

    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.+"
    testCompile "org.hamcrest:hamcrest-core:1.3"
    testCompile "org.hamcrest:hamcrest-library:1.3"
    testCompile 'org.robolectric:robolectric:3.0'
}

我尝试了Retrofit和Retrofit2,但在实现库时我的应用程序仍然崩溃了。

Caused by: java.lang.ClassNotFoundException: Didn't find class "okhttp3.logging.HttpLoggingInterceptor" on path: DexPathList[[dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-support-annotations-23.3.0_471cb7629dd1bd2eaf19897f0bfa2e292d59a1a2-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-mx.segundamano.android-payments-library-0.1-SNAPSHOT_afd67b66a26310987e26f412f53082c415795025-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-internal_impl-23.3.0_ffb5ab0b136af54fef26d794f3208310ca6ef941-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.3.0_2834a08897c09d9f4cf6a603048125bd86044324-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-com.android.support-support-v4-23.3.0_0fe06b17a4b2ae866e5066f3ea5bc8d596bd609e-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-com.android.support-multidex-1.0.1_9e5a739896463403b4d643ff254bf74dcc98371f-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-com.android.support-appcompat-v7-23.3.0_eba794855dfef17238864c6651afded84f82cf47-classes.dex", dex file "/data/data/mx.segundamano.paymentsdemo/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-23.3.0_41e987bda5b0c3ecdbb65ba4dfa7c24496152077-classes.dex"],nativeLibraryDirectories=[/data/app/mx.segundamano.paymentsdemo-1/lib/arm64, /vendor/lib64, /system/lib64]]

05-11 11:40:03.737 4279 4279 E AndroidRuntime: 在 dalvik.system.BaseDexClassLoader 中找到类(BaseDexClassLoader.j)


我有同样的问题。你是怎么解决的? - Henrique de Sousa
实际上问题不在应用程序中,而是在库中。当您将库发布到存储库时,需要设置发布以包含存储库中的依赖项。这样,当您将库作为应用程序的依赖项添加时,它将自动携带所有库的依赖项。 - Diego Ramírez Vásquez
4个回答

3

我认为这是一个简单的Android Studio问题。为了防止出现问题,重新排列导入顺序,然后清理并构建您的项目。

这样对我解决了问题。


其实我说这是迷信,你知道做事情没有理由。所以我真的不知道,但是你可以试试:compile 'com.squareup.retrofit2:converter-gson:2.0.2' compile 'com.squareup.okhttp3:logging-interceptor:3.2.0' compile 'com.squareup.retrofit2:retrofit:2.0.2'尝试清理项目 构建 如果这样还不行,重启AndroidStudio 有时候你需要重启电脑 因为有时ADB会失败 并且它会一直运行新的adb.exe - Athinodoros Sgouromallis

0

这就是我解决问题的方法!

我的问题不在应用程序中,而是在我的库中,我在gradle文件中设置了错误的发布任务。这是您在Bintray上应该采取的方法。

task generateSourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier 'sources'
}

artifacts {
    archives generateSourcesJar
}

bintray {
    ...
    configurations = ['archives']
}

请在您的Artifactory框架中查看如何向您的库添加依赖项。


0
今天,我也遇到了同样的问题。当我查看 Github 提供的文档时,我发现由于一些更改,您将面临此错误。如果您正在使用 retrofit 版本2.7.0(就像我的情况一样),则很可能会遇到此错误。为了解决这个问题,我采取了以下步骤:
  1. 我在我的安卓项目中添加了Java 8版本支持。要添加Java 8版本支持,请打开项目级别的Gradle文件,在android括号内添加以下代码。

    android {
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    }
    
  2. 在同一Gradle文件中,还需添加以下所述依赖项

    implementation "com.squareup.okhttp3:okhttp:3.13.1"
    

下面的链接简要介绍了为什么要进行所提到的更改

点击以获取更多额外信息的详情

希望这能帮助节省一些时间。


-1

试试这个,

compileOptions{
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    
    }

1
虽然这段代码可能解决了提问者的问题,但最好还是说明一下你的代码是如何解决提问者的问题的。这样,未来的访客可以从您的帖子中学习,并将其应用到自己的代码中。SO 不是一个编程服务,而是一个知识资源。此外,高质量、完整的答案更有可能被点赞。这些特点以及所有帖子都必须是自包含的要求,是 SO 作为一个平台的一些优势,这也区别于论坛。您可以编辑以添加额外信息和/或通过源文档补充您的解释。 - ysf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接