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

Flutter真机运行失败:找不到androidx.annotation相关类求助

Fixing AndroidX Annotation Not Found Errors in Flutter (Manual Environment Setup)

Hey there, let's work through this issue step by step. Since you set up your Flutter environment manually without Android Studio (using VS Code + brew-installed Gradle/Java), the problem likely stems from misconfigured AndroidX settings, version mismatches, or Gradle path conflicts. Here's how to diagnose and fix it:

1. Verify AndroidX is Enabled in Your Project

AndroidX annotations won't be recognized if your project isn't set to use AndroidX. Open android/gradle.properties and ensure these two lines are present and set to true:

android.useAndroidX=true
android.enableJetifier=true

Jetifier helps migrate old support library dependencies to AndroidX, so this is critical for resolving missing class errors.

2. Check Gradle + Android Gradle Plugin (AGP) Compatibility

Brew-installed Gradle might not match the version required by your project's AGP, which causes dependency resolution issues:

  • First, check your AGP version in android/build.gradle (look for the classpath 'com.android.tools.build:gradle:x.x.x' line).
  • Run gradle -v in your terminal to see which Gradle version your system is using. Make sure this version aligns with the AGP compatibility requirements (e.g., AGP 7.4.x requires Gradle 7.5+, AGP 8.0+ requires Gradle 8.0+).
  • To avoid global Gradle conflicts, it's better to use your project's Gradle wrapper instead of the brew-installed one. Open android/gradle/wrapper/gradle-wrapper.properties and confirm the distributionUrl points to a compatible Gradle version. Then run ./gradlew clean from the android directory to use the wrapper.

3. Ensure Java Version Compatibility

Flutter and AGP have strict Java version requirements:

  • AGP 7.x requires Java 11; AGP 8.x requires Java 17.
  • Run java -version to check your current Java version. If it's mismatched, use brew to install the correct version:
    # For Java 11
    brew install openjdk@11
    # Link it to make it the default (may require sudo)
    brew link openjdk@11 --force
    
    You can also use jenv to manage multiple Java versions if you need to switch between projects.

4. Clean Caches and Rebuild

Corrupted build caches often cause unexpected dependency issues. Run these commands in order:

flutter clean
rm -rf android/.gradle
flutter pub get
flutter run

This clears old build artifacts and forces Gradle to re-resolve all dependencies from scratch.

5. Explicitly Add AndroidX Annotation Dependency (If Needed)

In rare cases, your project might not pull in the AndroidX annotation dependency automatically. Open android/app/build.gradle and add this line to the dependencies block:

implementation 'androidx.annotation:annotation:1.6.0'

Use the latest compatible version for your AGP (1.6.0 works with most recent AGP versions).

6. Check Gradle Environment Variables

If you set a GRADLE_HOME environment variable pointing to your brew-installed Gradle, this might override the project's wrapper. Temporarily remove or comment out GRADLE_HOME from your shell config (.zshrc, .bash_profile, etc.), restart your terminal, and try building again.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:24:44