在Android原生项目中集成Flutter

基本介绍

flutter是google的跨平台开发框架, 他和react native及weex框架的最大差别是flutter不使用原生控件而使用gdi按帧渲染, 个人理解非常类似windows时代的DirectUI。本文将介绍如何在一个已有的安卓项目中添加flutter, 使得部分新功能采用flutter

移植的基本思想是新建一个flutter功能 和安卓工程对比看看差异在哪里, 并从flutter工程复制一些代码到安卓工程

操作过程
目录结构
一个android studio工程的目录结构是这样的

project 
├─local.properties 
├─build.gradle 
├─settings.gradle 
├─app 
│ ├─build 
│ ├─libs 
│ └─src 
└─gradle 
└─wrapper

而flutter的目录结构是:

flutter 
├─android 对应安卓工程的project目录 
├─build 
├─ios 
├─lib 
└─pubspec.yaml

1, 所以第一步, 把project目录名改成android
工程修改
2.复制pubspec.yaml和lib文件到跟android并列
3.修改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>

如果这边修改没成功运行时会报错如下

E/FlutterMain(13694): Flutter initialization failed.
E/FlutterMain(13694): java.lang.NullPointerException: Attempt to invoke virtual method 'void io.flutter.view.ResourceExtractor.waitForCompletion()' on a null object reference

4.修改settings.gradle

include ':app'

def flutterProjectRoot = rootProject.projectDir.parentFile.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
}

如果这步修改失败会提示GeneratedPluginRegistrant文件编译出错:
错误: 程序包io.flutter.plugins.pathprovider不存在
错误: 程序包io.flutter.plugins.sharedpreferences不存在

5.修改project的build.gradle,加入下面这一段代码:

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

6.修改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.")
}

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


android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "flutter_app2.yourcompany.com.myapplication"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

这个别忘了
flutter {
    source '../..'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

7.修改local.properties加入fluttersdk的路径

ndk.dir=E\:\\software\\android-sdk-windows\\ndk-bundle
sdk.dir=E\:\\software\\android-sdk-windows
flutter.sdk=D:\\flutter
flutter.buildMode=debug

然后,修改MainActivity.java

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);
    }
}

修改这8点内容后,就可以在Android原生项目中运行Fluuter代码了。

    原文作者:xiangzhihong8
    原文地址: https://blog.csdn.net/xiangzhihong8/article/details/81008152
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞