Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Kotlin support to your flutter project?

Tags:

kotlin

flutter

Upon reading the README file of barcode_scan plugin I encountered the instruction

This plugin is written in Kotlin. Therefore, you need to add Kotlin support to your project. See installing the Kotlin plugin.

The link only provides information about creating Kotlin project in Android Studio. How does one add Kotlin support for a flutter project? Is it even necessary?

EDIT: Since I already have an existing flutter package, I am looking for ways to add Kotlin support to that project

like image 857
Əlişiram Avatar asked Aug 29 '19 13:08

Əlişiram


People also ask

How do I add Kotlin support to Flutter project?

example. myapp' with your package. Next, if MainActivity. java is an empty class then just delete it under ../android/app/src/main/java/.. but if it is not then you will need change the class and its body to kotlin, copy it and only then delete it.

Does Flutter support Kotlin?

Google formally launched Kotlin support for Android mobile app development in 2019. But it's also critical to remember that Google developed the open-source mobile application development framework known as Flutter. Kotlin and Flutter both have a Google Label associated with them.


2 Answers

Doing it manually:

Check if you need to do this:

https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects

Next create a new flutter project for reference and call it myapp.

Next, from myapp/android/build.gradle copy:

ext.kotlin_version = '1.3.50'

and

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

Next, from myapp/android/app/build.gradle copy:

apply plugin: 'kotlin-android'

and

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

and

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

Next, create the path according to you package path and copy the file myapp/android/app/src/main/kotlin/com/example/myapp/MainActivity.kt

Next, in the file MainActivity.kt replace 'package com.example.myapp' with your package.

Next, if MainActivity.java is an empty class then just delete it under ../android/app/src/main/java/.. but if it is not then you will need change the class and its body to kotlin, copy it and only then delete it.

like image 63
Avi Cohen Avatar answered Sep 30 '22 03:09

Avi Cohen


You simply have to add a single file in Kotlin format (example under android>main>java>your packagename>test.kt and it will automatically upgrade. Can be an empty file. Add it using android studio. answer reference: https://github.com/mintware-de/flutter_barcode_reader/issues/121

like image 28
flutter_lover Avatar answered Sep 30 '22 03:09

flutter_lover