1. 问题
我接入了移动推送SDK,运行时报出java.lang.NoClassDefFoundError找不到某个类,但SDK中明明包含这个类,为什么会这样?
2. 问题原因
如果您的App工程使用了multidex架构,可能会出现java.lang.NoClassDefFoundError,原因在于:推送SDK由于需要在application中进行初始化,所以相关类需要被放到MainDex文件中,否则在app启动时可能会报错。
3. 解决方案
通过修改Gradle构建脚本,可以将一些类显示放到MainDex中:
- [backcolor=transparent]afterEvaluate [backcolor=transparent]{
- [backcolor=transparent] tasks[backcolor=transparent].[backcolor=transparent]matching[backcolor=transparent]{
- [backcolor=transparent] it[backcolor=transparent].[backcolor=transparent]name[backcolor=transparent].[backcolor=transparent]startsWith[backcolor=transparent]([backcolor=transparent]'create'[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]&&[backcolor=transparent] it[backcolor=transparent].[backcolor=transparent]name[backcolor=transparent].[backcolor=transparent]endsWith[backcolor=transparent]([backcolor=transparent]'MainDexClassList'[backcolor=transparent])
- [backcolor=transparent] [backcolor=transparent]}.[backcolor=transparent]each [backcolor=transparent]{[backcolor=transparent] tk [backcolor=transparent]->
- [backcolor=transparent] tk[backcolor=transparent].[backcolor=transparent]doLast [backcolor=transparent]{
- [backcolor=transparent] keepMainMultiDex[backcolor=transparent]([backcolor=transparent]tk[backcolor=transparent].[backcolor=transparent]outputFile[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}
- [backcolor=transparent]/**
- [backcolor=transparent] * 控制MainDex中的class列表
- [backcolor=transparent] * 将multidex.keep的内容追加到 maindexlist.txt 中
- [backcolor=transparent] * @param outputFile
- [backcolor=transparent] */
- [backcolor=transparent]def[backcolor=transparent] keepMainMultiDex[backcolor=transparent]([backcolor=transparent]File[backcolor=transparent] outputFile[backcolor=transparent]){
- [backcolor=transparent] [backcolor=transparent]File[backcolor=transparent] keepFile [backcolor=transparent]=[backcolor=transparent] file[backcolor=transparent]([backcolor=transparent]"multidex.keep"[backcolor=transparent]);
- [backcolor=transparent] outputFile [backcolor=transparent]<<[backcolor=transparent] [backcolor=transparent]'\n'
- [backcolor=transparent] outputFile [backcolor=transparent]<<[backcolor=transparent] keepFile[backcolor=transparent].[backcolor=transparent]getText[backcolor=transparent]([backcolor=transparent]'UTF-8'[backcolor=transparent])
- [backcolor=transparent]}
这样在gradle的构建过程中,会将multidex.keep文件中的类同样打到maindex中。接下来只需要将报出NoClassDefFoundError的类写到multidex.keep文件即可,如:
- [backcolor=transparent]com[backcolor=transparent]/[backcolor=transparent]alibaba[backcolor=transparent]/[backcolor=transparent]sdk[backcolor=transparent]/[backcolor=transparent]android[backcolor=transparent]/[backcolor=transparent]push[backcolor=transparent]/[backcolor=transparent]securitybox[backcolor=transparent]/[backcolor=transparent]alipush[backcolor=transparent]/[backcolor=transparent]AliPushSecurityBoxService
- [backcolor=transparent]com[backcolor=transparent]/[backcolor=transparent]alibaba[backcolor=transparent]/[backcolor=transparent]sdk[backcolor=transparent]/[backcolor=transparent]android[backcolor=transparent]/[backcolor=transparent]push[backcolor=transparent]/[backcolor=transparent]CloudPushService
- [backcolor=transparent]......