Android原生项目引入flutter_module

315 阅读1分钟

总体思路:1、先创建flutter_moudule工程,在module工程中进行flutter功能的开发;

          2、在同一文件加下,创建原生项目;

                  3、在原生项目中,引入module工程,2种方式,建议使用第一种:

第一种:

手动配置:a、在settings.gradle中加


setBinding(new Binding([gradle: this]))                                // new
evaluate(new File(                                                     // new
        settingsDir.parentFile,                                              // new
        '../zxym/mix_zxym/.android/include_flutter.groovy'                         // new
))

b、在APP下的build.gradle文件中添加: implementation project(path: ':flutter')

第二种:直接导入

基于Android Studio Bumblebee的大黄蜂版本。这个版本下的gradle加载与flutter_module的导入存在冲突,需要修改gradle加载方式。

操作步骤:

1、创建原生项目

2、创建flutter_module

3、确保两个项目在同一文件夹,方便后面的路径配置

4、大黄蜂版本下,gradle编译冲突处理

   a、项目下build.gradle文件整改

         由

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.1.2' apply false
    id 'com.android.library' version '7.1.2' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

换成

buildscript {
    ext.kotlin_version = '1.6.10'

    repositories {
        google()
        gradlePluginPortal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
        classpath "com.google.gms:google-services:4.3.10"
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'

    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()

    }
}

rootProject.buildDir = '../build'

task clean(type: Delete) {
    delete rootProject.buildDir
}

b、项目下settings.gradle文件整改

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "YsMixProject"
include ':app'

换成

rootProject.name = "YsMixProject"
include ':app'

setBinding(new Binding([gradle: this]))                                // new
evaluate(new File(                                                     // new
        settingsDir.parentFile,                                              // new
        '../mix_test/ys_flutter_module/.android/include_flutter.groovy'                         // new
))

这里提示报红,导包,不用管,是编辑器的问题,不影响使用。

c、app下build.gradle文件中添加

implementation project(path: ':flutter')

配置完后,点击 Sync Now

5、同步完成后会在原生目录上发现目录结构发生了变化,如下:

至此,说明项目导入成功。