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

Appcompat编译失败及运行崩溃求助:版本冲突与NoSuchMethodError异常

解决AppCompat编译冲突与运行崩溃问题

你遇到的是典型的Android Support库版本不一致引发的连锁问题:编译时的版本冲突警告,以及由此导致的运行时方法找不到崩溃。咱们一步步来解决:

问题根源

Android Studio的提示已经说得很清楚:你的项目里同时存在27.0.2和26.1.0两个版本的Support库。这是因为你直接声明的依赖(比如appcompat-v7:26.1.0)和第三方库(像Glide 4.5.0、Firebase 11.8.0)间接依赖的Support库版本不匹配,导致类加载时出现冲突——ResourcesCompat.getFont这个方法在低版本的support-v4库中不存在,高版本库混入后,类路径混乱最终引发了NoSuchMethodError。

解决方案

1. 强制统一所有Support库版本

在Module级别的build.gradle中添加一段配置,强制所有依赖的Support库都使用你指定的26.1.0版本,覆盖第三方库的间接依赖:

android {
    // 保留你原有的配置内容
}

// 添加这段配置,强制统一Support库版本
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.1.0'
            }
        }
    }
}

dependencies {
    // 保留你原有的依赖内容
}

2. 适配编译版本要求

你的compileSdkVersion和targetSdkVersion都是26,所以所有Support库必须使用26.x.x的版本,不能混用27.x.x版本,上面的配置已经帮你强制实现了这一点。如果后续想升级版本,记得同步提升compileSdkVersion和所有Support库的版本。

3. 清理并重新构建项目

修改完成后:

  • 点击Android Studio工具栏的Build -> Clean Project
  • 再点击Build -> Rebuild Project

修改后的完整build.gradle示例

apply plugin: 'com.android.application'
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.t.t"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

// 强制统一Support库版本
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.1.0'
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:customtabs:26.1.0'
    implementation 'com.github.bumptech.glide:glide:4.5.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.google.firebase:firebase-database:11.8.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

apply plugin: 'com.google.gms.google-services'

这样修改后,所有Support库会统一使用26.1.0版本,编译警告会消失,运行时的方法找不到错误也能解决。

内容的提问来源于stack exchange,提问作者Tapesh Gupta

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:51:21