A pair of applications provide a direct means of integrating with one another via application programming interfaces (APIs)

Overview

Project Set Up

Create a parent directory for the project. This will hold both the native Android application and Flutter module as separate projects within it. Having a parent directory to the native project will make it easier to add the module to.

Once the project has been cloned you may open it in Android Studio. There is nothing special about this project. It can be cloned or downloaded then opened in Android Studio in the same way as any other native Android project. Add Flutter Module

Open up terminal in Android Studio

cd ..

flutter create -t module name_of_module

Your Flutter module will now be at the same directory level and your native Android project.

Add compile options to the app level build.grade within the Android block if it does not already exist.

android {
  compileOptions {
    sourceCompatibility 1.8
    targetCompatibility 1.8
  }
}

Next open the settings.gradle file and add the following code to it below include ‘:app’

include ':app'
setBinding(new Binding([gradle:this]))
evaluate(new File(
  settingsDir.parentFile,
  'name_of_module/.android/include_flutter.groovy'
))

Sync gradle and this will add some additional code and files to you project to support Flutter.

Add Flutter as a dependency to your app level build.grade file.

dependencies {
  implementation project(':flutter')
}

Once the gradle is synced again then a Flutter module has successfully been added to the native project. Add a Flutter Activity

There are several ways to add Flutter or Dart code to a native Android application. In this tutorial I will focus on adding a screen written completely in Flutter to an Android application. A splash screen written in Flutter will be added as a FlutterActivity to the native app. This example can be expanded on to add fully functioning Flutter screens to your native app.

Add the following code to the Activity you want to launch the FlutterActivity from. This will automatically call the main.dart file in the Flutter module and launch it as an Activity.

startActivity(FlutterActivity.createDefaultIntent(this))

I then added a Handler with a delay to launch another Activity and calling finish() to close the splash screen we have open from Flutter. The full onCreate code of our native Activity looks as follows. This change completes the native portion of this process.

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    startActivity(FlutterActivity.createDefaultIntent(this))
    Handler().postDelayed({
    val intent = Intent(this@MainActivity,
                        HelloWorldActivity::class.java)
      startActivity(intent)
      finish()
    }, 5000)
  }

Make sure to add the FlutterActivity to the AndroidManifest.xml as follows:

">
<activity
    android:name="io.flutter.embedding.android.FlutterActivity"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize"
    />

Create the Flutter Splash Screen

This section goes through the process of creating the Flutter splash screen in dart. I am using a package called Flutter SpinKit to easily add a loading animation to my splash screen. It can be found at https://pub.dev/packages/flutter_spinkit and added as a dependency in pubspec.yaml

If you are familiar with Flutter you will see that the project structure of the module is very similar to a normal Flutter project. Navigate to main.dart in the lib folder. Delete the example counter app and add your code for the splash screen. My main.dart file looks as follows.

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
void main() => runApp(chooseWidget(window.defaultRouteName));
Widget chooseWidget(String route) {
  switch (route) {
    // name of the route defined in the host app
    case 'splashRoute':
      return MyFlutterView();
    default:
      return MyFlutterView();
  }
}

class MyFlutterView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: SplashScreen(),
    );
  }
}

class SplashScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Container(
        child: SpinKitDoubleBounce(
          color: Colors.purple,
          size: 100.0,
        ),
      ),
    );
  }
}

Run Your App

Make sure the minSdk in the build.gradle of the native project and Flutter project match.

Go back into your native Android project. Run the ‘app’ configuration on your device or emulator of choice.

You might also like...
Clean Code and Reactive Programming PlayGround for Bangkit 2021

Clean Code and Reactive Programming PlayGround for Bangkit 2021 Hello! This repo contains the IntelliJ project that I use to present my talk, "Clean A

Exercises for Functional Programming learning in Kotlin with Arrow

Exercises for Functional Programming in Kotlin with Arrow-kt Exercises and practice code for Functional Programming learning in Kotlin with Arrow Requ

A template that utilizes both Scala and Kotlin because why not (And also because I endorse programming hell)

Fabric-Scala-Kotlin-template A template that utilizes both Scala and Kotlin because why not (And also because I endorse programming hell) I don't care

Lambda-snake.kt - Snake Game Implementation for Web using Kotlin programming language compiled for Javascript
Lambda-snake.kt - Snake Game Implementation for Web using Kotlin programming language compiled for Javascript

Projeto da disciplina de Linguagem de Programação Funcional 2021.1 (jan/2022) 📄

A thought experiment on architecture, object-oriented programming, and composability.

Journal3 There's barely anything special about the features that Journal3 is offering, it's literally yet another journaling application. What is spec

It's finally easy to take photos/videos via camera or get photos/videos from gallery on Android.
It's finally easy to take photos/videos via camera or get photos/videos from gallery on Android.

Shutter-Android It's finally easy to take photos/videos via camera or get photos/videos from gallery on Android. What is Shutter? Shutter is an Androi

A Kotlin library for interacting with Discord via IPC

KDiscordIPC A Kotlin library for interacting with Discord via IPC Features Fully documented codebase macOS and Linux support (including M1 Macs, Windo

Blog implemented via the Storyblok Kotlin Multiplatform SDK (Android, JVM)
Blog implemented via the Storyblok Kotlin Multiplatform SDK (Android, JVM)

storyblok-mp-SDK-blog ... a showcase of using the Storyblok Kotlin Multiplatform Client to build a blog application (Android, JVM) What's included 🚀

BlurHash support for iOS, Android and JVM via Kotlin Multiplatform
BlurHash support for iOS, Android and JVM via Kotlin Multiplatform

blurhash A Kotlin Multiplatform library to use blurhash in your Android App, iOS / Mac App & JVM Backend. Android iOS JVM Why? If you've tried using b

Owner
Behruz Hurramov
:sun_with_face:software engineer by day, software engineer by night.:moon:
Behruz Hurramov
Ivy FRP is a Functional Reactive Programming framework for declarative-style programming for Android

FRP (Functional Reactive Programming) framework for declarative-style programming for Andorid. :rocket: (compatible with Jetpack Compose)

null 8 Nov 24, 2022
Main goal of this project is to find the best route from one country to another

Route-service Main goal of this project is to find the best route from one country to another. Data is presented as json format. I've implemented A* p

Teyyihan Aksu 4 Aug 2, 2022
[prototype] Generate TypeScript interfaces from Kotlin classes

Kotlinx Serialization TypeScript Generator Kotlinx Serialization TypeScript Generator creates TypeScript interfaces from Kotlinx Serialization classes

null 17 Dec 18, 2022
An example for who are all going to start learning Kotlin programming language to develop Android application.

Kotlin Example Here is an example for who are all going to start learning Kotlin programming language to develop Android application. First check this

Prabhakar Thota 56 Sep 16, 2022
Mobile Application Dvelopment Practical-12: Working with JSON APIs

Mobile Application Dvelopment Practical-12: Working with JSON APIs Developed by,

Achal Hingrajiya 0 Jan 11, 2022
Kotlin Multiplatform Mobile version of Tisdagsgolfen... yet another version.

TheCube in Kotlin Multiplatform Experimenting with KMM, and Kotlin, Jetpack, SwiftUI, and all the other new stuff... https://kotlinlang.org/docs/kmm-g

Kim Fransman 0 Dec 25, 2022
Yet Another Native Loader for the JVM.

yanl - yet another native loader Yet another Native library extractor/loader for the JVM, written in Kotlin. why other libraries simply don't fit my n

Stardust Enterprises 5 Dec 23, 2022
Yet another booru viewer for Android

Yet another booru imageboards viewer for Android Download Preview Click Here Building You can build this app just like any other flutter app, for exam

Nauval Rizky 129 Jan 4, 2023
Yet another advanced Craftfting Table mod. My entry for ModFest: Singularity

Crafting Bench A utility mod that adds a custom crafting table with semi-automatic crafting. Features For a complete list of features please check the

Luca Argolo 2 Aug 23, 2022
Android utilities for easier and faster Kotlin programming.

Android utilities for easier and faster Kotlin programming. Download Gradle compile 'com.costular:kotlin-utils:0.1' How to use It depends on utilities

Diego Francisco Concepción 51 Oct 3, 2022