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

Android Studio构建报错:android.support.annotation包不存在求助

Fixing "package android.support.annotation does not exist" in AndroidX Project

Hey there, let's work through this frustrating annotation error you're hitting. Since your project is already configured for AndroidX (you've got android.useAndroidX=true and android.enableJetifier=true in gradle.properties), the problem boils down to leftover references to the old support library annotations—either in your code or hidden in your dependencies. Here's how to fix it step by step:

1. Replace All Old Annotation Imports in Your Code

First, do a full search across all your Java/Kotlin files for any imports using the old support package:

  • Find lines like import android.support.annotation.NonNull; or import android.support.annotation.*
  • Replace every single one with the AndroidX equivalent: import androidx.annotation.NonNull; (or import androidx.annotation.*)

Don't just comment and re-add—make sure you hunt down every instance of the old support annotation imports. Even one missed line can cause this error.

2. Clean Up Your Dependencies

Looking at your build.gradle (Module:app) file, there's a conflicting dependency that's pulling in the old support annotations:

  • You have androidTestImplementation "com.android.support:support-annotations:28.0.0"—replace this with the AndroidX version:
    androidTestImplementation "androidx.annotation:annotation:1.1.0"
    

Next, check if any other dependencies are secretly bringing in the old support annotations. Run this command in your project root to generate a dependency tree:

./gradlew app:dependencies

Look for any entries under com.android.support:support-annotations. If you find one, add an exclusion to that dependency. For example:

implementation('some.dependency:xyz:1.0.0') {
    exclude group: 'com.android.support', module: 'support-annotations'
}

3. Clean and Rebuild Your Project

Sometimes cached build files keep old references around. Do a full clean and rebuild:

  • Go to Build > Clean Project in Android Studio
  • Then run Build > Rebuild Project
  • If that doesn't work, manually delete the .gradle folder in your project root, plus the build folders in both your project and module directories. Then sync your project with Gradle (File > Sync Project with Gradle Files).

4. Adjust ProGuard Settings (Since You're Using Minification)

You have minifyEnabled=true in both release and debug builds, which can sometimes strip AndroidX annotations accidentally. Add this line to your proguard-rules.pro file to keep the AndroidX annotation classes:

-keep class androidx.annotation.** { *; }

Also, I'd recommend disabling minification in your debug build—it makes debugging way easier and eliminates one potential source of issues:

debug {
    debuggable true
    minifyEnabled false
    shrinkResources false
    // proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

Give these steps a try, and your annotation error should be resolved. Let me know if you hit any snags!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 10:58:12