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
143197func buildApple () {
0 commit comments