You can add Flutter Module into your existing Android application. In this article, we will manually integrate the Flutter project into Kotlin. Before starting this, pelase be sure if you installed flutter sdk.

To begin, open your project in Android Studio, and then open the terminal within Android Studio. Let us assume that you have an existing Android app located at some/path/MyApp, and you want your Flutter project to be its sibling directory.

flutter create -t module --org com.example my_flutter_project


The Flutter Android engine uses Java 11 features. Please, be sure if you have Java 11.

android {
  //...
  compileOptions {
    sourceCompatibility 11
    targetCompatibility 11
  }
}


and then, add the flutter module as a dependency in Kotlin. Let us return to the terminal within Android Studio.

flutter build aar


Afterwards, the terminal will return the following result to you.

 Building with sound null safety 

Running Gradle task 'assembleAarDebug'...                          48,0s
Built build\host\outputs\repo.
Running Gradle task 'assembleAarProfile'...                        34,7s
Built build\host\outputs\repo.
Running Gradle task 'assembleAarRelease'...                        36,4s
Built build\host\outputs\repo.

Consuming the Module
  1. Open <host>\app\build.gradle
  2. Ensure you have the repositories configured, otherwise add them:

      repositories {
        maven {
            url 'C:\Users\Baris\AndroidStudioProjects\addfluttertokotlin\my_flutter_project\build\host\outputs\repo'
        }
       maven {
            url 'https://storage.googleapis.com/download.flutter.io'
        }
      }

  3. Make the host app depend on the Flutter module:

    dependencies {
      debugImplementation 'com.example.my_flutter:flutter_debug:1.0'
      profileImplementation 'com.example.my_flutter:flutter_profile:1.0'
      releaseImplementation 'com.example.my_flutter:flutter_release:1.0'
    }


  4. Add the `profile` build type:

    android {
      buildTypes {
        profile {
          initWith debug
        }
      }
    }


Open your “settings.gradle” file.

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        
        
        // Please paste the code as shown below from the terminal here.

   
    }
}
    
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()

        maven {
            url 'C:\\Users\\Baris\\AndroidStudioProjects\\FlutterDeneme\\my_flutter_project\\build\\host\\outputs\\repo'
            // This is relative to the location of the build.gradle file
            // if using a relative path.
        }
        maven {
            url 'https://storage.googleapis.com/download.flutter.io'
        }
    }
}


Afterward, implement the dependencies and add the profile build type as shown in the terminal. After complete these, synchronize your gradle. At this point, we can launch the Flutter module as an activity in Kotlin. Let us navigate to MainActivity.kt.

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
class MainActivity : AppCompatActivity() {

    private val flutterEngine: FlutterEngine by lazy {
        FlutterEngine(this.application)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        flutterEngine.dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
        FlutterEngineCache.getInstance().put("my flutter engine",flutterEngine)

        launchFlutter()
    }

    fun launchFlutter()
    {
        startActivity(FlutterActivity.withCachedEngine("my flutter engine").build(this))
    }


Next, add the following code to your AndroidManifest.xml file, as shown below.

<activity android:name="io.flutter.embedding.android.FlutterActivity"/>


After completing these steps, you can run your application. Although the design and implementation of activities may be more straightforward in Flutter, it should be noted that integrating the Flutter module into your Kotlin project will increase the size of your project files, as well as the APK file size. Also there are some limitations: 

• Embedding multiple Flutter libraries into an application is not supported.
• In add-to-app scenarios, plugins that do not support the FlutterPlugin interface may display unpredictable behaviors if they rely on assumptions that are not feasible, such as assuming the constant presence of a Flutter Activity.
• The Flutter module on Android exclusively accommodates AndroidX applications.