欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

go打包aar及flutter調(diào)用aar流程詳解

 更新時(shí)間:2023年03月19日 10:06:15   作者:ClzSkywalker  
這篇文章主要為大家介紹了go打包aar及flutter調(diào)用aar流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

一、目的

本篇文章的目的是記錄本人使用flutter加載與調(diào)用第三方aar包。

二、背景

本人go后端,業(yè)余時(shí)間喜歡玩玩flutter。一直有一個(gè)想法,go可以編譯為第三方平臺(tái)的可執(zhí)行程序,而flutter可以是一個(gè)用于開發(fā)跨平臺(tái)UI的工具,如果開發(fā)一個(gè)程序,go用于后臺(tái)服務(wù),flutter只用于描繪UI,是否可以做到。

查詢了下github上的開源項(xiàng)目,有幾個(gè)類似的:

上述三個(gè),大致都是將flutter做為一個(gè)跨平臺(tái)的UI工具來進(jìn)行使用(思源不是flutter),然后使用第三方語言實(shí)現(xiàn)基本業(yè)務(wù)邏輯。

三、流程

問題:

  • go如何打包為移動(dòng)端的包
  • flutter如何調(diào)用該包

問題一:go如何打包為移動(dòng)端的包

1.環(huán)境配置

第一步需要解決的是環(huán)境配置,想打包安卓的包,肯定需要安卓的工具。

下載android studio

打開SDK Tools 工具庫(kù),安裝NDK,請(qǐng)務(wù)必安裝該版本:21.0.6113669

NDK解釋:

Native Development Kit,是 Android的一個(gè)工具開發(fā)包

快速開發(fā)C、 C++的動(dòng)態(tài)庫(kù),并自動(dòng)將so和應(yīng)用一起打包成 APK,即可通過NDKAndroid中 使用JNI與本地代碼(如C、C++)交互

踩坑:默認(rèn)安裝是23最高版本,打包失敗,請(qǐng)勾選show package details,會(huì)展開更加詳細(xì)的NDK版本,務(wù)必下載21.0.6113669 版本?。?!

2.go配置與打包

golang.org/x/mobile/cmd/gomobile

在項(xiàng)目中執(zhí)行命令:

go build golang.org/x/mobile/cmd/gomobile
gomobile init

使用gomobile庫(kù)可以將go程序打包為移動(dòng)端的包

本項(xiàng)目程序截圖:

在cmd/mobile中有一個(gè)kernel.go文件,該文件就是提供給移動(dòng)端方法調(diào)用的入口StartKernel,里面是啟動(dòng)一個(gè)協(xié)程,該協(xié)程中會(huì)啟動(dòng)對(duì)應(yīng)的http服務(wù)。

在我本地,我增加了一個(gè)構(gòu)建安卓aar包的腳本

#!/usr/bin/env bash
# 構(gòu)建移動(dòng)端腳本
CRTDIR=$(pwd)
# 判斷是否有output文件夾
if [ ! -d "${CRTDIR}/output" ]; then
  mkdir ${CRTDIR}/output
fi
# gomobile bind [-target android|ios|iossimulator|macos|maccatalyst] [-bootclasspath <path>] [-classpath <path>] [-o output] [build flags] [package]
# gomobile bind ./kernel/
gomobile bind -target=android -o=./output/mobile.aar -ldflags '-s -w'  ./cmd/mobile

執(zhí)行該腳本,本地output會(huì)生成兩文件:

  • mobile-sources.jar -- 具體實(shí)現(xiàn)的可以看該包,內(nèi)部提供了一些靜態(tài)本地方法
  • mobile.aar -- 我們真正需要的包

mobile-sources.jar內(nèi)容:

// Code generated by gobind. DO NOT EDIT.
// Java class mobile.Mobile is a proxy for talking to a Go program.
//
//   autogenerated by gobind -lang=java github.com/clz.skywalker/event.shop/kernal/cmd/mobile
package mobile;
import go.Seq;
public abstract class Mobile {
	static {
		Seq.touch(); // for loading the native library
		_init();
	}
	private Mobile() {} // uninstantiable
	// touch is called from other bound packages to initialize this package
	public static void touch() {}
	private static native void _init();
	public static native void startKernel(long port, long local, String mode, String dbPath, String logPath);
}

好了,現(xiàn)在我們已經(jīng)拿到了aar包了。

問題二:flutter如何調(diào)用aar

找半天文章,沒有看到flutter直接調(diào)用aar包,如果你找到了請(qǐng)告訴我。

我現(xiàn)在的解決方案是參考官網(wǎng)的:用寫插件的方式去實(shí)現(xiàn),安卓加載aar,然后flutter再調(diào)用。

第一步:存放aar與修改gradle配置

在android文件夾下的app/libs 中放入mobile.aar文件,如果沒有l(wèi)ibs文件夾的話就創(chuàng)一個(gè)。

編輯app/build.gradle文件,增加如下代碼:

dependencies {
    // implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    // implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    // implementation files('libs/kernel.aar')
    implementation(name:'mobile',ext:'aar')
}

注釋的是本人嘗試后有問題的使用方式,本人非安卓開發(fā)人員,不是很清楚為什么不能那么使用,如果你知道的話可以告訴下我,沒有注釋的是本人親試沒問題的加載方式。

第二步:修改MainActivity.java入口代碼

參考該文章,實(shí)現(xiàn) configureFlutterEngine 方法,通過向 configureFlutterEngine 注冊(cè)方法,可以實(shí)現(xiàn)調(diào)用native的方法。

MethodChannel的名字與flutter代碼約定好,必須一模一樣。

package github.com/ClzSkywalker;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import java.util.Objects;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;
// 引入go打包的aar庫(kù)
import mobile.Mobile;
public class MainActivity extends FlutterActivity {
    // 約定通道的名稱,flutter可以通過通道名調(diào)用對(duì)應(yīng)的方法
    private static final String CHANNEL = "kernel.startKernel";
    private static boolean kernelIsRunning = false;
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
      GeneratedPluginRegistrant.registerWith(flutterEngine);
      new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
              .setMethodCallHandler(
                      (call, result) -> {
                          if (call.method.contentEquals("startKernel")) {
                            if (kernelIsRunning) {
                              result.success("");
                              return;
                            }
                            long port= Long.parseLong(Objects.requireNonNull(call.argument("port")).toString());
                              long local= Long.parseLong(Objects.requireNonNull(call.argument("local")).toString());
                              String mode= Objects.requireNonNull(call.argument("mode")).toString();
                              String dbPath= Objects.requireNonNull(call.argument("dbPath")).toString();
                              String logPath= Objects.requireNonNull(call.argument("logPath")).toString();
                              new Thread(() -> {
                                  // 調(diào)用aar中的方法
                                  Mobile.startKernel(port,local,mode,dbPath,logPath);
                              }).start();
                            kernelIsRunning=true;
                            result.success("");
                          }else{
                              result.notImplemented();
                          }
                      }
              );
  }
}

第三步:flutter調(diào)用

簡(jiǎn)短寫一下,調(diào)用還是挺簡(jiǎn)單的,MethodChannel("name"),name的名字必須要與java中約定的通道名稱一致。

    static const channel = MethodChannel('kernel.startKernel');
    kernelMap['port'] = 4935;
    kernelMap['local'] = 0;
    if (kDebugMode) {
      kernelMap['mode'] = 'test';
    } else {
      kernelMap['mode'] = 'release';
    }
    kernelMap['dbPath'] = dirPath;
    kernelMap['logPath'] = logPath.path;
    await channel.invokeMethod<void>('startKernel', kernelMap);

四、結(jié)論

總的來說難度沒有那么大,在過去的時(shí)候嘗試過類似操作,不過一直想的是flutter直接調(diào)用第三方平臺(tái)庫(kù),錯(cuò)誤的思路實(shí)現(xiàn)起來阻塞重重。

如果要調(diào)用第三方庫(kù),可以嘗試做成一個(gè)flutter插件。

以上就是go打包aar及flutter調(diào)用aar流程詳解的詳細(xì)內(nèi)容,更多關(guān)于go打包aar flutter調(diào)用aar的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Golang使用協(xié)程實(shí)現(xiàn)批量獲取數(shù)據(jù)

    Golang使用協(xié)程實(shí)現(xiàn)批量獲取數(shù)據(jù)

    服務(wù)端經(jīng)常需要返回一個(gè)列表,里面包含很多用戶數(shù)據(jù),常規(guī)做法當(dāng)然是遍歷然后讀緩存。使用Go語言后,可以并發(fā)獲取,極大提升效率,本文就來聊聊具體的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助
    2023-02-02
  • 一文帶你深入了解Golang中的Mutex

    一文帶你深入了解Golang中的Mutex

    這篇文章主要為大家詳細(xì)介紹了Golang中Mutex的相關(guān)知識(shí),知其然,更要知其所以然。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-03-03
  • Golang爬蟲框架 colly的使用

    Golang爬蟲框架 colly的使用

    本文主要介紹了Golang爬蟲框架 colly的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 使用go mod導(dǎo)入本地自定義包問題

    使用go mod導(dǎo)入本地自定義包問題

    這篇文章主要介紹了使用go mod導(dǎo)入本地自定義包問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 詳解如何使用Golang實(shí)現(xiàn)Cron定時(shí)任務(wù)

    詳解如何使用Golang實(shí)現(xiàn)Cron定時(shí)任務(wù)

    定時(shí)任務(wù)是許多應(yīng)用程序中常見的一種需求,它們可以用于執(zhí)行定期的清理任務(wù),發(fā)送通知,生成報(bào)告等,在這篇博客中,我們將介紹如何在Go語言中使用robfig/cron包來實(shí)現(xiàn)Cron定時(shí)任務(wù),需要的朋友可以參考下
    2024-04-04
  • Go基于struct?tag實(shí)現(xiàn)結(jié)構(gòu)體字段級(jí)別的訪問控制

    Go基于struct?tag實(shí)現(xiàn)結(jié)構(gòu)體字段級(jí)別的訪問控制

    本文將會(huì)基于這個(gè)主題展開,討論Go中的結(jié)構(gòu)體tag究竟是什么,我們?cè)撊绾卫盟?另外,文末還提供了一個(gè)實(shí)際案例,實(shí)現(xiàn)結(jié)構(gòu)體字段級(jí)別的訪問,幫助我們進(jìn)一步提升對(duì)struct tag的理解
    2024-02-02
  • 深入探究Go語言中for?range語句

    深入探究Go語言中for?range語句

    為了更加便捷地遍歷這些數(shù)據(jù)類型,Go語言引入了for...range語句,本文將以數(shù)組遍歷為起點(diǎn),逐步介紹for...range語句在不同數(shù)據(jù)類型中的應(yīng)用,希望對(duì)大家有所幫助
    2023-06-06
  • Go中Context使用源碼解析

    Go中Context使用源碼解析

    這篇文章主要為大家介紹了Go中Context使用源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Go語言使用Json的方法實(shí)現(xiàn)

    Go語言使用Json的方法實(shí)現(xiàn)

    本文主要介紹了Go語言使用Json的方法實(shí)現(xiàn)
    2024-05-05
  • 詳解go語言判斷管道是否關(guān)閉的常見誤區(qū)

    詳解go語言判斷管道是否關(guān)閉的常見誤區(qū)

    這篇文章主要想和大家一起探討一下在Go語言中,我們是否可以使用讀取管道時(shí)的第二個(gè)返回值來判斷管道是否關(guān)閉,文中的示例代碼講解詳細(xì),有興趣的可以了解下
    2023-10-10

最新評(píng)論