diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0532157f3..08a7e2a7f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -623,7 +623,7 @@ jobs: - name: Build run: |- mkdir clients/android/app/libs - cp libbox.aar clients/android/app/libs + cp *.aar clients/android/app/libs cd clients/android ./gradlew :app:assemblePlayRelease :app:assembleOtherRelease env: @@ -705,7 +705,7 @@ jobs: run: |- go run -v ./cmd/internal/update_android_version --ci mkdir clients/android/app/libs - cp libbox.aar clients/android/app/libs + cp *.aar clients/android/app/libs cd clients/android echo -n "$SERVICE_ACCOUNT_CREDENTIALS" | base64 --decode > service-account-credentials.json ./gradlew :app:publishPlayReleaseBundle diff --git a/cmd/internal/build_libbox/main.go b/cmd/internal/build_libbox/main.go index f76ea4929..6a0ccd08f 100644 --- a/cmd/internal/build_libbox/main.go +++ b/cmd/internal/build_libbox/main.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "path/filepath" + "strconv" "strings" _ "github.com/sagernet/gomobile" @@ -69,9 +70,27 @@ func init() { debugTags = append(debugTags, "debug") } -func buildAndroid() { - build_shared.FindSDK() +type AndroidBuildConfig struct { + AndroidAPI int + OutputName string + Tags []string +} +func filterTags(tags []string, exclude ...string) []string { + excludeMap := make(map[string]bool) + for _, tag := range exclude { + excludeMap[tag] = true + } + var result []string + for _, tag := range tags { + if !excludeMap[tag] { + result = append(result, tag) + } + } + return result +} + +func checkJavaVersion() { var javaPath string javaHome := os.Getenv("JAVA_HOME") if javaHome == "" { @@ -87,21 +106,24 @@ func buildAndroid() { if !strings.Contains(javaVersion, "openjdk 17") { log.Fatal("java version should be openjdk 17") } +} - var bindTarget string +func getAndroidBindTarget() string { if platform != "" { - bindTarget = platform + return platform } else if debugEnabled { - bindTarget = "android/arm64" - } else { - bindTarget = "android" + return "android/arm64" } + return "android" +} +func buildAndroidVariant(config AndroidBuildConfig, bindTarget string) { args := []string{ "bind", "-v", + "-o", config.OutputName, "-target", bindTarget, - "-androidapi", "21", + "-androidapi", strconv.Itoa(config.AndroidAPI), "-javapkg=io.nekohasekai", "-libname=box", } @@ -112,34 +134,59 @@ func buildAndroid() { args = append(args, debugFlags...) } - tags := append(sharedTags, memcTags...) - if debugEnabled { - tags = append(tags, debugTags...) - } - - args = append(args, "-tags", strings.Join(tags, ",")) + args = append(args, "-tags", strings.Join(config.Tags, ",")) args = append(args, "./experimental/libbox") command := exec.Command(build_shared.GoBinPath+"/gomobile", args...) command.Stdout = os.Stdout command.Stderr = os.Stderr - err = command.Run() + err := command.Run() if err != nil { log.Fatal(err) } - const name = "libbox.aar" copyPath := filepath.Join("..", "sing-box-for-android", "app", "libs") if rw.IsDir(copyPath) { copyPath, _ = filepath.Abs(copyPath) - err = rw.CopyFile(name, filepath.Join(copyPath, name)) + err = rw.CopyFile(config.OutputName, filepath.Join(copyPath, config.OutputName)) if err != nil { log.Fatal(err) } - log.Info("copied to ", copyPath) + log.Info("copied ", config.OutputName, " to ", copyPath) } } +func buildAndroid() { + build_shared.FindSDK() + checkJavaVersion() + + bindTarget := getAndroidBindTarget() + + // Build main variant (SDK 23) + mainTags := append([]string{}, sharedTags...) + mainTags = append(mainTags, memcTags...) + if debugEnabled { + mainTags = append(mainTags, debugTags...) + } + buildAndroidVariant(AndroidBuildConfig{ + AndroidAPI: 23, + OutputName: "libbox.aar", + Tags: mainTags, + }, bindTarget) + + // Build legacy variant (SDK 21, no naive outbound) + legacyTags := filterTags(sharedTags, "with_naive_outbound") + legacyTags = append(legacyTags, memcTags...) + if debugEnabled { + legacyTags = append(legacyTags, debugTags...) + } + buildAndroidVariant(AndroidBuildConfig{ + AndroidAPI: 21, + OutputName: "libbox-legacy.aar", + Tags: legacyTags, + }, bindTarget) +} + func buildApple() { var bindTarget string if platform != "" {