Android Native 工程集成 Flutter 的两种方式,androidsdk开发封装

  • app 模块下的 build.gradle

android {

}

// 在编译过程中产生的中间产物将会存放在该文件夹下
buildDir = new File(rootProject.projectDir, “…/build/host”)

dependencies {
implementation project(’:flutter’)

}

主工程 app 依赖了子模块 :flutter,但是我们在工程中并没有使用以flutter命名的子模块。

  • Flutter 模块下的 build.gradle

def localProperties = new Properties()
def localPropertiesFile = new File(buildscript.sourceFile.parentFile.parentFile, ‘local.properties’)
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader(‘UTF-8’) { reader ->
localProperties.load(reader)
}
}

def flutterRoot = localProperties.getProperty(‘flutter.sdk’)
if (flutterRoot == null) {
throw new GradleException(“Flutter SDK not found. Define location with flutter.sdk in the local.properties file.”)
}

def flutterVersionCode = localProperties.getProperty(‘flutter.versionCode’)
if (flutterVersionCode == null) {
flutterVersionCode = ‘1’
}

def flutterVersionName = localProperties.getProperty(‘flutter.versionName’)
if (flutterVersionName == null) {
flutterVersionName = ‘1.0’
}

在初次点开该 gradle 文件时会在 GradleException 处标红,不用解决,不影响正常编译。

这边主要是从local.properties获取一些配置数据,如 flutter sdk 位置等等。

apply plugin: ‘com.android.library’
apply from: “$flutterRoot/packages/flutter_tools/gradle/flutter.gradle”

android {
compileSdkVersion 28

defaultConfig {

}
}

flutter {
source ‘…/…’
}

dependencies {

}

在第二行通过apply from 引入了 flutter.gradle 文件,其主要作用是为 Flutter模块引入 flutter 相关依赖,.so文件等等。

注意:flutter 模块要求 compileSdkVersion >= 28,对于很多使用 kotlin 代码且工程 sdk 低于 28 的可能有毁灭性的打击,会报 onCreate() override nothing等类型不匹配的错误,需要一一手动修改错误。

flutter 结构体主要表示flutter源码的位置,../..的意思就是Flutter文件夹下的代码就是源码。

  • .android/setting.gradle

include ‘:app’

rootProject.name = ‘android_generated’
setBinding(new Binding([gradle: this]))
evaluate(new File(settingsDir, ‘include_flutter.groovy’))

打开文件后,Binding类也会报错,此时不要为其 import ,不然会报错,报红不影响编译。

这几行代码的意思就是说,将 Flutter 模块引入到Android工程中。Flutter 模块并没有显示地使用 include ':flutter' ,那为什么 Flutter 模块会以 :flutter 这样的形式被依赖呢?带着问题,我们看看include_flutter.groovy

  • .android/include_flutter.groovy

gradle.include ‘:flutter’
gradle.project(’:flutter’).projectDir
= new File(flutterProjectRoot, ‘.android/Flutter’)

可以看到,文件内仍然是通过 include ':flutter' 语法引入到 Android 工程内,同时为其指定模块位置为 Flutter 文件夹

if (System.getProperty(‘build-plugins-as-aars’) != ‘true’) {
def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot, ‘.flutter-plugins’)
if (pluginsFile.exists()) {
pluginsFile.withReader(‘UTF-8’) { reader -> plugins.load(reader) }
}

plugins.each { name, path ->
def pluginDirectory = flutterProjectRoot.toPath().resolve(path).resolve(‘android’).toFile()
gradle.include “: n a m

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值