Skip to content

Commit

Permalink
Merge pull request #8 from bobrofon/main
Browse files Browse the repository at this point in the history
Fix build with CGO_ENABLED=0 GOOS=android
  • Loading branch information
wlynxg authored Oct 4, 2024
2 parents a79b5ca + 4173812 commit 839bc3a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
7 changes: 7 additions & 0 deletions android_api_level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !(android && cgo)

package anet

func androidDeviceApiLevel() int {
return -1
}
23 changes: 23 additions & 0 deletions android_api_level_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build android && cgo

package anet

// #include <android/api-level.h>
import "C"

import "sync"

var (
apiLevel int
once sync.Once
)

// Returns the API level of the device we're actually running on, or -1 on failure.
// The returned value is equivalent to the Java Build.VERSION.SDK_INT API.
func androidDeviceApiLevel() int {
once.Do(func() {
apiLevel = int(C.android_get_device_api_level())
})

return apiLevel
}
1 change: 0 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) {
return ifi.Addrs()
}

// Deprecated: Android version is detected automatically.
func SetAndroidVersion(version uint) {}
44 changes: 26 additions & 18 deletions interface_android.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package anet

// #include <android/api-level.h>
import "C"

import (
"bytes"
"errors"
Expand All @@ -19,6 +16,7 @@ const (
)

var (
customAndroidApiLevel = -1
errInvalidInterface = errors.New("invalid network interface")
errInvalidInterfaceIndex = errors.New("invalid network interface index")
errInvalidInterfaceName = errors.New("invalid network interface name")
Expand Down Expand Up @@ -125,6 +123,31 @@ func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) {
return ifat, err
}

// SetAndroidVersion set the Android environment in which the program runs.
// The Android system version number can be obtained through
// `android.os.Build.VERSION.RELEASE` of the Android framework.
// If version is 0 the actual version will be detected automatically if possible.
func SetAndroidVersion(version uint) {
switch {
case version == 0:
customAndroidApiLevel = -1
case version >= 11:
customAndroidApiLevel = android11ApiLevel
default:
customAndroidApiLevel = 0
}
}

func androidApiLevel() int {
if customAndroidApiLevel != -1 {
// user-provided api level should be used
return customAndroidApiLevel
}

// try to autodetect api level
return androidDeviceApiLevel()
}

// An ipv6ZoneCache represents a cache holding partial network
// interface information. It is used for reducing the cost of IPv6
// addressing scope zone resolution.
Expand Down Expand Up @@ -417,18 +440,3 @@ func nameToFlags(name string) (net.Flags, error) {

return linkFlags(*(*uint32)(unsafe.Pointer(&ifr[syscall.IFNAMSIZ]))), nil
}

// Returns the API level of the device we're actually running on, or -1 on failure.
// The returned value is equivalent to the Java Build.VERSION.SDK_INT API.
var androidApiLevel = func() func() int {
var apiLevel int
var once sync.Once

return func() int {
once.Do(func() {
apiLevel = int(C.android_get_device_api_level())
})

return apiLevel
}
}()

0 comments on commit 839bc3a

Please sign in to comment.