Convert an existing Flutter Kotlin project to Flutter Java project
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.
I had the same problem, for me this solution works.
- Move folder com.example.test_app (any name you have) from android/app/src/main/kotlin -> android/app/src/main/java
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); } }
Remove following code android/app/build.grandle
... apply plugin: 'kotlin-android' ... sourceSets { main.java.srcDirs += 'src/main/kotlin' }
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' }