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生成的类覆盖。

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

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐