Convert an existing Flutter Kotlin project to Flutter Java project

Viewed 15706

I created a Flutter project using default values, which are Kotlin for Android and Swift for iOS. Halfway through the project I needed to integrate a 3rd party Android SDK that requires Java. Can I convert a Flutter project to Java for Android after creation?

I know I will need yo use Platform Channels to integrate native code with my Flutter app, this is not my concern.

7 Answers

I had the same problem, for me this solution works.

  1. Move folder com.example.test_app (any name you have) from android/app/src/main/kotlin -> android/app/src/main/java
  2. Replace MainActivity.kt with Java version, or copy down here

    package com.example.test_app;
    
    import androidx.annotation.NonNull;
    import io.flutter.embedding.android.FlutterActivity;
    import io.flutter.embedding.engine.FlutterEngine;
    import io.flutter.plugins.GeneratedPluginRegistrant;
    
    public class MainActivity extends FlutterActivity {
     @Override
     public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
     GeneratedPluginRegistrant.registerWith(flutterEngine);
     }
    }
    
  3. Remove following code android/app/build.grandle

    ...
    apply plugin: 'kotlin-android'
    ...
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    
  4. In the same place replace following:

    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.1.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    }
    

    to

    dependencies {
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    

By default flutter template supports writing Android code using Kotlin, or iOS code using Swift. To use Java or Objective-C, use the -i and/or -a flags:

In a terminal run: flutter create -i objc -a java your_project_name.

If you wanna change your existing app platform language choice, as a workaround you can delete the android/ directory and run flutter create -a java to get the directory re-created for the new language choice (same for ios/ and Swift). You need to re-apply custom changes though.

If you are creating a new project from cmd:

flutter create -i objc -a java project_name

Note: -a means android flag and -i means ios flag. If you want just for java, you can remove -i objc

If you want to convert an existing project:

flutter create -a java .

There is a dot at the end of the above line, when converting an existing project

run flutter create -a java . inside your project directory

flutter create: If run on a project that already exists, this will repair the project, recreating any files that are missing.

delete the kotlin directory in android/src/main if it only consists of the generated example code

Just wanted to follow up with this, as I had to change the default language in my Flutter project from Kotlin to Java today, and ran into a little pain point, where I found that running flutter create -i swift -a java . in my project directory worked, but was causing my project name to change based on whatever directory it was in, and if that directory name was the last value of my "package name" (ie: com.flutter.app and the directory was "app"), this was then throwing a D8: Program type already present: error when compiling.

This is due to the MainActivity of the original Kotlin src. What I discovered is that I had to (after checking out my source code into this "app" folder that I wanted to use in the package name), first delete this existing android/app/src/main/kotlin folder AND THEN run the flutter create -i swift -a java . command.

Note that I didn't have any unique code added to the Kotlin folder, so removal of this folder wasn't a concern.

Hope this helps anyone else experiencing a similar issue.

Just in case after trying to do this then this, you found out that you cannot generate java class, suggestion and navigate declaration doesn't appear, etc.,

right click java folder, Mark Directory as > Source root. Then, edit your java class by open new android studio window by choose android directory

reference : Why Android Studio doesn't allow me to create Java Classes?

I am on Kotlin /Flutter too.

With the latest Firebase Flutter rewrite, most of the kt files are no longer needed. Switch to v2 embedding, and it wont matter you are using Kotlin...

https://firebase.flutter.dev/docs/migration/

Related