go: x/mobile: Calling net.InterfaceAddrs() fails on Android SDK 30
What version of Go are you using (go version)?
$ go version go version go1.14.6 darwin/amd64 $gomobile version gomobile version +973feb4 Sat Aug 1 11:21:45 2020 +0000 (android,ios); androidSDK=/sdk/platforms/android-30
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
go env Output
$ go env GO111MODULE="on" GOARCH="amd64" GOBIN="" GOCACHE="/Users/user/Library/Caches/go-build" GOENV="/Users/user/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/user/golang" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/Cellar/go/1.14.6/libexec" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/Cellar/go/1.14.6/libexec/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/Users/user/golang/src/golang.org/x/mobile/go.mod" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kq/3436m_v11sg0l7zqtmv2r1gw0000gn/T/go-build713467523=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
Calling net.InterfaceAddrs() fails on Android app targetting SDK version 30. With build.gradle looking like:
android {
compileSdkVersion 30
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "com.example.testapp"
minSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
Building the exact same app targetting SDK 29 works, and returns no error:
android {
compileSdkVersion 30
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "com.example.testapp"
minSdkVersion 29
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
What did you expect to see?
Targetting SDK 30 would behave no differently.
What did you see instead?
Calling net.InterfaceAddrs() results in error route ip+net: netlinkrib: permission denied when embedded in Android app targetting SDK 30 ®:
2020-08-04 15:10:21.386 15754-15754/? W/Thread-2: type=1400 audit(0.0:616): avc: denied { bind } for scontext=u:r:untrusted_app:s0:c158,c256,c512,c768 tcontext=u:r:untrusted_app:s0:c158,c256,c512,c768 tclass=netlink_route_socket permissive=0 b/155595000 app=com.example.testapp
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 32
- Comments: 89 (3 by maintainers)
Commits related to this issue
- 1. back to Android API target 29 due to https://github.com/golang/go/issues/40569 — committed to vikulin/crispa-android by vikulin 3 years ago
- Due to golang issue https://github.com/golang/go/issues/40569 , lower down the targetSDKVersion to 29 — committed to mobazha/mobazha by fengzie 3 years ago
- **Release notes:** - Major release which includes a debug APK - targetSDK rolled backed to 29 due to compatibility issues with p2p network calls in golang([go repo](https://github.com/golang/go/issues... — committed to simbadMarino/dCloud by simbadMarino 2 years ago
- README.md: patch to run on Android >= 11 to fix [x/mobile: Calling net.InterfaceAddrs() fails on Android SDK 30](https://github.com/golang/go/issues/40569#issuecomment-1190950966) — committed to flyskywhy/textiot by flyskywhy 2 years ago
- React使用详解.md: Fixed [x/mobile: Calling net.InterfaceAddrs() fails on Android SDK 30](https://github.com/golang/go/issues/40569#issuecomment-1190950966) if use GO on Android >= 11 — committed to flyskywhy/g by flyskywhy 2 years ago
Any updates?
I’m having a question: This problem is open since 2020. There is no workaround. Basically it means, that all Android apps, relying on GO in some way and “doing network things”, will not work if they are running Android 11 or higher (at least the latter is for sure not that exotic anymore as it was in 2020)
I’m wondering why this issue is not making bigger waves, but most likely the relevance of gomobile for Android is lower than expected (which is kind of a pity, since it is in some limited ways a perfect replacement of C++).
But at least network things should be working.
I found a way to solve
net.Interface()andnet.InterfaceAddrs()problems, you can check the repository: https://github.com/wlynxg/anet. Hope this helps everyone to fix this bugBrilliant! Inspired by your work, I looked into how Android implements
getifaddrs, and found thatRTM_GETADDRis used for non-system apps asRTM_GETLINKis forbidden [1]. On the other hand, there are already some code usingRTM_GETADDRin Go [2]. I collected relevant functions, removed usage of forbiddenRTM_GETLINKand got a working version:Pure-Go InterfaceAddrs() for newer Android
[1] https://android.googlesource.com/platform/bionic/+/refs/tags/android-13.0.0_r18/libc/bionic/ifaddrs.cpp#315 [2] https://github.com/golang/go/blob/go1.20rc1/src/net/interface_linux.go#L124
I was able to work around this issue by calling
getifaddrsvia cgo, which was preferable to using JNI for me.getifaddrsis available in Android 7.0+.Code in the gist here replicates most but not all of the stdlib functionality. Some data isn’t populated. https://gist.github.com/iamcalledrob/67b710b1ca09465b906f04b91bb56e1f
For anyone using the technique of having Java code pass a string with an interface list over to the Go code, we did find an issue after some time in the field.
For an Android phone in a Locale like Saudi Arabia which uses Hindu-Arabic numerals, the Java code will automatically format the decimal numbers using the locale-appropriate glyphs:
lo ١ ٦٥٥٣٦ true false true false false |The receiving Go code cannot handle this. As these strings are purely internal to pass information between the Java runtime and Go environment, they will never be visible to the user. We addressed it by setting the locale in the Java code to always format the string in a way which the Go code can handle:
sb.append(String.format(java.util.Locale.ROOT, "%s %d %d %b %b %b %b %b |", nif.getName(),The full PR to address it is in https://github.com/tailscale/tailscale-android/commit/fd42b4b3526a33338e6fa3f38ce332380139a860, the App.java file contains the relevant changes. We had also used a similar technique for DNS which required a similar fix but is not likely used in your system.
Seems to be caused by these new restrictions in Android 11:
https://developer.android.com/training/articles/user-data-ids#mac-11-plus
There are still unaddressed review comments which might explain why the PR has stalled.
https://go-review.googlesource.com/c/go/+/507415/3/src/syscall/netlink_linux.go
https://go-review.googlesource.com/c/go/+/507415/3/src/net/interface_linux.go
@ehsan6sha , sure, below I describe what you need to do:
Download your prefered go version source code from: https://go.dev/dl/ (I downloaded go1.19.9 for instance) and unzip it into your

home/yourusernamedirectory, if you already have a “go” folder remove it and later on unzip.As per https://github.com/golang/go/issues/40569#issuecomment-1600898227 go to your recently unzipped go folder and find the
srcfolder and proceed to modify src/net/interface_linux.go & /src/syscall/netlink_linux.go files as described in the comment above.Open a terminal window and go to your /home/yourusername/go/src, then build your modified go source by using sending
$ ./all.bashthrough the command line as per go source install instructionsAfter successful build, go to your golfing project and build your binaries as usual, change your targetSdkVersion to 30 or above in your build.gradle file, compile your project in Android Studio et voila!
As @yan12125 this is a super clean patch in the go source code to work around the issue, looks effective until now 😃. Next step its to upload my app to google play as final test hehe
Here is how I fix this
x/mobile: Calling net.InterfaceAddrs() fails on Android SDK 30issue to resolve IPFS in mobile issue like route ip+net: netlinkrib: permission denied withx/mobile/cmd/gomobileas compile tools , ref to cmd/tailscale: implement getInterfaces + SDK 30 which usegioui.orgas compile tools.MainApplication.javaTake react native project for example, in
YOUR_PROJECT/android/app/src/main/AndroidManifest.xmlso be the
MainApplication.javahere.And also in
MainApplication.javago.modAdd
Above will download
git.wow.st/gmp/jniandinet.af/netaddrinto~/go/pkg/mod/, and modifygo.modautomatically.Above will remove
vendor/automatically then copy again from~/go/pkg/mod/.If
go mod downloadmodifiedgo.modintothen they will not be copied into
vendor/bygo mod vendor, so you can temporarily addinto one of your own
.gofile, rungo mod downloadagain, found// indirectdisappeared, reset your own.gofile, rungo mod vendoragain, found they were copied intovendor/.vendor/github.com/multiformats/go-multiaddr/net/net.goReplace
with
And also in
vendor/github.com/multiformats/go-multiaddr/net/net.go, replacewith
or
21.4.7075529 is default in RN 0.66 and gradle 6.7.1
If r22 or higher, will cause
just like x/mobile/cmd/gomobile: gomobile build on simple program returns “ld: error: duplicate symbol: x_cgo_inittls”
Finally, you can continue your
gomobile bindwork flow 😋PS: notice
java.util.Locale.ROOTas described below https://github.com/golang/go/issues/40569#issuecomment-1191823859Any more updates on this one? Android 11 was released today.
PS: I can confirm that I only see this problem on my Android 13 device. The older Android 8 device doesn’t have that issue.
Marked my comment as resolved because it was relevant to that import cycle !
Sorry, since this is my first PR project, I did not perform compilation testing, which resulted in such a problem. I re-investigated the problem and found that the compilation failed due to package references. I re-submitted the code.
My point is that it’s better to use the simple fix and restore basic functionality for 75% of the devices out there, and accept that information like the MAC will be dropped for the shrinking number of phones running Android 10 or older.
I am looking for a better way to obtain the version number of the Android system for better problem-solving
I am sorry for such a problem, I will re-do the PR later
Probably not. Apparently both functions need
RTM_GETLINK, which is forbidden on newer Android.@simbadMarino Can you please elaborate on the full steps you took? I mean which files you modified, etc ? Sorry for being a noob here
@simbadMarino could you try this patch? It works with Go 1.21 rc1 source.
any update?
Thanks! But I’m not sure how to integrate those changes into stdlib in an elegant way as I’ve mentioned above.
Any error meesages?
I believe so. My example is modified from existing functions in stdlib, so those modifications should be compatible with stdlib. I can give it a try and create a pull request after finding time on reading contribution guidelines.
We ended up developing an alternative using Android APIs, which we call instead of getInterfaceAddrs(). https://github.com/tailscale/tailscale-android/pull/21
Reproduced in Yggdrasil project for net.Interfaces(): https://github.com/yggdrasil-network/yggdrasil-go/blob/master/src/multicast/multicast.go#L192