GOMOBILE写Android和iOS移动端SDK
来源:互联网 发布:mac打字左下角 编辑:程序博客网 时间:2024/06/07 18:42
GOMOBILE
设置GO环境以及GOPATH,参考GO环境设置。
GOMOBILE资料参考官方WIKI,这个WIKI貌似对SDK和NDK没有说清楚。
Android SDK+NDK
下载和安装AndroidSDK和AndroidNDK,MacOS安装到:
# AndroidSDK/Users/winlin/Library/Android/sdk# AndroidNDK/Users/winlin/Library/Android/sdk/ndk-bundle
也就是看到下面的文件就算成功:
Mac:sdk winlin$ ls /Users/winlin/Library/Android/sdkbuild-tools extras ndk-bundle platform-tools skins system-images cmake licenses patcher platforms sources toolsMac:sdk winlin$ ls /Users/winlin/Library/Android/sdk/ndk-bundleCHANGELOG.md ndk-gdb prebuilt sources android-ndk-r13b ndk-stack python-packages toolchains build ndk-which shader-tools ndk-build package.xml simpleperf ndk-depends platforms source.properties
一般AndroidStudio的local.properties
文件,这个是自动生成的,会默认这指定SDK和NDK的地址:
Mac:hello-jni winlin$ cat android-ndk/hello-jni/local.properties ## This file is automatically generated by Android Studio.# Do not modify this file -- YOUR CHANGES WILL BE ERASED!ndk.dir=/Users/winlin/Library/Android/sdk/ndk-bundlesdk.dir=/Users/winlin/Library/Android/sdk
参考项目android-ndk/hello-jni。
GOMOBILE TOOLS
直接下载和安装gomobile:
go get golang.org/x/mobile/cmd/gomobile
备注:如果被墙了,可以自己把gomobile项目clone到
$GOPATH/src/golang.org/x
下面,GITHUB地址。
先设置下SDK的环境变量给gomobile用,当然如果NDK在其他地方需要指定NDK路径。然后初始化gomobile:
export ANDROID_HOME=/Users/winlin/Library/Android/sdk$GOPATH/bin/gomobile init
注意:如果NDK没有安装对,gomobile init不会出错,但是后面gomobile bind会出错。
GO Code
GOLANG的CODE非常简单,参考gomobile/examples/bind/hello:
// Copyright 2015 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// Package hello is a trivial package for gomobile bind example.package helloimport "fmt"func Greetings(name string) string { return fmt.Sprintf("Hello, %s!", name)}
备注:需要按照GOLANG的package的规则,放在
$GOPATH/src
下面,否则编译会报错。
Build Mobile SDK
然后编译AndroidNDK用的库:
$GOPATH/bin/gomobile bind -target=android
生成了Android可以引用的.aar
,直接搞定:
Mac:temp winlin$ ls -lhtotal 5712-rw-r--r-- 1 winlin staff 2.8M Mar 9 10:31 hello.aar-rw-r--r-- 1 winlin staff 333B Mar 9 10:28 hello.go
还可以生成iOS用的库:
$GOPATH/bin/gomobile bind -target=ios
可以看到iOS的库:
Mac:temp winlin$ ls -lh Hello.framework/total 32lrwxr-xr-x 1 winlin staff 24B Mar 9 10:32 Headers -> Versions/Current/Headerslrwxr-xr-x 1 winlin staff 22B Mar 9 10:32 Hello -> Versions/Current/Hellolrwxr-xr-x 1 winlin staff 24B Mar 9 10:32 Modules -> Versions/Current/Moduleslrwxr-xr-x 1 winlin staff 26B Mar 9 10:32 Resources -> Versions/Current/Resourcesdrwxr-xr-x 4 winlin staff 136B Mar 9 10:32 Versions
库的大小:
Mac:temp winlin$ du -sh *5.1M Hello.framework2.8M hello.aar4.0K hello.go
Android APP
新建一个Android APP,目录在/Users/winlin/git/MyApplication
。
将hello.aar
链接或拷贝到app/libs
下面:
cd ~/git/MyApplication/app/libsln -sf $GOPATH/src/temp/hello.aar
然后修改app/build.gradle
(在Android Studio中也可以改,打开Gradle Scripts => build.gradle(Module: app)
这个文件就是),添加下面内容:
repositories { flatDir { dirs 'libs' }}dependencies { compile(name: 'hello', ext: 'aar')}
注意:
dependencies
一般都有这个了,只需要把compile
加进去就可以。注意:
compile
的name
不能瞎写,和那个hello.aar
文件名一致。
然后在项目中就可以看到内容了:
在MainActivity中可以直接引用:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView txt = new TextView(this); txt.setText(Hello.greetings("SRS")); setContentView(txt); }}
运行结果:
iOS APP
新建一个iOS的Application,选Swift语言:
在Main.storyboard
中,添加一个UILabel控件:
在ViewController.swift
中引用UILabel控件为成员。在Main.storyboard
中选中label后,右键,拖New Referencing Outlet
到ViewController.swift
中:
输入这个引用,也就是成员变量的名字:
可以看到创建了一个引用:
在viewDidLoad
中修改Label的值:
class ViewController: UIViewController { @IBOutlet weak var txtMain: UILabel! override func viewDidLoad() { super.viewDidLoad() txtMain.text = "Hello, iOS" }
运行就可以看到Label的值改变了。下面引入Hello.framework,先拷贝到项目下面:
cd ~/git/HelloWorldln -sf $GOPATH/src/temp/Hello.framework
文件如下:
Mac:HelloWorld winlin$ ls -lhtotal 0drwxr-xr-x 7 winlin staff 238B Mar 9 13:56 Hello.frameworkdrwxr-xr-x 7 winlin staff 238B Mar 9 13:48 HelloWorlddrwxr-xr-x 5 winlin staff 170B Mar 9 13:56 HelloWorld.xcodeprojdrwxr-xr-x 4 winlin staff 136B Mar 9 13:21 HelloWorldTestsdrwxr-xr-x 4 winlin staff 136B Mar 9 13:21 HelloWorldUITestsMac:HelloWorld winlin$ ls -lh Hello.framework/total 32lrwxr-xr-x 1 winlin staff 24B Mar 9 13:56 Headers -> Versions/Current/Headerslrwxr-xr-x 1 winlin staff 22B Mar 9 13:56 Hello -> Versions/Current/Hellolrwxr-xr-x 1 winlin staff 24B Mar 9 13:56 Modules -> Versions/Current/Moduleslrwxr-xr-x 1 winlin staff 26B Mar 9 13:56 Resources -> Versions/Current/Resourcesdrwxr-xr-x 4 winlin staff 136B Mar 9 13:56 Versions
在xcode中,添加framework:
在代码中引入framework:
代码改成这样:
import UIKitimport Helloclass ViewController: UIViewController { @IBOutlet weak var txtMain: UILabel! override func viewDidLoad() { super.viewDidLoad() txtMain.text = HelloGreetings("SRS") }
调用的是SDK中的函数。运行结果:
Conclusion
总结下:
- 直接用AndroidNDK编写SDK,需要自己写JNI。而gomobile一个命令,把脏活累活都给弄好了。便利性gomobile占优。
- 可以一份代码支持Android和iOS,维护上比较方便。
- 体积上,gomobile的so最起码有2.8MB,比C要大很多。
- 成熟度上,gomobile目前还没release,虽然2015就已经在GO大会上出现了。
- GOMOBILE写Android和iOS移动端SDK
- golang go语言 gomobile 移动端开发笔记
- Android、ios移动端身份证识别SDK
- iOS/Android开发移动短信SDK列表
- 用Chrome和android SDK远程调试移动端页面
- 【用objective-c写android和ios游戏】初次使用Stella SDK
- 写iOS SDK注意事项
- 写iOS SDK注意事项
- 写iOS SDK注意事项
- iOS 写SDK注意事项
- 写iOS SDK注意事项
- 写iOS SDK注意事项
- 写iOS SDK注意事项
- Chrome和android SDK远程调试移动端页面,报device not found 错误解决方法
- 移动端(iOS)SDK开发注意点
- 移动端ios车牌识别sdk开发包分享
- android 接入移动基地SDK
- Android和iOS的名片拍照扫描识别功能sdk
- Android中如何给layout资源文件分类 在layout下创建子文件夹
- 人工智能自动代码生成3---数据库获取代码实现1
- L1-030. 一帮一
- 深度学习 LSTM
- mysql常用操作
- GOMOBILE写Android和iOS移动端SDK
- 基于DragonBoard 410c的遥控炮台五之远程交互(下)
- 360笔试题
- Day_two--->java数据类型的转换
- Phpcms 详情页显示所属栏目的同级栏目
- 【通俗向】假设检验(三):卡方检验和t检验
- 关于python 的django-forbidden-csrf-cookie-not-set问题
- SpringMVC国际化
- 时间服务器同步设置ntp