Flutter not found when developing plugin for Android

Viewed 6542

I forked a plugin with the intent to make an update to use the embedding v2 for Android. But when I opened the android folder on Android Studio it does not find Flutter. It fails to import these classes:

import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.PluginRegistry.Registrar;

Is there anything I am missing to edit the Android part of a Flutter plugin?

This is the plugin I am trying to update: https://github.com/lslv1243/launch_review

Thanks in advance!

5 Answers

It worked for me when I did the next steps (assuming that Dart and Flutter plugins are configured in the Android Studio Preferences properly):

  1. Close all the projects up until you see the "welcome to Android Studio" window with recent projects list and a set of options in the center
  2. Select "Open existing Android Studio project"
  3. Select the file plugin/example/android/build.gradle

You should open the project in android studio from the example/android location.

It turned out to be a known error: https://github.com/flutter/flutter/issues/19830. You can find different solutions and general discussion of the problem there.

Provided solutions did not help in my case. What had worked out is this one (by @incloudss):

For everyone who still struggles with that problem, ignore the official documentation, and do not open android project the way they describe it there.

Instead:

  1. Open the main flutter project.
  2. Go to Tools -> Flutter -> Open For Editing In Android Studio(yes, it doesn't >make sense because we are already in AS, but it works).

I faced a similar situation when I imported an android project from the plug-in module of my Flutter project. That means, My Flutter project has an android folder at the root directory of the project, and another android directory inside the module I imported as a plug-in. If I try to open that module with the android studio it will not detect "io.flutter.plugin.common" packages.Because it missing the 'flutter.jar' library.

So what I did was, I Just added the flutter.jar path to dependency,

implementation files('/Users/username/Developer/flutter/bin/cache/artifacts/engine/android-arm64-release/flutter.jar')

here I choose 'android-arm64-release' because I am using M1 chip macbook.

Now i can edit the android module of the plugin project.But to run the flutter project i have to comment out the flutter.jar dependency and sync again before run, otherwise it will show conflict errors.

Related