升级Android Studio与Gradle插件后出现android.support.test包不存在错误
问题场景
将Android Studio升级至2023.1.1版本,Gradle插件升级至8.2.0版本,同时将一款搁置1.5年的Android App的目标SDK从29修改为33。此前代码编译无警告,现在编译时出现大量package not exist错误。
错误信息
D:\work_area\android_projects\BubblesInLine\app\src\androidTest\java\ro\notnull\bubblesinline\ExampleInstrumentedTest.java:4: error: package android.support.test does not exist import android.support.test.InstrumentationRegistry; ^ D:\work_area\android_projects\BubblesInLine\app\src\androidTest\java\ro\notnull\bubblesinline\ExampleInstrumentedTest.java:5: error: package android.support.test.runner does not exist import android.support.test.runner.AndroidJUnit4; ^ D:\work_area\android_projects\BubblesInLine\app\src\androidTest\java\ro\notnull\bubblesinline\ExampleInstrumentedTest.java:7: error: package org.junit does not exist import org.junit.Test; ^ D:\work_area\android_projects\BubblesInLine\app\src\androidTest\java\ro\notnull\bubblesinline\ExampleInstrumentedTest.java:8: error: package org.junit.runner does not exist import org.junit.runner.RunWith; ^ D:\work_area\android_projects\BubblesInLine\app\src\androidTest\java\ro\notnull\bubblesinline\ExampleInstrumentedTest.java:10: error: package org.junit does not exist import static org.junit.Assert.*; ^ D:\work_area\android_projects\BubblesInLine\app\src\androidTest\java\ro\notnull\bubblesinline\ExampleInstrumentedTest.java:17: error: cannot find symbol @RunWith(AndroidJUnit4.class) ^ symbol: class RunWith D:\work_area\android_projects\BubblesInLine\app\src\androidTest\java\ro\notnull\bubblesinline\ExampleInstrumentedTest.java:17: error: cannot find symbol @RunWith(AndroidJUnit4.class) ^ symbol: class AndroidJUnit4 D:\work_area\android_projects\BubblesInLine\app\src\androidTest\java\ro\notnull\bubblesinline\ExampleInstrumentedTest.java:19: error: cannot find symbol @Test ^ symbol: class Test location: class ExampleInstrumentedTest D:\work_area\android_projects\BubblesInLine\app\src\androidTest\java\ro\notnull\bubblesinline\ExampleInstrumentedTest.java:22: error: cannot find symbol Context appContext = InstrumentationRegistry.getTargetContext(); ^ symbol: variable InstrumentationRegistry location: class ExampleInstrumentedTest D:\work_area\android_projects\BubblesInLine\app\src\androidTest\java\ro\notnull\bubblesinline\ExampleInstrumentedTest.java:24: error: cannot find symbol assertEquals("ro.notnull.bubblesinline", appContext.getPackageName()); ^ symbol: method assertEquals(String,String) location: class ExampleInstrumentedTest
当前build.gradle内容
apply plugin: 'com.android.application' android { signingConfigs { config { keyAlias '********' keyPassword '********' storeFile file('D:/work_area/android/apps/keys_store/free_apps/android_free_apps.jks') storePassword '********' } } defaultConfig { applicationId "ro.notnull.bubblesinline" minSdkVersion 21 targetSdkVersion 33 compileSdk 33 versionCode 11 versionName "1.7.2" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' debuggable false signingConfig signingConfigs.config shrinkResources true } debug { //buildConfigField("String", "BUILD_TIME", "\"0\"") debuggable true } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } namespace 'ro.notnull.bubblesinline' } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation "androidx.gridlayout:gridlayout:1.0.0" implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.gms:play-services-ads:22.5.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.recyclerview:recyclerview:1.3.2' testImplementation 'junit:junit:4.12' }
解决方案
问题根源是androidTest目录下的Instrumented测试代码缺少对应依赖,你添加的testImplementation 'junit:junit:4.12'仅适用于本地单元测试(test目录),而非设备上运行的Instrumented测试。同时旧的android.support.test包已被AndroidX替代,需要同步替换导入和依赖。
1. 补充Instrumented测试依赖
在dependencies块中添加以下内容:
androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
2. 更新测试类的导入语句
修改ExampleInstrumentedTest.java中的旧导入:
import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4;
替换为AndroidX版本的导入:
import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.ext.junit.runners.AndroidJUnit4;
3. 同步并重新编译
点击Android Studio顶部的「Sync Project with Gradle Files」按钮,等待同步完成后重新编译项目即可。
内容的提问来源于stack exchange,提问作者Marcel Preda
相关产品推荐
相关产品推荐

