'Flutter:MainActivity cannot be converted to FlutterEngine in Java Native code
I am trying to create a plugin for Flutter in Java. I am trying to pass the Android activity
parameter in my main Java
class.
This is my native code:
package com.example.trsurveys;
import androidx.annotation.NonNull;
import android.app.Activity;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
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.PluginRegistry.Registrar;
import theoremreach.com.theoremreach.TheoremReach;
public class TrsurveysPlugin implements FlutterPlugin, MethodCallHandler {
private MethodChannel channel;
private Registrar registrar;
private Activity activity;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "trsurveys");
channel.setMethodCallHandler(this);
}
When I try to debug my app it shows me the following errors
private TrsurveysPlugin(Activity activity) {
this.activity = activity;
}
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "trsurveys");
channel.setMethodCallHandler(new TrsurveysPlugin(registrar.activity()));
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("init")) {
String apiKey = call.argument("apiKey");
TheoremReach.initWithApiKeyAndUserIdAndActivityContext(apiKey, "USER_ID", activity);
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
When I try to debug my app I get the following errors:
error: constructor TrsurveysPlugin in class TrsurveysPlugin cannot be applied to given types;
flutterEngine.getPlugins().add(new com.example.trsurveys.TrsurveysPlugin());
^
required: Activity
found: no arguments
reason: actual and formal argument lists differ in length
...path/Downloads/TheoremReach-error/example/android/app/src/main/java/com/example/trsurveys_example/MainActivity.java:11: error: incompatible types: MainActivity cannot be converted to FlutterEngine
GeneratedPluginRegistrant.registerWith(this);
^
This is the code that appears in my GeneratedPluginRegistrant.java
file.
package io.flutter.plugins;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.FlutterEngine;
@Keep
public final class GeneratedPluginRegistrant {
public static void registerWith(@NonNull FlutterEngine flutterEngine) {
flutterEngine.getPlugins().add(new com.example.trsurveys.TrsurveysPlugin());
}
}
While making the Plugin I have tried deleting the GeneratedPluginRegistrant.java file but every time it gets generated with the same configurations.
Solution 1:[1]
Open your project manifest file and remove this Lines
meta-data
android:name=”flutterEmbedding”
android:value=”2?
Then build and run it again.
Solution 2:[2]
This happened to me when upgrading a repository from older flutter version to the newer one. I followed the official flutter link for the migration.
I made appropriate changes on the android manifest file. Namely:
- Remove
android:name
attribute from<application>
tag - Change
android:name
of the single<activity>
toandroid:name="io.flutter.embedding.android.FlutterActivity"
- Add
<meta-data android:name="flutterEmbedding" android:value="2"/>
as child of tag
However, I didn't change any code on the MainActivity
which caused the error. The MainActivity
was using older imports which was incompatible with the above three changes.
Final manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.appbrewery.magic8ball">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:label="magic_8_ball"
android:icon="@mipmap/ic_launcher">
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<meta-data android:name="flutterEmbedding" android:value="2"/>
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Deleting the MainActivity
resolved the issue for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Asendo |
Solution 2 | Illegal Argument |