建立安卓原生工程(語言選擇爲Kotlin,工程命名爲:android_demo) android
建立Flutter工程(下面是採用IntelliJ IDEA建立的,語言選擇爲Kotlin/Swift,工程命名爲:flutter_demo) git
android/app/build.gradle
:apply plugin: 'com.android.application' 複製代碼
改成bash
apply plugin: 'com.android.library' 複製代碼
apply plugin: 'com.android.library' ... android { ... defaultConfig { minSdkVersion 16 targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } ... } 複製代碼
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.flutter_demo"> <application> <activity android:name="com.example.flutter_demo.MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <meta-data android:name="io.flutter.app.android.SplashScreenUntilFirstFrame" android:value="true" /> </activity> </application> </manifest> 複製代碼
MainActivity
文件import io.flutter.view.FlutterMain
複製代碼
FlutterMain.startInitialization(this.applicationContext)
複製代碼
最終代碼以下:markdown
import android.os.Bundle import io.flutter.app.FlutterActivity import io.flutter.plugins.GeneratedPluginRegistrant import io.flutter.view.FlutterMain class MainActivity: FlutterActivity() { override fun onCreate(savedInstanceState: Bundle?) { FlutterMain.startInitialization(this.applicationContext) super.onCreate(savedInstanceState) GeneratedPluginRegistrant.registerWith(this) } } 複製代碼
settings.gradle
文件,增長以下配置: (下面Flutter路徑必須是相對路徑,不能用絕對路徑)include ':app', ':flutter_demo' project(':flutter_demo').projectDir = new File(settingsDir, '../flutter_demo/android/app') 複製代碼
local.properties
中,增長以下配置(能夠從Flutter工程下android目錄下local.properties文件中拷貝):flutter.sdk=本身的flutterSDK路徑
複製代碼
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } 複製代碼
implementation project(「:flutter_demo」)
複製代碼
最終代碼(關鍵代碼節選):app
android { ... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation project(":flutter_demo") ... } 複製代碼
val componentName = ComponentName(this, "com.example.flutter_demo.MainActivity") val intent = Intent().setComponent(componentName) startActivity(intent) 複製代碼