androidflutterandroid-studioflutter-plugin

Flutter project build fails with `Unresolved reference: SharedPreferencesAsyncApi in shared_preferences_android plugin`


I'm trying to run my Flutter app on an Android emulator, but I'm encountering the following Kotlin compilation errors related to the shared_preferences_android plugin:

Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
e: file:///Users/rakib/.pub-cache/hosted/pub.dev/shared_preferences_android-2.4.10/android/src/main/kotlin/io/flutter/plugins/sharedpreferences/SharedPreferencesPlugin.kt:45:50 Unresolved reference: SharedPreferencesAsyncApi
e: ... [many similar unresolved references follow] ...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':shared_preferences_android:compileDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

Solution

  • How I Fix This

    Run these commands in the Flutter project root:

    flutter clean
    flutter pub get
    cd android
    ./gradlew clean
    cd ..
    flutter pub run build_runner build --delete-conflicting-outputs
    flutter run
    

    These steps clean the Flutter and Android builds, refresh dependencies, and regenerate necessary files, which can resolve many common issues related to code generation or build conflicts.