So, I wanted to include the Flutter Barcode Scanner package in my Flutter project. So I added it to the dependencies in my pubspec.yaml
file. But when I ran the app, it immediately crashed with the error message:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp/com.mycompany.myapp.MainActivity}:
java.lang.ClassCastException: com.mycompany.myapp.MainActivity cannot be cast to io.flutter.embedding.android.FlutterActivity
...
Caused by: java.lang.ClassCastException: com.neuberapps.math_trainer.MainActivity cannot be cast to io.flutter.embedding.android.FlutterActivity
at com.amolg.flutterbarcodescanner.FlutterBarcodeScannerPlugin.registerWith(FlutterBarcodeScannerPlugin.java:87)
at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:29)
at com.mycompany.myapp.MainActivity.onCreate(MainActivity.kt:11)
I tried adding the package to another project and it worked without any problems. So, because the problem was obviously somewhere in my first project and according to the error message in my MainActivity.kt
I opened it in both projects and it was different:
Project 1 (with error):
import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
}
}
Project 2 (without error):
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
}
I've tried to just copy the code from one project to the other but of course this didn't work. It produced the following build time error:
...\MainActivity.kt: (10, 44): Type mismatch: inferred type is FlutterEngine but PluginRegistry! was expected
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
I'd guess the problem somehow occurs because of this difference. How do I correctly change my file from the first one to the second one.
Thanks a lot for any help!
Project 1 was created with a Flutter version below 1.12, and thus has the older plugin APIs. To migrate to the newer plugin APIs, follow the instructions at Upgrading pre 1.12 Android projects .
Specifically, after updating MainActivity.kt
, you will also need to add some metadata to the <application>
tag in your manifest. This tells Flutter to generate a different version of GeneratedPluginRegistrant
.
<application ... >
<!-- ... -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.