Android Studio和Gradle插件更新后错误:package android.support.test不存在

00jrzges  于 6个月前  发布在  Android
关注(0)|答案(1)|浏览(118)

我已经将Android Studio更新到2023.1.1,Gradle插件更新到8.2.0。这个Android应用已经有1.5年没有碰过了,我决定编译它并更改目标SDK(现在是33,以前是29)。
在代码编译之前,没有任何警告消息。现在,当我尝试编译时,我得到了很多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'

}


作为指定的here,我已经添加了testImplementation 'junit:junit:4.12',但它没有帮助。在我在build.gradle文件//testCompile 'junit:junit:4.12'之前,该行实际上是注解的。

s2j5cfk0

s2j5cfk01#

最后还是解决了。
的问题

因此,我在build.gradle文件中添加了以下行

dependencies {
    // other/old lines ...
    androidTestImplementation 'androidx.test:core:1.5.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
}

字符串
以及ExampleInstrumentedTest.java文件中的正确导入语句

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

相关问题