Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

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"

enter image description here

like image 587
HemOdd Avatar asked Dec 07 '19 12:12

HemOdd


2 Answers

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.

like image 171
Kaustuv Prajapati Avatar answered Oct 18 '22 10:10

Kaustuv Prajapati


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);
  }
}
like image 33
Emran Avatar answered Oct 18 '22 09:10

Emran