V2Ray 编译 Android aar

V2Ray 在实现上使用 go 语言,同时客户端和服务端共用同一套代码,根据配置文件的不同来判断是客户端还是服务端。如果想在 Android 的客户端上运行 V2Ray,那么就需要知道如何编译 v2ray-core 到 aar 库,提供给 Android 客户端。

1. 未解决问题

1.1. gomobile 不支持 Go module 编译

如果在使用 gomobile 编译时,使用 Go module(也就是 GO111MODULE=on ) 会报错,找不到 package, 参考 https://github.com/golang/go/wiki/Mobile
gomobile-not-support-go-module

2. 提前准备

因为编译需要使用到 tun2socks 组件,所以建议先阅读一下相关的知识点。
http://arloor.com/posts/other/android-vpnservice-and-vpn-dev/
https://briteming.blogspot.com/2017/09/socksvpn-fqrouter.html

3. 配置环境

3.1. Mac OS

3.1.1. install android-sdk && android-ndk 环境

最简单的办法,直接安装 Android Studio,然后在 Android Studio 中配置:
android-studio-sdk
android-studio-ndk

其中,SDK Platforms 下载几个 Android 的版本。

3.1.2. 安装 GO

go 我使用的版本: go version go1.13.1 darwin/amd64

3.1.3. clone 仓库

1
2
# clone 仓库到本地
git clone https://github.com/yuanmomo/AndroidLibV2rayLite.git

3.1.4. 执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
###### 使用梯子
export ALL_PROXY=socks5://127.0.0.1:1086
export all_proxy=socks5://127.0.0.1:1086

###### 配置 android 的 sdk 和 ndk
export ANDROID_HOME="/Users/MoMo/Library/Android/sdk"
export ANDROID_NDK_HOME="/Users/MoMo/Library/Android/sdk/ndk/20.0.5594570"

###### 根据环境选择执行脚本:
# Mac OS
sh build/build-on-mac.sh

# 编译 android
sh build-on-mac.sh android [data] [dep]
### data 参数表示会更新 geoip.dat geosite.dat 文件
### dep 表示会执行 go get -u 更新本地的依赖

3.2. Linux(Ubuntu/Debian/CentOS)

3.2.1. clone 仓库

1
2
3
4
5
6
7
8
# clone 仓库到本地
git clone https://github.com/yuanmomo/AndroidLibV2rayLite.git

# 执行脚本
/bin/bash build/build-on-linux.sh sdk data dep
### sdk 表示会安装 Android SDK Tool 和 NDK
### data 参数表示会更新 geoip.dat geosite.dat 文件
### dep 表示会执行 go get -u 更新本地的依赖

3.2.2. 编译过程

  1. 安装 Docker
  2. 安装 Go
  3. 安装 Android SDK + NDK + Tools + OpenJDK8
  4. Docker 拉取 yuanmomo/android-v2ray-build:1.0.0 镜像后开始编译

3.3. 注意事项

  1. 注意需要梯子
  2. 脚本默认 GOATH = ~/go 目录
  3. 会自动下载 geoip.data 和 geosite.dat 文件
  4. 编译生成的 aar 和 source 在 ${GOPATH}/src/AndroidLibV2rayLite 目录下

Mac OS :
mac-os-build-output

Linux

Just for my love !!
6 评论
dewacuan Chrome 108.0.0.0 Windows 8.1
2022-12-07回复

halloo// i need help .. i want to build v2ray in android studio but i dont find any tutorial on google, i use translate to find your website.. hopely you can help me.. thanks.. lot
my telegram @dilombok

Anonymous Chrome 89.0.4389.90 Windows 8.1
2021-03-22回复

老大,搞个 xray-core 的 aar 库编译教程

Anonymous Edge 85.0.564.51 Windows 10.0
2020-09-15回复

Hi again,
Thanks for the advice earlier.
There are a couple of new issues:

  1. The latest v2raycore has moved to the new v2fly directory
  2. The latest v2raycore has moved up to 4.28.1 which requires go 1.15.

Will these require any change in the Androidlibv2raylite code?
Many thanks in advance.

Anonymous Chrome 84.0.4147.135 macOS 10.15.2
2020-08-25回复

您好,使用该脚本在mac无法编译成功啊

package v2ray.com/core/main/distro/all: unrecognized import path “v2ray.com/core/main/distro/all”: fetching v2ray.com/core: Get “http://www.v2ray.com/core/?go-get=1": redirected from secure URL https://v2ray.com/core?go-get=1 to insecure URL http://www.v2ray.com/core/?go-get=1

Vince Chrome 84.0.4147.111 Android 9
2020-08-09回复

Thanks for the advice. I managed to build the libv2ray.aar based on your instruction but it seems the latest libv2ray.aar is incompatible with the v2rayng client code base. Since we can only build libv2ray.aar using latest v2ray version, is there any instruction that gives us some pointers how to modify the v2rayng code base to work with the latest libv2ray.aar ?

vince Chrome 80.0.3987.122 Linux
2020-02-26回复

I am trying to pull this docker image but it says a username and password are required:
Docker 拉取 yuanmomo/android-v2ray-build:1.0.0 镜像后开始编译

Just wonder what is inside this image? how could i create it myself?
Many thanks in advance for any advice.

Anonymous Chrome 84.0.4147.89 macOS 10.15.6
2020-07-18回复

@vince , Sorry for the late response.

Actually, this docker image doesn’t need add permissions. You can pull it every where directly.

If you want to build it yourself, try to follow this Docker file, thx.

https://github.com/yuanmomo/AndroidLibV2rayLite/blob/master/build/docker/Dockerfile

Powered By Valine
v1.4.16