How to use Java or Kotlin with Flutter for Android Development

Welcome to Flutter tutorial. Today we will see how to use Java or Kotlin with Flutter for Android development. It is needed because you cannot do everything in Flutter that Java or Kotlin does. Flutter takes less time for developing good looking UI, but sometimes it does not have plugins for complex operations. That is why Flutter gives a way to use power of Java and Kotlin in Android. Let's start.

Here we will create a simple Application which will call a function written in MainActivity.kt or .java when button in pressed.

Note: It will only work for Android, not iOS. It can be done for iOS also, but I don't know Swift :).

First of all we have to create project, so open Terminal or Command Prompt, change directory to wherever you want to create the project, and run the following command if you are using Kotlin

flutter create -a kotlin ourproject

If you want to use Java then just -
flutter create ourproject

I am dividing this thing in three parts: Flutter(or Dart), Java, and Kotlin. If you want to use Java then skip kotlin part, and vice-versa.

Flutter Part:

Create a Stateful widget and your state class should look like this:

Also add this two lines at the top or import these two modules:
import 'dart:async';
import 'package:flutter/services.dart';
In above dart code, callNativeFunction is the function which will call our native(Java/Kotlin) function using invokeMethod function of MethodChannel class. The string passed to constructor of MethodChannel class can be anything meaningful you want.

invokeMethod: There are two arguments given to it. The first argument, which is string, is used to refer the function you want to call in Java or Kotlin. It can be anything, actually it is useful when you want to invoke multiple native functions. By comparing strings you can check which native function user want to call from Flutter. Second argument contains actual arguments you want send to Java/Kotlin. We will use both of them.

await and async are used, because we want to happen this all in background and Flutter should do all its UI related work. 

setState is called, because we have to build or render our UI again after receiving data from native function.

Note: use FutureBuilder class/widget for building the widgets which use data received from Java or Kotlin, because heavy operation like reading from external storage in Java/Kotlin takes time. UI will be rebuilded without updated data if you do not know when to call setState, and your native function is still working in background. FutureBuilder gives facility to automatically call setState when your data is received completely.

In Future, I will write an article about using async, await and FutureBuilder.

Kotlin Part:

This Kotlin part of the article also contains explanation about objects, and functions used in both Java, and Kotlin.
Go to following directory and open MainActivity.kt file ourproject\android\app\src\main\kotlin\com\example\ourproject
paste this code to the MainActivity.kt file and run your app using flutter run in project directory-

Here, setMethodCallHandler of MethodChannel handles or calls whatever functions flutter has told it to call. The string CHANNEL given to MethodChannel should be same as the string given to it in Flutter.

You can see we are comparing strings in if condition, call.method contains the string invokeMethod took as first argument in Flutter. When you want to call multiple functions, you can compare strings referring to your functions with call.method to call right functions.

call.argument returns argument whose name you give to it as argument :). You can go back in Dart code, and see our argument name. It was 'arg'.

result.success takes the data as argument which you want to return, or you want to send to Flutter. It can be anything like Int, String or a List.

Java Part:

Explanation of the Java code given be below is the same as explanation of Kotlin code, after all Kotlin they use same classes.

Java code and Kotlin code are same, only their syntax are changing. So, you can  read the above explanation for the code given below. Go to ourproject\android\app\src\main\java\com\example\ourproject and paste the following code in file. After that, you can run your app using flutter run in the project directory.

Post a Comment


  1. Wonderful post. I simply stumbled upon your weblog and wanted to mention that I have
    truly loved surfing around your blog posts.
    You can find more information regarding software services click herebest software solution in hyderabad

  2. incompatible types: MainActivity cannot be converted to FlutterEngine. please help sir

  3. Is it possible to extend java class in flutter?