简体   繁体   English

Flutter 项目:缺少 MainActivity.java

[英]Flutter Project: MainActivity.java is missing

I am trying to create a Platform Channel in a Flutter project to access Android-specific java code.我正在尝试在 Flutter 项目中创建一个平台通道来访问特定于 Android 的 java 代码。 I am creating a new Flutter Application project in Android Studio and following this tutorial which mentions:我正在 Android Studio 中创建一个新的 Flutter 应用程序项目,并遵循教程,其中提到:

1- Navigate to the directory holding your Flutter app, and select the android folder inside it. 1- 导航到 Flutter 应用所在的目录,然后选择其中的 android 文件夹。 Click OK.单击确定。

2- Open the MainActivity.java file located in the java folder in the Project view. 2- 打开位于项目视图中 java 文件夹中的 MainActivity.java 文件。

However, the project only contains MainActivity.kt and not Java:但是,该项目仅包含 MainActivity.kt 而不是 Java:

在此处输入图片说明

I tried creating a new activity inside the java folder manually by using context menu>New>Activity but it doesn't work.我尝试使用上下文菜单>新建>活动在java文件夹中手动创建一个新活动,但它不起作用。


EDIT:编辑:

The best solution for this (if you can create a new project) is to uncheck "Include Kotlin support for Android code" when you are setting up the project.对此的最佳解决方案(如果您可以创建新项目)是在设置项目时取消选中“Include Kotlin support for Android code”。 This automatically creates MainActivity.java.这会自动创建 MainActivity.java。 The same goes for Objective-C and Swift. Objective-C 和 Swift 也是如此。 If you want to use Objective-C, uncheck "Include Swift support for iOS code"如果您想使用 Objective-C,请取消选中“Include Swift support for iOS code”

在此处输入图片说明

If you are here because you are following the steps for setting up firebase_messaging , you can look at this answer and just create the Application.kt file (instead of Java) next to your MainActivity.kt file.如果您在这里是因为您正在按照设置firebase_messaging的步骤操作,您可以查看此答案并在MainActivity.kt文件旁边创建Application.kt文件(而不是 Java)。 Here it is:这里是:

package com.example.yourapp

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    override fun registerWith(registry: PluginRegistry?) {
        io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}
flutter create -a java .

Try this command.试试这个命令。 with '.'和 '。' at end.最后。 It represent current project directory.它代表当前项目目录。 apply this command from project root folder.从项目根文件夹应用此命令。 This command will try to recreate android project with java (this will setup your MainActiviy.java).此命令将尝试使用 java 重新创建 android 项目(这将设置您的 MainActiviy.java)。 It won't affect currently setup manifest or any other firebase related setup.它不会影响当前设置的清单或任何其他与 Firebase 相关的设置。

You can simply create the class file MainActivity.java with the Java code and delete the Kotlin one.您可以简单地使用 Java 代码创建类文件 MainActivity.java 并删除 Kotlin 类文件。 It should work:它应该工作:

public class MainActivity extends FlutterActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}

screen屏幕

Create MainActivity.java and remember place it in this direction: Android/App/src/MainActivity.java创建 MainActivity.java 并记住把它放在这个方向: Android/App/src/MainActivity.java

If you project is missing the mainactivity file in that case you can run this command如果您的项目在这种情况下缺少 mainactivity 文件,您可以运行此命令

flutter create .

the above command will add any missing files related to your project for all the platforms that your project supports,including mainActivity.java and incase you want to change the platform language eg java to kotlin for android then you can specify the language using the -a flag上面的命令将为您的项目支持的所有平台添加与您的项目相关的任何丢失的文件,包括 mainActivity.java 并且如果您想更改平台语言,例如 java 到 kotlin for android 那么您可以使用-a指定语言旗帜

flutter create -a kotlin .

this will create a kotlin directory and add the missing files adding a dot at the end indicates the project name would not be altered这将创建一个 kotlin 目录并添加丢失的文件,在末尾添加一个点表示不会更改项目名称

Note: This command wont work if your project directory name has (space,-,Uppercase letter) it should strictly be in lowercase and separated with _ instead of space.注意:如果您的项目目录名称有(空格,-,大写字母),则此命令将不起作用,它应该严格使用小写字母并用 _ 而不是空格分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM