Android Studio编译报错: :app:mergeExtDexDebug空指针异常求助
Android Studio编译报错:mergeExtDexDebug任务触发空指针异常
问题背景
正在开发一款PDF展示应用,已完成启动页、文件上传页、PDF展示页基础框架,以及上传按钮唤起系统PDF选择框的逻辑。计划接入第三方PDF库时出现编译报错,此前无此问题,推测与配置相关。
报错信息
核心报错:Execution failed for task ':app:mergeExtDexDebug'.
触发异常:java.lang.NullPointerException: Cannot invoke "String.length()" because "<parameter1>" is null
完整错误堆栈:
Task :app:mergeExtDexDebug FAILED AGPBI: {"kind":"error","text":"java.lang.NullPointerException","sources": Execution failed for task ':app:mergeExtDexDebug'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Failed to transform appcompat-resources-1.7.0.aar (androidx.appcompat:appcompat- > Execution failed for DexingNoClasspathTransform: > Error while dexing. > Failed to transform appcompat-1.7.0.aar (androidx.appcompat:appcompat:1.7.0) to match > Error while dexing. * Try: > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Exception is: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:mergeExtDexDebug'. Caused by: java.lang.NullPointerException
相关配置
应用级build.gradle
plugins { id 'com.android.application' } android { namespace 'com.example.jurisscan' compileSdk 34 defaultConfig { applicationId "com.example.jurisscan" minSdk 24 targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } buildFeatures { viewBinding true } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.appcompat:appcompat:1.7.0' implementation 'com.google.android.material:material:1.12.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
项目级build.gradle
// Top-level build file where you can add configuration common to all sub-projects/modules. plugins { id 'com.android.application' version '8.0.0' apply false id 'com.android.library' version '8.0.0' apply false }
settings.gradle
pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } } rootProject.name = "JurisScan" include ':app'
已尝试的解决方案
- 调整配置参数
- 更新SDK及相关组件
- 清理缓存并重启Android Studio
解决思路建议
切换AppCompat版本
空指针由appcompat-resources-1.7.0触发,尝试切换版本:- 降级到
androidx.appcompat:appcompat:1.6.1 - 升级到最新稳定版(如1.8.0)
修改后同步Gradle,重新编译。
- 降级到
升级Android Gradle Plugin(AGP)版本
当前AGP版本为8.0.0,尝试升级到8.1.x或8.2.x稳定版,项目级build.gradle中修改:plugins { id 'com.android.application' version '8.2.2' apply false id 'com.android.library' version '8.2.2' apply false }同步后检查是否解决问题。
优化Dex编译配置
在应用级build.gradle的android块中添加:dexOptions { javaMaxHeapSize "4g" preDexLibraries = false dexInProcess = true }同时适配AGP 8.x推荐的Java版本:
compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 }彻底清理依赖缓存
- 执行
File > Invalidate Caches... > Invalidate and Restart - 删除项目根目录下的
.gradle文件夹和app/build文件夹 - 重新执行
./gradlew clean build --refresh-dependencies命令
- 执行
排查依赖冲突
执行./gradlew app:dependencies查看依赖树,排查是否有其他库与appcompat存在版本冲突,尤其是资源类依赖。
内容的提问来源于stack exchange,提问作者Sarimm Chaudhry
相关产品推荐
相关产品推荐

