How to fix #39;java.lang.NoClassDefFoundError#39; in Android .aar project(如何修复Android.aar项目中的java.lang.NoClassDefFoundError)
问题描述
我构建了一个Android.aar
库,我正在尝试将其与其中一个项目集成。当应用程序尝试打开.aar
库的初始屏幕时,我使用REVERFIT进行API调用。我收到以下异常
我没有在我的java.lang.NoClassDefFoundError:解析失败 地址:Lokhttp3/OkHttpClient$Builder;
.aar
项目中模糊或启用PRO-Guard。
下面是我的.aar
Gradle依赖项
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
推荐答案
好的,这是一个常见问题。在其他项目中使用Android库(AAR)有多种方式。例如:
- 通过使用以下命令将此AAR作为模块导入到示例项目中
implementation project(':mylibrary')
。 - 通过将您的AAR上传到maven存储库(artiFactory、maven local、Jitpack等)
注意:
- 如果您使用的是上面的数字1,那么您还必须 使用相同的命令将(翻新、okhttp3等)添加到样例项目中 版本,因为默认情况下AAR不包括子项 从属关系。这就是为什么你会得到这个例外 "java.lang.NoClassDefFoundError:解析失败: Lokhttp3/OkHttpClient$Builder‘"。
- 如果您使用上面的编号2,则必须确保您的pom.xml文件包含您的子依赖项,因为服务器需要下载它们并使其在示例项目中可用。
我推荐什么?
我建议开发人员使用MavenLocal()
,它在将您的AAR发布到Jitpack之类的公共存储库或您想要的任何位置之前复制真实的场景。
我该如何做?
- 库模块的内部build.gradle:
apply plugin: 'maven-publish'
project.afterEvaluate {
publishing {
publications {
library(MavenPublication) {
setGroupId 'YOUR_GROUP_ID'
//You can either define these here or get them from project conf elsewhere
setArtifactId 'YOUR_ARTIFACT_ID'
version android.defaultConfig.versionName
artifact bundleReleaseAar //aar artifact you want to publish
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
}
运行assemble
和publishToMavenLocal
分级任务。你会看到类似这样的东西:
- 在示例项目中
allprojects {
repositories {
mavenLocal()
...
}
}
implementation '${YOUR_GROUP_ID}:${YOUR_ARTIFACT_ID}:${YOUR_VERSION}'
这篇关于如何修复Android.aar项目中的java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!