Flutter: How to Fix Android module can’t recognize flutter SDK source code

1. During the development of the flutter plug-in, when opening the Android project of the plug-in and preparing to write the native code, we found that all kinds of reports were red, the code could not jump, and the experience was very bad

The main reason is that the Android project does not rely on the flutter library and uses the local.properties In flutter.sdk Path to load

sdk.dir=C :/Users/11/AppData/Local/Android/ Sdkflutter.sdk=C :\src\flutter\flutter_ windows_ 1.22.6-stable\flutter

Finally, we’re on Android/ build.gradle File, write read flutter.sdk Finally, use compileonly files to rely on the local flutter library.

//Get the local.properties configuration file
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}
//Get the sdk path of flutter
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    compileOnly files("$flutterRoot/bin/cache/artifacts/engine/android-arm/flutter.jar")
    compileOnly 'androidx.annotation:annotation:1.1.0'
}

The new sync gradle is effective.

Read More: