使用 gomobile 编译适用于 Android 的 frpc 类库

环境

OS: Ubuntu 18.04
JDK: 11.0.11
Go: 1.16.3
Android SDK: android-30
Android NDK: 22.1.7171670
Frp: 0.36.2

Set env

编辑 /etc/profile

export ANDROID_HOME=/usr/local/android/sdk
export ANDROID_SDK_MANAGER=$ANDROID_HOME/cmdline-tools/latest
export ANDROID_NDK_HOME=$ANDROID_HOME/ndk

export JAVA_HOME=/usr/local/jdk/jdk-11.0.11

export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin

export PATH=$PATH:$JAVA_HOME/bin
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_SDK_MANAGER/bin:$ANDROID_HOME/platform-tools:$ANDROID_NDK_HOME
export PATH=$PATH:$GOBIN

JDK 11

Download JDK11

  1. 解压
  2. jdk-11.0.11 移动至 /usr/local/jdk 目录下

Go

Download Go

  1. 解压
  2. go 移动至 /usr/local 目录下

Android SDK and NDK

Download Android SDK Manager

Android SDK Manager Doc

  1. cmdline-tools 重命名为 latest
  2. latest 目录移动到 /usr/local/android/sdk/cmdline-tools 目录下
  3. 执行 sdkmanager --verbose "platform-tools" "platforms;android-30" "ndk;22.1.7171670"
  4. 为 NDK 建立软连接,便于 gomobile 找到 ln -s /usr/local/android/sdk/ndk/22.1.7171670/ /usr/local/android/sdk/ndk-bundle

gomobile

go get golang.org/x/mobile/cmd/gomobile
go get golang.org/x/mobile/cmd/gobind

Frp

git clone https://github.com/fatedier/frp $(go env GOPATH)/src/github.com/fatedier/frp

编辑 $(go env GOPATH)/src/github.com/fatedier/frp/cmd/frpc/sub/root.go

......

108 - err := runClient(cfgFile)
108 + err := RunClient(cfgFile)
......

182 - func runClient(cfgFilePath string) (err error) {
182 + func RunClient(cfgFilePath string) (err error) {

编辑 $(go env GOPATH)/src/github.com/fatedier/frp/cmd/frpc/main.go

......

15 - package main
15 + package frpclib
......

34 + func Run(cfgFilePath string) error {
35 +     crypto.DefaultSalt = "frp"
36 +     return sub.RunClient(cfgFilePath)
37 + }
cd $(go env GOPATH)/src/github.com/fatedier/frp
go mod download
gomobile init
gomobile bind -target=android ./cmd/frpc
LICENSED UNDER CC BY-NC-SA 4.0
Comment