开发者社区> 问答> 正文

集成SDK后运行App报java.lang.NoClassDefFoundError该如何解决



1. 问题


我接入了移动推送SDK,运行时报出java.lang.NoClassDefFoundError找不到某个类,但SDK中明明包含这个类,为什么会这样?

2. 问题原因


如果您的App工程使用了multidex架构,可能会出现java.lang.NoClassDefFoundError,原因在于:推送SDK由于需要在application中进行初始化,所以相关类需要被放到MainDex文件中,否则在app启动时可能会报错。

3. 解决方案


通过修改Gradle构建脚本,可以将一些类显示放到MainDex中:

  1. [backcolor=transparent]afterEvaluate [backcolor=transparent]{
  2. [backcolor=transparent]    tasks[backcolor=transparent].[backcolor=transparent]matching[backcolor=transparent]{
  3. [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])
  4. [backcolor=transparent]    [backcolor=transparent]}.[backcolor=transparent]each [backcolor=transparent]{[backcolor=transparent] tk [backcolor=transparent]->
  5. [backcolor=transparent]        tk[backcolor=transparent].[backcolor=transparent]doLast [backcolor=transparent]{
  6. [backcolor=transparent]            keepMainMultiDex[backcolor=transparent]([backcolor=transparent]tk[backcolor=transparent].[backcolor=transparent]outputFile[backcolor=transparent]);
  7. [backcolor=transparent]        [backcolor=transparent]}
  8. [backcolor=transparent]    [backcolor=transparent]}
  9. [backcolor=transparent]}
  10. [backcolor=transparent]/**
  11. [backcolor=transparent] * 控制MainDex中的class列表
  12. [backcolor=transparent] * 将multidex.keep的内容追加到 maindexlist.txt 中
  13. [backcolor=transparent] * @param outputFile
  14. [backcolor=transparent] */
  15. [backcolor=transparent]def[backcolor=transparent] keepMainMultiDex[backcolor=transparent]([backcolor=transparent]File[backcolor=transparent] outputFile[backcolor=transparent]){
  16. [backcolor=transparent]    [backcolor=transparent]File[backcolor=transparent] keepFile [backcolor=transparent]=[backcolor=transparent] file[backcolor=transparent]([backcolor=transparent]"multidex.keep"[backcolor=transparent]);
  17. [backcolor=transparent]    outputFile [backcolor=transparent]<<[backcolor=transparent] [backcolor=transparent]'\n'
  18. [backcolor=transparent]    outputFile [backcolor=transparent]<<[backcolor=transparent] keepFile[backcolor=transparent].[backcolor=transparent]getText[backcolor=transparent]([backcolor=transparent]'UTF-8'[backcolor=transparent])
  19. [backcolor=transparent]}

这样在gradle的构建过程中,会将multidex.keep文件中的类同样打到maindex中。接下来只需要将报出NoClassDefFoundError的类写到multidex.keep文件即可,如:
  1. [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
  2. [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
  3. [backcolor=transparent]......

展开
收起
猫饭先生 2017-10-23 14:20:45 1732 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
集成智能接入网关APP:优化企业级移动办公网络 立即下载
一个跨平台的云服务SDK需要什么 立即下载
云效助力企业集成安全到DevOps中 立即下载