在 Android 中集成 Flutter
Posted 黄毛火烧雪下
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在 Android 中集成 Flutter相关的知识,希望对你有一定的参考价值。
在 android 中集成 Flutter
直接使用 Android Studio 是在现有应用中自动集成 Flutter 模块比较便捷的方法。在 Android Studio 中,你可以在一个项目中同时编写 Android 代码和 Flutter 代码,还可以继续使用各种常用的 IntelliJ Flutter 插件功能,例如 Dart 代码自动补全、热重载和 widget 检查器等。
https://flutter.cn/docs/development/add-to-app/android/add-flutter-screen?tab=custom-activity-launch-kotlin-tab
1.settings.gradle
include ':app'
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile,
'flutter_demo_library/.android/include_flutter.groovy'
))
include ':flutter_demo_library'
project(':flutter_demo_library').projectDir = new File('../flutter_demo_library')
2.项目build.gradle
buildscript
ext.kotlin_version = '1.6.10'
repositories
google()
mavenCentral()
dependencies
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
allprojects
repositories
google()
mavenCentral()
3.appbuild.gradle
plugins
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
android
compileSdk 32
defaultConfig
applicationId "com.lxt.flutter_demo_android"
minSdk 23
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
compileOptions
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
kotlinOptions
jvmTarget = '1.8'
dependencies
implementation project(':flutter')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
4.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.lxt.flutter_demo_android">
<application
android:name=".MyApp"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Flutter_demo_android"
tools:targetApi="31" >
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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"
/>
</application>
</manifest>
5.MyFlutterEngineBinding.kt
package com.lxt.flutter_demo_android
import android.content.Context
import io.flutter.FlutterInjector
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
/**
* 通过FlutterGroupEngine创建Engine
*/
fun Context.createGroupEngine(entryPointName: String): FlutterEngine
// 获取applicationContext
val appContext = this.applicationContext as MyApp
// 创建跳转点(enterPoint), enterPoint需要与Flutter中的方法名一致
val entryPoint = DartExecutor.DartEntrypoint(
FlutterInjector.instance().flutterLoader().findAppBundlePath(), entryPointName
)
// 创建FlutterEngine
val engine = appContext.engines.createAndRunEngine(this, entryPoint)
// 预加载,减少白屏或闪屏
engine.dartExecutor.executeDartEntrypoint(entryPoint)
// 放入缓存
FlutterEngineCache.getInstance().put(entryPointName, engine)
return engine
6.MyApplication -
package com.lxt.flutter_demo_android
import android.app.Application
import io.flutter.embedding.engine.FlutterEngineGroup
class MyApp : Application()
lateinit var engines: FlutterEngineGroup
override fun onCreate()
super.onCreate()
engines = FlutterEngineGroup(this)
7.MainActivity
class MainActivity :Activity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createGroupEngine("main")
findViewById<TextView>(R.id.tv_login).setOnClickListener
this?.let it1 ->
it1.startActivity(FlutterActivity
.withCachedEngine("main")
.build(it1))
// startActivity(FlutterActivity.createDefaultIntent(this))
以上是关于在 Android 中集成 Flutter的主要内容,如果未能解决你的问题,请参考以下文章