Search code examples
javaandroidkotlinflutter

Convert an existing Flutter Kotlin project to Flutter Java project


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.


Solution

  • 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'
      }