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.
If you want just for java, you can remove -i objc flutter create -a java . 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
you need to specify your project's directory. if you are inside the dir just ad a '.' at the end: flutter create -a java . Note: -a means android flag and -i means ios flag. If you want just for java, you can remove -i objc flutter create -a java .
The most obvious route is to create a new Flutter app with the flutter create command, and then replace the generated runner projects with the current ones. However, this is a much more difficult method because we may have to do many tweaks to the current projects, and if we miss something, everything will be ruined.
Well, Flutter should still be your choice. You can use all of your existing native-language code, while writing your new features in Flutter. In the remainder of this whitepaper, we will present a technical case study where we did just that.
I had the same problem, for me this solution works.
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'
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With