Skip to content

Commit 10ac789

Browse files
committed
platform: Split library for Android SDK 21 and 23
1 parent 450655b commit 10ac789

File tree

2 files changed

+74
-20
lines changed

2 files changed

+74
-20
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ jobs:
623623
- name: Build
624624
run: |-
625625
mkdir clients/android/app/libs
626-
cp libbox.aar clients/android/app/libs
626+
cp *.aar clients/android/app/libs
627627
cd clients/android
628628
./gradlew :app:assemblePlayRelease :app:assembleOtherRelease
629629
env:
@@ -705,7 +705,7 @@ jobs:
705705
run: |-
706706
go run -v ./cmd/internal/update_android_version --ci
707707
mkdir clients/android/app/libs
708-
cp libbox.aar clients/android/app/libs
708+
cp *.aar clients/android/app/libs
709709
cd clients/android
710710
echo -n "$SERVICE_ACCOUNT_CREDENTIALS" | base64 --decode > service-account-credentials.json
711711
./gradlew :app:publishPlayReleaseBundle

cmd/internal/build_libbox/main.go

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/exec"
77
"path/filepath"
8+
"strconv"
89
"strings"
910

1011
_ "github.com/sagernet/gomobile"
@@ -69,9 +70,27 @@ func init() {
6970
debugTags = append(debugTags, "debug")
7071
}
7172

72-
func buildAndroid() {
73-
build_shared.FindSDK()
73+
type AndroidBuildConfig struct {
74+
AndroidAPI int
75+
OutputName string
76+
Tags []string
77+
}
7478

79+
func filterTags(tags []string, exclude ...string) []string {
80+
excludeMap := make(map[string]bool)
81+
for _, tag := range exclude {
82+
excludeMap[tag] = true
83+
}
84+
var result []string
85+
for _, tag := range tags {
86+
if !excludeMap[tag] {
87+
result = append(result, tag)
88+
}
89+
}
90+
return result
91+
}
92+
93+
func checkJavaVersion() {
7594
var javaPath string
7695
javaHome := os.Getenv("JAVA_HOME")
7796
if javaHome == "" {
@@ -87,21 +106,23 @@ func buildAndroid() {
87106
if !strings.Contains(javaVersion, "openjdk 17") {
88107
log.Fatal("java version should be openjdk 17")
89108
}
109+
}
90110

91-
var bindTarget string
111+
func getAndroidBindTarget() string {
92112
if platform != "" {
93-
bindTarget = platform
113+
return platform
94114
} else if debugEnabled {
95-
bindTarget = "android/arm64"
96-
} else {
97-
bindTarget = "android"
115+
return "android/arm64"
98116
}
117+
return "android"
118+
}
99119

120+
func buildAndroidVariant(config AndroidBuildConfig, bindTarget string) {
100121
args := []string{
101122
"bind",
102123
"-v",
103124
"-target", bindTarget,
104-
"-androidapi", "21",
125+
"-androidapi", strconv.Itoa(config.AndroidAPI),
105126
"-javapkg=io.nekohasekai",
106127
"-libname=box",
107128
}
@@ -112,32 +133,65 @@ func buildAndroid() {
112133
args = append(args, debugFlags...)
113134
}
114135

115-
tags := append(sharedTags, memcTags...)
116-
if debugEnabled {
117-
tags = append(tags, debugTags...)
118-
}
119-
120-
args = append(args, "-tags", strings.Join(tags, ","))
136+
args = append(args, "-tags", strings.Join(config.Tags, ","))
121137
args = append(args, "./experimental/libbox")
122138

123139
command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
124140
command.Stdout = os.Stdout
125141
command.Stderr = os.Stderr
126-
err = command.Run()
142+
err := command.Run()
127143
if err != nil {
128144
log.Fatal(err)
129145
}
130146

131-
const name = "libbox.aar"
147+
const generatedName = "libbox.aar"
148+
if config.OutputName != generatedName {
149+
err = os.Rename(generatedName, config.OutputName)
150+
if err != nil {
151+
log.Fatal(E.Cause(err, "rename output"))
152+
}
153+
}
154+
132155
copyPath := filepath.Join("..", "sing-box-for-android", "app", "libs")
133156
if rw.IsDir(copyPath) {
134157
copyPath, _ = filepath.Abs(copyPath)
135-
err = rw.CopyFile(name, filepath.Join(copyPath, name))
158+
err = rw.CopyFile(config.OutputName, filepath.Join(copyPath, config.OutputName))
136159
if err != nil {
137160
log.Fatal(err)
138161
}
139-
log.Info("copied to ", copyPath)
162+
log.Info("copied ", config.OutputName, " to ", copyPath)
163+
}
164+
}
165+
166+
func buildAndroid() {
167+
build_shared.FindSDK()
168+
checkJavaVersion()
169+
170+
bindTarget := getAndroidBindTarget()
171+
172+
// Build main variant (SDK 23)
173+
mainTags := append([]string{}, sharedTags...)
174+
mainTags = append(mainTags, memcTags...)
175+
if debugEnabled {
176+
mainTags = append(mainTags, debugTags...)
177+
}
178+
buildAndroidVariant(AndroidBuildConfig{
179+
AndroidAPI: 23,
180+
OutputName: "libbox.aar",
181+
Tags: mainTags,
182+
}, bindTarget)
183+
184+
// Build legacy variant (SDK 21, no naive outbound)
185+
legacyTags := filterTags(sharedTags, "with_naive_outbound")
186+
legacyTags = append(legacyTags, memcTags...)
187+
if debugEnabled {
188+
legacyTags = append(legacyTags, debugTags...)
140189
}
190+
buildAndroidVariant(AndroidBuildConfig{
191+
AndroidAPI: 21,
192+
OutputName: "libbox-legacy.aar",
193+
Tags: legacyTags,
194+
}, bindTarget)
141195
}
142196

143197
func buildApple() {

0 commit comments

Comments
 (0)