安卓老工程集成flutter第二篇

老工程集成flutter有两大方法:

  1. 把老工程完全改成flutter,这种方法能实现dart代码的动态加载,但改动很大, 因为flutter引擎内部写死了android-studio里的一些路径,如果AndroidManifest.xml的路径,改造可以做,但不一定有很好的兼容性
    具体集成流程参考:https://blog.csdn.net/langouster/article/details/80511569

  2. 制作一个纯的flutter工程A,fluuter的开发调试都在这个工程进行,当需要发布到老工程B时,把A工程flutter编译后的平台相关的中间文件复制到B工程,B工程没有dart的调试功能,类似与基础了一个第三方的功能模块 本文介绍此种方法

综合考虑后使用第二种方法更加适合我们,具体流程如下

  1. 老工程导入flutter相关文件
    a.首先编译纯flutter工程A(命令行flutter build apk –release)
    b.把A工程”build\app\intermediates\flutter\relase\assets”文件夹复制到老的非Flutter B工程”app\src\main\assets”下
    c.把A工程下的.flutter-plugins文件复制到B工程目录下,和app目录并列,并根据实际情况编辑文件内的路径
    d.把”version/flutter.gradle”复制到B工程”app\”目录下
    e.复制

  2. 修改setttings.gradle,加入以下代码,用于包含flutter插件用到的第三方包

def flutterProjectRoot = rootProject.projectDir.toPath()

def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
    pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}

plugins.each { name, path ->
    def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
    include ":$name"
    project(":$name").projectDir = pluginDirectory
}
  1. 修改local.properties,加入
flutter.sdk=D:\\flutter  #路径根据实际情况
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1
  1. 修改app/build.gradle,在最后加入

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('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) {
    throw new GradleException("versionCode not found. Define flutter.versionCode in the local.properties file.")
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    throw new GradleException("versionName not found. Define flutter.versionName in the local.properties file.")
}
apply from: 'flutter.gradle'

最好指定一下指令集,flutter安装后默认支持的指令集有armeabi-v7a,armeabi-v8a,如果需要支持armeabi需自己编译引擎

android {
    defaultConfig { //添加这部分
        ndk {
            abiFilters 'armeabi-v7a'    //只生成armv7的so
        }
    }
  1. 修改AndroidManifest.xml文件
    <application  android:name="io.flutter.app.FlutterApplication" 加入这行 android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
  1. 新建一个activity, 集成于FlutterActivity, 参考下面的代码
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;


public class MainActivity extends FlutterActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
    }
}
    原文作者:langouster
    原文地址: https://blog.csdn.net/langouster/article/details/81566668
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞