带有本地 aar 的 ClassDefNotFoundError

ClassDefNotFoundError with local aar

我知道好像有人问过这个问题,但它们略有不同,我找不到解决问题的方法。

我有一个 Android 应用程序,它依赖于几个 aar 库,而这些库又依赖于(纯 java)jar。在 Android Studio 中开发时,我将所有 aar 和 jars 作为我公司本地关系的依赖项包括在内。这按预期工作。

但是,对于部署,我们有本地 aar 文件,这些文件已经在其 libs 文件夹中包含了它们的 jar 依赖项。在已部署的 build.gradle 中,我们定义了以下条目以包含它们:

repositories {
    jcenter()
    flatDir {
        dirs '../../libs'
    }

     maven {
        url "https://maven.google.com"
    }
}

dependencies {

    compile 'com.android.support:appcompat-v7:26.1.0'
    compile ":NameOfAarFile:1.0.0@aar"
    compile ":NameOfAnotherAar:1.0.0@aar"

    compile fileTree(include: ['*.jar'], dir: 'libs')
}

如果我在 android 工作室中打开项目,gradle 会进行检查并让我构建它。它会打开,但是一旦您尝试使用源自 aar 中的 jar 的功能,我就会得到 NoClassDefFoundError.

所以我从输出中反编译了 apk,运行 它们通过 dex2jar 并查看 类 和 jd-gui 只是为了找出它们是在他们预期的地方。

现在我真的很想知道 NoClassDefFoundError 是来自,还是 - 更重要的是 - 如何摆脱它?

official documentation提到:

A library module can include a JAR library

You can develop a library module that itself includes a JAR library; however you need to manually edit the dependent app modules's build path and add a path to the JAR file.

如果可能的话,我真的很想将 jar 留在 aar 中。

如有必要,我会提供更多信息。

我知道发生了什么事。 如果您在 Android Studio 中禁用 InstantRun,它会起作用。