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

Android Studio中无法解析Configuration类,如何正确导入使用?

解决Android Studio中“Cannot resolve symbol 'Configuration'”的问题

Hey there, let's fix that "Cannot resolve symbol 'Configuration'" error you're running into. The main issue here is that you haven't imported the required Configuration class, and we'll also tidy up your project setup to avoid any other potential conflicts.

1. 导入正确的Configuration类

The Configuration class is part of Android's framework, living in the android.content.res package. You just need to add this import line to your test class:

import android.content.res.Configuration;

Here's how your updated imports should look:

package android.example.com;
import android.content.Context;
import android.content.res.Configuration; // 新增这行导入
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.runner.RunWith;

2. 初始化Configuration实例(如果需要使用它)

Right now you've only declared a Configuration variable in your setup() method, but haven't initialized it. If you need to work with the device's configuration, you can fetch it using the app's Context:

@Before
public void setup() {
    // 获取测试目标应用的Context
    Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    // 从Context的Resources中获取Configuration实例
    Configuration config = appContext.getResources().getConfiguration();
    
    // 现在你可以使用config对象了,比如获取屏幕方向、系统语言等
    // 示例:int orientation = config.orientation;
}

3. 清理build.gradle中的冲突依赖

Looking at your Module: app build.gradle, there are some conflicting dependencies that might cause unexpected issues later. Let's clean those up:

  • You're mixing AndroidX and old Support Library test dependencies (like com.android.support.test:runner alongside androidx.test:runner). This can lead to build errors, so remove the old support library test lines:
    // 删掉这两行
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    
  • You also have duplicate Espresso dependencies – keep only the AndroidX version (androidx.test.espresso:espresso-core:3.2.0) as it's the newer, maintained one.

Here's the cleaned-up dependencies section for reference:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    
    // 本地单元测试依赖
    testImplementation 'junit:junit:4.12'
    testImplementation 'androidx.test:core:1.0.0'
    testImplementation 'org.mockito:mockito-core:1.10.19'
    
    // 仪器化测试依赖(统一使用AndroidX)
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation "androidx.work:work-testing:2.2.0"
    
    compileOnly 'com.jakewharton.espresso:espresso:1.1-r2'
    androidTestCompile('com.jakewharton.espresso:espresso:1.1-r2') {
        exclude group: 'com.squareup.dagger'
    }
}

4. 同步并重建项目

After making these changes:

  • Click the Sync Project with Gradle Files button in the Android Studio toolbar to update your dependencies.
  • Run Build > Clean Project followed by Build > Rebuild Project to clear any cached build files that might be causing issues.

That should resolve the "Cannot resolve symbol 'Configuration'" error and get your test class working properly.

内容的提问来源于stack exchange,提问作者J. Doe

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:53:21