英文:
Golang: Android apps with gomobile crash when connect in UDP
问题
我用go语言的mobile包编写了Android应用程序,在达到以下代码后应用程序崩溃:
ServerAddr,_ := net.ResolveUDPAddr("udp",SERVER_IP_AND_PORT)
LocalAddr, _ := net.ResolveUDPAddr("udp", ":0")
Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
buf := []byte("lalala")
_,err := Conn.Write(buf) //应用程序在这一行崩溃
其中(实际IP由"x"表示):
const SERVER_IP_AND_PORT string = "xxx.xx.xx.xxx:10001"
我在我的笔记本电脑上测试了这段代码,它们完全正常工作。我只使用mobile包创建apk文件进行UDP测试。
我尝试使用"log"包来跟踪错误,但它不起作用。我下载了很多logcat应用程序,但没有一个能捕获错误日志(甚至是log.Print("AAA"))。我还尝试将日志写入文件,但也不起作用,请查看这个问题。
无论如何,有人对问题有什么想法,或者有什么方法可以定位错误吗?(请避免使用我安装正确的Android Studio时遇到的一千个问题)
非常感谢!
英文:
I wrote Android apps in go with mobile package, the apps crash after reaching the following code:
ServerAddr,_ := net.ResolveUDPAddr("udp",SERVER_IP_AND_PORT)
LocalAddr, _ := net.ResolveUDPAddr("udp", ":0")
Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
buf := []byte("lalala")
_,err := Conn.Write(buf) //apps crash on this line
Where (actual ip is represented by "x"):
const SERVER_IP_AND_PORT string = "xxx.xx.xx.xxx:10001"
I tested the code on my laptop and they work perfectly fine. I use mobile package only for creating apk files to do the UDP tests.
I tried to use "log" package to trace errors but it doesn't work. I downloaded many logcat apps but none can catch the error logs (or even log.Print("AAA")). I also tried to write logs in files but don't work either, please check out this question.
Anyway, does anyone have any idea about what the problem is, or any ways to locate the bug? (And please avoid using Android Studio which I have a thousand more problems to even install it correctly)
Thank you very much!
答案1
得分: 1
到目前为止,我们必须手动向AndroidManifest.xml文件中添加权限,因为gomobile包没有自动完成这个步骤(已经提出了功能请求)。在他们实现这个功能之前,我们必须手动添加权限。
有关更多信息,请参考以下问题:
英文:
By now we have to manually add permissions to AndroidManifest.xml, gomobile package is not doing that (feature request is filed). Before they implement this feature we have to do it manually.
For more information please refer to this question:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论