• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

flutter调用go

武飞扬头像
yujunlong3919
帮助1

命令

mkdir demo
cd demo
go mod init demo
编写greeting.go
go env -w GOPROXY=https://goproxy.cn
go install golang.org/x/mobile/cmd/gomobile@latest
gomobile init
go get golang.org/x/mobile/bind
gomobile bind -target=android
会生成greeting.aar和greeting-sources.jar

greeting.go


package greeting
func SayHi(text string) string {
    return text
}

引入greeting.aar和使用

创建flutter插件
在android下面创建libs
复制greeting.aar到libs下面
配置android/build.gradle

   
group 'com.app.flutter2goplugin'
version '1.0-SNAPSHOT'

buildscript {
   ext.kotlin_version = '1.7.10'
   repositories {
       谷歌()
       mavenCentral()
   }
   dependencies {
       classpath 'com.android.tools.build:gradle:7.2.0'
       classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
   }
}

allprojects {
   repositories {
       谷歌()
       mavenCentral()

   }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
   compileSdkVersion 31

   compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
   }

   kotlinOptions {
       jvmTarget = '1.8'
   }

   sourceSets {
       main.java.srcDirs  = 'src/main/kotlin'
   }

   defaultConfig {
       minSdkVersion 16
   }
}

rootProject.allprojects{
   repositories {
       谷歌()
       mavenCentral()
       flatDir {
           dirs project(':flutter2goplugin').file('libs')
       }
   }
}


dependencies {
   implementation(name: "greeting", ext: "aar")
}

学新通
android中使用Flutter2gopluginPlugin.kt

package com.app.flutter2goplugin

import android.util.Log
import androidx.annotation.NonNull
import greeting.Greeting

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result


class Flutter2gopluginPlugin : FlutterPlugin, MethodCallHandler {
    
    private lateinit var channel: MethodChannel

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter2goplugin")
        channel.setMethodCallHandler(this)
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        if (call.method == "getPlatformVersion") {
            Log.e("getPlatformVersion", Greeting.sayHi("bbbbbbbbbbb"))
            result.success("Android ${android.os.Build.VERSION.RELEASE}")
        } else {
            result.notImplemented()
        }
    }
    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }
}

学新通
参考文档

完整流程 Flutter 集成 Golang 多语言跨端开发基础案例

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfbahb
系列文章
更多 icon
同类精品
更多 icon
继续加载