34

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.

shadysamir
  • 612
  • 1
  • 7
  • 13

7 Answers7

31

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'
    }
    
Alireza
  • 193
  • 1
  • 5
Sheruan Bashar
  • 506
  • 4
  • 7
  • I've already done that, but always read kotlin file ```Couldn't read file LocalFile: 'xxxx.kt' even though it exists. Please verify that this file has read permission and try again.``` – crazecoder Nov 07 '19 at 02:22
  • In my case, I've already done all the very same steps, but I got `/MainActivity.java:13: error: method does not override or implement a method from a supertype @Override` (cannot override `configureFlutterEngine`) :( – daveoncode Feb 03 '20 at 08:56
  • @daveoncode These imports worked for me. import io.flutter.embedding.android.FlutterActivity; import io.flutter.embedding.engine.FlutterEngine; import io.flutter.plugins.GeneratedPluginRegistrant; – Afridi Kayal Feb 05 '20 at 17:42
  • don't forget to change "test_app" from the import line to the name of your app – Mawardy Mar 31 '20 at 06:59
18

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.

Allen
  • 6,745
  • 5
  • 41
  • 59
  • 3
    flutter create -a java didn't work properly, it returned "No option specified for the output directory" , I advice all not to delete android directory , however renaming it or move it somewhere else for backups. until you make sure the new directory is created successfully . – Mawardy Mar 31 '20 at 06:51
  • 5
    you need to specify your project's directory. if you are inside the dir just ad a '.' at the end: `flutter create -a java .` – jeff May 08 '20 at 11:06
17

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

NullByte08
  • 884
  • 10
  • 15
16

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

jeff
  • 1,169
  • 1
  • 19
  • 44
1

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.

ouija
  • 186
  • 1
  • 6
0

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?

mahdidham
  • 1
  • 7
0

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/

giorgio79
  • 3,787
  • 9
  • 53
  • 85