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

如何解决Android Studio中‘源代码与字节码不匹配’问题?

Android Studio SDK源码查看异常问题修复方案

问题详情

已安装的Android组件:
已安装的Android组件

未安装其他API或系统镜像,项目的build.gradle配置如下:

plugins {
    id 'com.android.application'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "android.bignerdranch.quiz"
        minSdk 19
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

尽管调试使用的API与系统镜像版本一致,但查看Android SDK源码时仍出现异常提示:
源码查看异常提示

已尝试以下操作但未解决:

  • 清理项目
  • 重装SDK
  • 同步Gradle文件
  • 刷新Gradle依赖
  • 将minSdkVersion设置为与Compile和Target版本一致
  • 删除Gradle缓存文件

可行修复步骤

1. 确认SDK源码组件已安装

打开SDK Manager > SDK Tools,确保勾选Android SDK Sources for Android 32并完成安装。安装后重启Android Studio,重新同步项目。

2. 手动关联SDK源码路径

如果自动关联失效,手动配置源码路径:

  • 打开项目根目录下的.idea/misc.xml文件
  • 找到<component name="AndroidSdkConfiguration">节点,添加或修改源码路径配置:
    <sourceUrl url="file:///你的SDK路径/sources/android-32" />
    
    (替换为你本地实际的Android SDK路径,比如C:\Users\你的用户名\Android\Sdk\sources\android-32)
  • 保存文件后重启Android Studio。

3. 统一项目语言级别

打开File > Project Structure > Modules > 对应模块 > Properties,将Language level设置为8 - Lambdas, type annotations etc.(与build.gradle中compileOptions的Java版本一致),同步项目后重试。

4. 清除Android Studio缓存

点击File > Invalidate Caches...,勾选Clear file system cache and local history,点击Invalidate and Restart。此操作会清除Studio本地缓存,解决缓存导致的关联异常。

5. 排查SDK版本冲突

检查本地SDK目录,确保仅保留API 32相关组件,避免多版本SDK干扰。同时通过执行./gradlew dependencies(Windows下为gradlew.bat dependencies)查看依赖树,排查是否有依赖库引入了其他版本的SDK相关包,如有则通过exclude语句排除冲突依赖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 19:24:21