You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio报错:Could not initialize class及NoClassDefFoundError求助

解决Android Gradle构建报错:Could not initialize class com.android.build.gradle.internal.dsl.decorator.AndroidPluginDslDecoratorKt

你的报错核心是ASM库版本冲突或缺失,第二个报错java.lang.NoClassDefFoundError: org/objectweb/asm/commons/Method直接指向这个问题,以下是针对性解决方案:

  • 强制统一ASM依赖版本
    在项目根目录的配置文件中添加版本约束,确保所有依赖使用同一版本的ASM(推荐9.5版本,适配多数AGP版本):

    • 若使用Gradle 7.0+的dependencyResolutionManagement(在settings.gradle[.kts]中):
      dependencyResolutionManagement {
          repositories {
              google()
              mavenCentral()
          }
          constraints {
              all {
                  if (it.group == "org.ow2.asm") {
                      it.version { strictly "9.5" }
                  }
              }
          }
      }
      
    • 若使用旧版Gradle(无dependencyResolutionManagement,在根build.gradle[.kts]的allprojects块中):
      allprojects {
          configurations.all {
              resolutionStrategy {
                  force "org.ow2.asm:asm:9.5"
                  force "org.ow2.asm:asm-commons:9.5"
              }
          }
      }
      
  • 彻底清理Gradle缓存并重建
    执行命令强制刷新依赖并清理缓存:

    # Mac/Linux
    ./gradlew clean build --refresh-dependencies
    
    # Windows
    gradlew.bat clean build --refresh-dependencies
    

    若命令执行无效,手动删除本地Gradle缓存目录:

    • Windows: C:\Users\<你的用户名>\.gradle\caches
    • Mac/Linux: ~/.gradle/caches
  • 校验AGP与Gradle版本兼容性
    打开根目录的build.gradle[.kts]或settings.gradle[.kts],确保Android Gradle插件(AGP)和Gradle版本匹配:

    • AGP 7.4.x → Gradle 7.5+
    • AGP 8.0.x → Gradle 8.0+
    • AGP 8.1.x → Gradle 8.1+
      调整到官方推荐的对应版本后重新同步项目。
  • 排查并移除冲突的第三方依赖
    执行命令生成依赖树,查找引入旧版ASM的第三方库:

    # Mac/Linux
    ./gradlew dependencies
    
    # Windows
    gradlew.bat dependencies
    

    找到冲突依赖后,在依赖声明中排除ASM相关模块:

    implementation("com.example:conflicting-library:1.0.0") {
        exclude group: "org.ow2.asm"
    }
    

内容的提问来源于stack exchange,提问作者Hassaan J.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 06:58:24