Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 956
shadysamir Avatar asked Oct 27 '19 13:10

shadysamir


People also ask

How to remove Kotlin files in flutter?

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

How do I create a Java project in flutter?

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 .

How to replace a flutter runner project with a new project?

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.

Should you use flutter or native language for your next project?

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.


3 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'
    }
    
like image 119
Sheruan Bashar Avatar answered Oct 19 '22 21:10

Sheruan Bashar


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.

like image 18
Allen Avatar answered Oct 19 '22 21:10

Allen


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

like image 14
NullByte08 Avatar answered Oct 19 '22 22:10

NullByte08