Flutter was added in Android project, to some features Flutter hybrid development programs (with source code)

When we try Flutter, in fact, can be added Flutter in our existing project and transform our part is not particularly important function, to avoid triggering a greater risk, you can put a new technology into the room. When React Native, and we did try a similar program, and later based on the stability and maintenance costs, and ultimately in exchange for native development.

Who is mixed with Flutter development?

APP is a typical busy fish native & Flutter development programs, through Android "show layout border" tool, you can see, busy fish APP product details page, game trading, short term trading are already using Flutter transformation.

2431302-8e8054d4c105ff76.png
Analysis package size increases the cost of analysis (5.1MB increase)

By analyzing the formal apk packet generation, we can see that, to achieve Flutter is mainly implemented in C ++, this will increase the size of 3.5MB, the other is the assets folder, there will increase 1.6 MB, due to the increase in the jar Flutter little code, you can can be ignored. From analysis of the project if added Flutter, will increase the size of around 5.1MB of this size is very significant, not very much.

2431302-bc2e8e4bb03f938a.png
  • isolate_snapshot_data application data segment
  • isolate_snapshot_instr application instruction segment
  • vm_snapshot_data VM virtual machine data segment
  • vm_snapshot_instr VM virtual machine instruction segment
Flutter rely Principle Analysis

We can see by build.gradle file generated by default android project, in fact, added to flutter in our ready-made project support is very simple, the core is flutter.gradle, flutter in the installation package flutter / packages / flutter_tools / gradle can see the file.

apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

packages / flutter_tools / gradle / flutter.gradle
this is the main role of file:

  1. Increase flutter.jar and so dependent.
  2. Flutter Plugin build dependencies plug.
  3. 插入工程编译产物,就是assets目录下的内容,isolate_snapshot_data和vm_snapshot_data。
在现成项目引入Flutter,基础版本教程

由于开发Flutter是需要配置Flutter的环境,在实际的团队当中,并不是所有成员都必须参与到Flutter开发中,非Flutter开发人员也不应该需要配置Flutter开发环境,所以我们只需要将需要的代码引入进来,非Flutter开发人员就不需要配置环境,所以我们只需要复制flutter.jar和libflutter.so和assets文件到我们项目即可。

public final class GeneratedPluginRegistrant {
  public static void registerWith(PluginRegistry registry) {
    if (alreadyRegisteredWith(registry)) {
      return;
    }
  }

  private static boolean alreadyRegisteredWith(PluginRegistry registry) {
    final String key = GeneratedPluginRegistrant.class.getCanonicalName();
    if (registry.hasPlugin(key)) {
      return true;
    }
    registry.registrarFor(key);
    return false;
  }
}

创建一个Activity承载Flutter

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}

在现成项目引入Flutter,升级版本教程

从上面的基础教程,我们其实就已经可以实现在现有的项目中使用Flutter,但是每次都需要复制文件到指定目录,其实我们可以换个方式来实现,就是通过依赖管理实现,我们将flutter.jar和libflutter.so文件,还有assets里面的编译产物一起打包生成aar,然后上传到maven仓库,我们主工程就可以非常简单地通过依赖方式引入,丝毫不会污染原来的工程代码,Flutter开发和原生开发就可以进行了隔离,后续会补充这部分的教程。

dependencies {
    implementation 'com.taoweiji.flutter:aboutme:1.0.0'
}

创建一个Activity承载Flutter

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}

教程

创建android module

在android目录下的app是flutter默认的运行宿主,如果我们需要打包成一个aar,那么我们需要创建一个module来承载,这个module最重要的地方是build.gradle,这个文件的内容复制android/app/build.gradle目录下的文件,把 apply plugin: 'com.android.application' 改成apply plugin: 'com.android.library',并增加groupversion的定义。

//build.gradle
apply plugin: 'com.android.library'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
...略去几十行代码
group = 'com.taoweiji.flutter'
version = '1.0.0-SNAPSHOT'
配置Maven上传

发布aar有两种,一个是本地发布,一个是搭建Maven服务器来实现,我们需要修改android/build.gradle文件

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
    }
}
...
subprojects {
    apply plugin: 'maven'
    uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: uri('/Users/Wiki/repo'))// 填写本地的仓库地址
                //repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                //    authentication(userName: ossrhUsername, password: ossrhPassword)
                //}
            }
        }
    }
}
编写打包发布脚本

Create a script file publish_android_aar.sh flutter in the root directory of the project

#!/usr/bin/env bash
# publish_android_aar.sh

# 我们用于打包aar的module名称
myFlutterModule="myflutter"


echo "Clean old build"
find . -d -name "build" | xargs rm -rf
flutter clean

echo "Get packages"
flutter packages get

echo "Build release AOT"
flutter build aot --release  --output-dir=build/flutteroutput/aot
echo "Build release Bundle"
flutter build bundle --precompiled  --asset-dir=build/flutteroutput/flutter_assets

# 复制插件生成的GeneratedPluginRegistrant.java到我们需要打包的module
echo 'Copy GeneratedPluginRegistrant.java to module'
mkdir -p android/${myFlutterModule}/src/main/java/io/flutter/plugins && cp android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java "$_"

# 将依赖的插件打包发布到本地或者远程的maven仓库,需要修改android/build.gradle
echo 'Build and publish module to repo'
cd android
gradlewScript=""
file="../.flutter-plugins"
while read line
do
    array=(${line//=/ })
    moduleName=${array[0]}
    gradlewScript="$gradlewScript:${moduleName}:clean :${moduleName}:uploadArchives "
done < ${file}
gradlewScript=${gradlewScript}":${myFlutterModule}:clean :${myFlutterModule}:uploadArchives "
echo "./gradlew ${gradlewScript}"
./gradlew ${gradlewScript}
Packaging commands specified

Execute commands on the command line, you can publish aar

sh publish_android_aar.sh

Using ready-made project

The introduction of a local warehouse

build.gradle modify the project root directory

buildscript {
    repositories {
        maven {
            url uri('/Users/Wiki/repo')//填写本地的仓库地址
        }
    }
}
Using ready-made project

build.gradle modify app directory

dependencies {
    implementation "com.taoweiji.flutter:myflutter:1.0.1-SNAPSHOT"
}
Creating an Activity

We need to create an Activity to host the entrance Flutter, I remember oh configuration in AndroidManifest.xml

public class MyFlutterActivity extends FlutterActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 初始化Flutter
        FlutterMain.startInitialization(getApplicationContext());
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
    }
}

We're done here, Flutter development and the development of native separated, is introduced by way of maven.

Complete source code

https://github.com/taoweiji/FlutterDemo

Additional

Flutter Android and call each other, passing parameters

Flutter early attempts: Getting Started Tutorial

Flutter installation tutorial

Guess you like

Origin blog.csdn.net/weixin_33862514/article/details/90867499