简体   繁体   中英

Flutter Project: MainActivity.java is missing

I am trying to create a Platform Channel in a Flutter project to access Android-specific java code. I am creating a new Flutter Application project in Android Studio and following this tutorial which mentions:

1- Navigate to the directory holding your Flutter app, and select the android folder inside it. Click OK.

2- Open the MainActivity.java file located in the java folder in the Project view.

However, the project only contains MainActivity.kt and not Java:

在此处输入图片说明

I tried creating a new activity inside the java folder manually by using context menu>New>Activity but it doesn't work.


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. This automatically creates MainActivity.java. The same goes for Objective-C and Swift. If you want to use Objective-C, uncheck "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. 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). It won't affect currently setup manifest or any other firebase related setup.

You can simply create the class file MainActivity.java with the Java code and delete the Kotlin one. 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

If you project is missing the mainactivity file in that case you can run this command

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

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

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.

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.

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