You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

求助:无法导入android.support.design.widget.Snackbar类,提示导入无法解析

Fixing "The import android.support.design cannot be resolved" for Snackbar

Hey there, I’ve dealt with this exact error countless times when working with Snackbar in Android projects. Let’s break down the solutions based on whether you’re using the old Support Library or the modern AndroidX setup:

The android.support.design package comes from the Support Design library, which was deprecated in favor of AndroidX. If you need to stick with it for an older project:

  • Add the Design library dependency to your module-level build.gradle (usually app/build.gradle):
    dependencies {
        // 28.0.0 is the final version of the legacy Support Library
        implementation 'com.android.support:design:28.0.0'
    }
    
  • Make sure your compileSdkVersion matches the dependency version. For example:
    android {
        compileSdkVersion 28
        // ... other project configurations
    }
    
  • Sync your Gradle project (click the "Sync Now" prompt in Android Studio), then clean and rebuild: Go to Build > Clean Project, followed by Build > Rebuild Project to clear cached build files that might be causing conflicts.

AndroidX replaced the Support Library entirely, and Snackbar now lives in the Material Components library. Here’s how to fix it:

  • Update your import statement from the old support package to the AndroidX version:
    // Replace this outdated line:
    // import android.support.design.widget.Snackbar;
    // With this AndroidX equivalent:
    import com.google.android.material.snackbar.Snackbar;
    
  • Add the Material Components dependency to your module-level build.gradle:
    dependencies {
        // Use the latest stable version (check Android Studio's suggestions for updates)
        implementation 'com.google.android.material:material:1.9.0'
    }
    
  • Verify your compileSdkVersion is compatible with the Material library (at least 33 for recent versions):
    android {
        compileSdkVersion 33
        // ... other project configurations
    }
    
  • Sync Gradle, then clean and rebuild your project as mentioned earlier.

Bonus: Migrating an old project to AndroidX

If you’re still on the Support Library and want to switch to AndroidX (highly recommended for long-term maintenance), Android Studio has a built-in tool:

  1. Go to Refactor > Migrate to AndroidX...
  2. Follow the prompts to back up your project and start the migration
  3. After migration, update any remaining old import statements to their AndroidX equivalents (Android Studio will usually highlight these for you)

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

火山引擎 最新活动