当前位置: 首页 > news >正文

用Go语言写Android应用 (2) - 从Android的Java调用Go代码

用Go语言写Android应用 (2) - 从Android的Java调用Go代码

上一篇我们讲到,Go在Android中的作用,就相当于NDK中的C/C++。上节我们学习了参照NDK的方式用纯Go语言来写应用。
但是,也正如在Android中,C/C++主要是通过JNI的方式被Java代码调用,本节我们就学习如何使用Java代码来调用Go代码。

Java调Go的JNI例子

Java部分

我们首先来看这个简单得不能再简单的Java部分的代码,只有一个TextView,然后调用Go写的Hello.Greetings函数返回一个字符串,显示在这个TextView上。

package org.golang.example.bind;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import go.hello.Hello;


public class MainActivity extends Activity {

    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.mytextview);

        // Call Go function.
        String greetings = Hello.Greetings("Android and Gopher");
        mTextView.setText(greetings);
    }
}

布局也没什么好说的,就只有一个TextView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mytextview" />

</RelativeLayout>

Go代码

我们再看看Go代码,也是简单到不能再简单了,把收到的字符串前面加上"Hello, ",然后再返回给Java就完事了。

package hello

import "fmt"

func Greetings(name string) string {
    return fmt.Sprintf("Hello, %s!", name)
}

编译和运行

于是,见证奇迹的时刻来了,我们把这个bind的例子编译运行一下吧。
按照Android的惯例,编译是通过gradle脚本来完成的,我们首先要在build.gradle中配一下Go的环境。

修改GOROOT下面的src/golang.org/x/mobile/example/bind/android/hello/build.gradle,配置GOPATH,GO和GOMOBILE的路径:

plugins {
    id "org.golang.mobile.bind" version "0.2.6"
}

gobind {
    /* The Go package path; must be under one of the GOPATH elements or
     a relative to the current directory (e.g. ../../hello) */
    pkg = "golang.org/x/mobile/example/bind/hello"

    /* GOPATH where the Go package is; check `go env` */
    GOPATH = "D:/go/"

    /* Absolute path to the go binary */
    GO = "d:/go/bin/"

    /* Optionally, set the absolute path to the gomobile binary if the
    /* gomobile binary is not located in the GOPATH's bin directory. */
    GOMOBILE = "d:/go/bin/gomobile"
}

下面我们就可能通过gradle来编译这两家伙,以Windows下为例:
执行下面的命令:

gradlew assembleDebug

这时需要科学上网去下载对应版本的gradle,还需要在环境变量里配置好javac的路径。

然后我们就可以看到欢快的build的日志了:

:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:hello:gobind
:app:prepareAndroidHelloUnspecifiedLibrary
:app:prepareComAndroidSupportAppcompatV72211Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42211Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:prePackageMarkerForDebug
:app:transformClassesWithDexForDebug UP-TO-DATE
:app:mergeDebugJniLibFolders UP-TO-DATE
:app:transformNative_libsWithMergeJniLibsForDebug
:app:processDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:validateDebugSigning
:app:packageDebug
:app:zipalignDebug
:app:assembleDebug

BUILD SUCCESSFUL

Total time: 56.107 secs

Build成功之后,apk就生成在GOROOTsrcgolang.orgxmobileexamplebindandroidappbuildoutputsapk下面。
adb install安装下就可以看到我们的成果啦~

当然,如果喜欢用IDE的,可以将工程导入到Android Studio里面,hello下面的build.gradle按上面说的改好,通过Android Studio来生成apk也是一样的。

原理

上面我们很欢快地完成了一个例子,但是背后的事情都是gradle插件和脚本帮我们做了。比JNI还要舒服,因为JNI总还有个Javah工具要生成个头文件,还要写Android.mk的。

我们打开生成的apk或者aar文件就可以看到,GoBind帮我们将我们的代码和相关的包装代码编译进了一个叫libgojni.so的库里面。

下面我们揭开这个工程的神秘面纱,看看我们如何也能够实现从Go到Java的映射,我们有一个神奇的工具gomobile bind.

比如说我们写一个最简单的计数器类吧:

package testCounter

type Counter struct {
    Value int
}

func (c *Counter) Inc() { c.Value++ }

func New() *Counter { return &Counter{100} }

然后我们执行gomobile bind命令:

gomobile bind -target=android testCounter

结果,gomobile命令给我们生成了一个testCounter.aar。
这是一个jar包一样的压缩格式,我们把它解开看看。发现,有classes.jar,还有jni,下面是各种指令集下的so库。

我们反编译一下classes.jar,看看我们上面几行的Go代码变成了什么样子。

先看干货吧,我们的Inc()方法,Value的set和get方法都被声明成了JNI的代码。

    public final native long getValue();

    public final native void setValue(long paramLong);

    public native void Inc();

下面是完整版的:

import go.Seq;
import go.Seq.Proxy;
import go.Seq.Ref;
import java.util.Arrays;

public abstract class TestCounter
{
  public static void touch()
  {
  }

  private static native void init();

  public static native Counter New();

  static
  {
    Seq.touch();
    init();
  }

  public static final class Counter extends Seq.Proxy
  {
    private Counter(Seq.Ref paramRef)
    {
      super(); } 
    public final native long getValue();

    public final native void setValue(long paramLong);

    public native void Inc();

    public boolean equals(Object paramObject) { if ((paramObject == null) || (!(paramObject instanceof Counter))) {
        return false;
      }
      Counter localCounter = (Counter)paramObject;
      long l1 = getValue();
      long l2 = localCounter.getValue();
      if (l1 != l2) {
        return false;
      }
      return true; }

    public int hashCode()
    {
      return Arrays.hashCode(new Object[] { Long.valueOf(getValue()) });
    }

    public String toString() {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("Counter").append("{");
      localStringBuilder.append("Value:").append(getValue()).append(",");
      return "}";
    }
  }
}

所以,gomobile bind的秘密很简单,利用Java与C语言的JNI接口,将Go也封装成C的接口,然后完成两者的对接。

为什么不只是接口转化下这么简单,还要写这么复杂的类呢?答案在于GC。不管是Java还是Go都是有GC的,两种带GC的语言碰到一起,是需要小心行事的。具体的细节可以看下这篇文章:https://docs.google.com/document/d/1y9hStonl9wpj-5VM-xWrSTuEJFUAxGOXOhxvAs7GZHE/edit?pref=2&pli=1

当然,这其中还有很多细节值得讲一讲,我们下节就从Go与C语言的接口:cgo开始讲起吧。

相关文章:

  • RootMe--HTTP - Open redirect
  • SerializeDeserialize
  • Unity3dShader边缘发光效果
  • 利用python jieba库统计政府工作报告词频
  • Azure linux centos 默认登陆账号是什么?
  • TeeChart Pro VCL/FMX教程(一):入门——构建图表
  • Sass 快速入门教程
  • 结对开发石家庄地铁查询系统
  • P2V操作完整步骤,物理机转换openstack虚拟机
  • eclipse中利用hibernate插件,根据数据库表反向生成Javabean
  • 工厂模式
  • 1.XGBOOST算法推导
  • XCode 快捷键
  • Flutter:界面刷新和生命周期
  • OGNL
  • Apache Pulsar 2.1 重磅发布
  • CentOS7 安装JDK
  • idea + plantuml 画流程图
  • interface和setter,getter
  • Linux Process Manage
  • MD5加密原理解析及OC版原理实现
  • mongo索引构建
  • Transformer-XL: Unleashing the Potential of Attention Models
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 测试如何在敏捷团队中工作?
  • 从PHP迁移至Golang - 基础篇
  • 三分钟教你同步 Visual Studio Code 设置
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 微信开源mars源码分析1—上层samples分析
  • 想写好前端,先练好内功
  • 用Python写一份独特的元宵节祝福
  • 白色的风信子
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • ​secrets --- 生成管理密码的安全随机数​
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • (145)光线追踪距离场柔和阴影
  • (Java数据结构)ArrayList
  • (八)c52学习之旅-中断实验
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (五)c52学习之旅-静态数码管
  • (转)socket Aio demo
  • (转)Sql Server 保留几位小数的两种做法
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • .FileZilla的使用和主动模式被动模式介绍
  • .NET Conf 2023 回顾 – 庆祝社区、创新和 .NET 8 的发布
  • .NET 发展历程
  • .NET4.0并行计算技术基础(1)
  • .NET企业级应用架构设计系列之技术选型
  • .project文件
  • @LoadBalanced 和 @RefreshScope 同时使用,负载均衡失效分析
  • @取消转义
  • [\u4e00-\u9fa5] //匹配中文字符
  • [ACL2022] Text Smoothing: 一种在文本分类任务上的数据增强方法