golang开发android应用(二) - go语言生成android动态库

dearcode · · 932 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

gomobile的两个有用参数,bind 生成动态库,build 生成apk

生成android动态库

gomobile bind -target=android github.com/dearcode/candy/server/android
生成文件candy.aar,先查看下大小:
du -sh candy.aar
13M     candy.aar
巨大,13MB,本来想压缩下,发现这aar本身就是压缩过的,生成后的文件大小本身与代码有关,我用的是项目中的代码,如果按官方的测试代码生成的文件只有2MB
file candy.aar
candy.aar: Zip archive data, at least v2.0 to extract

指定运行平台

gomobile默认会生成四个平台arm, arm64, 386, amd64,你也可以指定平台,例:

gomobile bind -target=android/arm github.com/dearcode/candy/server/android
这样生成的文件就相当小了,,,
du -sh candy.aar
3.0M    candy.aar

package与struct同名问题

目前gomobile版本,包名不能与类名相同,如相同,则先最生根据包名生成的类文件的会被覆盖:
gomobile bind -v -target=android github.com/dearcode/candy/server/android
github.com/dearcode/candy/server/android
write /tmp/gomobile-work-063666765/fakegopath/pkg/android_arm64/github.com/dearcode/candy/server/android.a
write /tmp/gomobile-work-063666765/gomobile_bind/go_candymain.go
write /tmp/gomobile-work-063666765/gomobile_bind/go_main.go
write /tmp/gomobile-work-063666765/androidlib/main.go
write /tmp/gomobile-work-063666765/android/src/main/java/go/candy/Candy.java
write /tmp/gomobile-work-063666765/android/src/main/java/go/candy/Candy.java
write /tmp/gomobile-work-063666765/gomobile_bind/java_candy.c
write /tmp/gomobile-work-063666765/gomobile_bind/candy.h
write /tmp/gomobile-work-063666765/android/src/main/java/go/Universe.java
write /tmp/gomobile-work-063666765/android/src/main/java/go/error.java
write /tmp/gomobile-work-063666765/gomobile_bind/java_universe.c
write /tmp/gomobile-work-063666765/gomobile_bind/universe.h
write /tmp/gomobile-work-063666765/gomobile_bind/seq_android.go
write /tmp/gomobile-work-063666765/gomobile_bind/seq_android.c
write /tmp/gomobile-work-063666765/gomobile_bind/seq.h
write /tmp/gomobile-work-063666765/gomobile_bind/seq.go
write /tmp/gomobile-work-981501834/gomobile_bind/seq.go

具体源码在这里:

https://github.com/golang/mobile/blob/master/cmd/gomobile/bind.go#L255

GenJava时会生成包名对应的类,但在GenClass的时候,分根据golang里的struct名生成新的类,此时如果有相同,则会被struct生成的类覆盖。



有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:dearcode

查看原文:golang开发android应用(二) - go语言生成android动态库

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

932 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传