乐趣区

关于android:flutter添加到android原生

1. 创立一个 flutter module

flutter create -t module --org com.example my_flutter

2. 在原生 build.gradle 下

android {
  //...
  compileOptions {
    sourceCompatibility 1.8
    targetCompatibility 1.8
  }
}

3. 增加 Flutter 模块作为依赖项

有两种形式

(1)无需装置 flutter sdk

Flutter 库打包为由 AAR 和 POM 工件组成的通用本地 Maven 存储库,

(2)须要装置 sdk

原生 settings.gradle 下增加

// Include the host app project.
include ':app'                                    // assumed existing content
setBinding(new Binding([gradle: this]))                                // new
evaluate(new File(                                                     // new
  settingsDir.parentFile,                                              // new
  'my_flutter/.android/include_flutter.groovy'                         // new
))  

原生 build.gradle 下

dependencies {implementation project(':flutter')
}

4.AndroidManifest.xml

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

@style/LaunchTheme 批改

5. 启动

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity

class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)
        FlutterActivity.createDefaultIntent(this)
        startActivity(
            FlutterActivity
                .withNewEngine()
                .initialRoute("/")
                .build(this)
        )
    }
}
退出移动版