Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Gomobile builds
resolve `latest` by default; set `LIBXRAY_GOMOBILE_VERSION` to select a Go module
version. Both `gomobile` and `gobind` use that resolved version.

Set `LIBXRAY_ANDROID_API` to raise the Android API level (default 21), which is
also the AAR's `minSdkVersion`. The libraries' relocations then take less space:
packed (`--pack-dyn-relocs=android`) from 23, RELR (`DT_ANDROID_RELR`) from 28.

Linux and Windows builds also produce `bin/xray` or `bin/xray.exe`. This
session Core can protect Go DNS lookups from the VPN route and accepts:

Expand Down
10 changes: 8 additions & 2 deletions build/app/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def before_build(self):
self.prepare_gomobile()

def build(self):
api = int(os.environ.get("LIBXRAY_ANDROID_API") or 21)
extldflags = "-Wl,-z,max-page-size=16384"
if api >= 28: # DT_ANDROID_RELR, which Android loads since API 28
extldflags += ",--pack-dyn-relocs=relr,--use-android-relr-tags"
elif api >= 23: # DT_ANDROID_REL(A), which Android loads since API 23
extldflags += ",--pack-dyn-relocs=android"
self.snapshot_go_env()
try:
self.before_build()
Expand All @@ -27,10 +33,10 @@ def build(self):
"-target",
"android",
"-androidapi",
"21",
str(api),
"-trimpath",
"-ldflags=-checklinkname=0 -s -w -buildid= "
"-extldflags=-Wl,-z,max-page-size=16384",
f"-extldflags={extldflags}",
]
)
if ret.returncode != 0:
Expand Down
19 changes: 19 additions & 0 deletions build/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ def test_cli_passes_options_to_every_builder(self):
)
builder.return_value.build.assert_called_once_with()

def test_android_api_selects_relocation_packing(self):
for value, api, packing in (
("", "21", ""),
("22", "22", ""),
("23", "23", ",--pack-dyn-relocs=android"),
("28", "28", ",--pack-dyn-relocs=relr,--use-android-relr-tags"),
):
with (
self.subTest(value=value),
patch.dict("app.android.os.environ", {"LIBXRAY_ANDROID_API": value}),
patch.object(self.builder, "before_build"),
patch("app.android.os.chdir"),
patch("app.android.subprocess.run", return_value=subprocess.CompletedProcess([], 0)) as run,
):
self.builder.build()
args = run.call_args.args[0]
self.assertEqual(args[args.index("-androidapi") + 1], api)
self.assertTrue(args[-1].endswith("-extldflags=-Wl,-z,max-page-size=16384" + packing))

def test_gomobile_and_gobind_use_the_same_resolved_version(self):
version = "v0.0.0-20260821190718-4776eadac327"
for requested in ("", version):
Expand Down
4 changes: 4 additions & 0 deletions readme/README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ gomobile 默认解析 `latest`,
也可通过环境变量 `LIBXRAY_GOMOBILE_VERSION` 指定 Go 模块版本;`gomobile` 与
`gobind` 使用同一个解析版本。

设置 `LIBXRAY_ANDROID_API` 可提高 Android API 级别(默认 21),该值也是 AAR 的
`minSdkVersion`。此时库的重定位占用空间更小:不低于 23 时为压缩格式
(`--pack-dyn-relocs=android`),不低于 28 时为 RELR(`DT_ANDROID_RELR`)。

Linux 和 Windows 构建还会生成 `bin/xray` 或 `bin/xray.exe`。该会话 Core
可保护 Go DNS 查询不被 VPN 路由重新捕获,并接受以下命令:

Expand Down