我想在libgdx的ios项目中集成友盟统计分析。
友盟目前没有libgdx的插件,so,自己把它搞进来吧。
我们通过robovm binding 在java代码和友盟ios SDK之间搭个桥就可以了。
请看这里:https://github.com/BlueRiverInteractive/robovm-ios-bindings
==========================
1. 首先我们给eclipse(ADT)安装个robovm插件,插件地址:http://download.robovm.org/eclipse/
2. 下载umeng的game分析SDK解压备用
3. 创建RoboVM iOS project:New=》Other=》RoboVM=>RoboVM iOS Project
Next=>
Project name: umeng-binding
Main class: com.tide.binding.umeng.Sample
App name: umeng binding
App id: umengBinding
(以上三信息可在创建后的robovm.properties中修改)
Finish!
4. 创建后的项目中默认只有一个src源文件夹,补上包:com.tide.binding.umeng
5. 创建代码,也就是上面提到的Main class: com.tide.binding.umeng.Sample
代码如下:
1 package com.tide.binding.umeng; 2 3 import org.robovm.apple.foundation.NSAutoreleasePool; 4 import org.robovm.apple.foundation.NSString; 5 import org.robovm.apple.uikit.UIApplication; 6 import org.robovm.apple.uikit.UIApplicationDelegateAdapter; 7 8 public class Sample extends UIApplicationDelegateAdapter { 9 @Override 10 public void didFinishLaunching (UIApplication application) { 11 System.out.println("didFinishLaunching"); 12 } 13 14 public static void main (String[] argv) { 15 try (NSAutoreleasePool pool = new NSAutoreleasePool()) { 16 UIApplication.main(argv, null, Sample.class); 17 } 18 } 19 }
6. umeng game SDK 压缩包解压后共有三个文件
libMobClickGameLibrary.a =>拷贝到项目的libs目录下,没有这个目录新建即可。
robovm.xml 中添加这个lib,不然运行时报找不到类
代码如下:
1 <config> 2 <executableName>${app.executable}</executableName> 3 <mainClass>${app.mainclass}</mainClass> 4 <os>ios</os> 5 <arch>thumbv7</arch> 6 <resources> 7 <resource> 8 <directory>resources</directory> 9 </resource> 10 </resources> 11 <target>ios</target> 12 <iosInfoPList>Info.plist.xml</iosInfoPList> 13 <libs> 14 <lib>libs/libMobClickGameLibrary.a</lib> 15 </libs> 16 </config>
MobClick.h 稍后我们编写与之对应的binding代码
MobClickGameAnalytics.h 稍后我们编写与之对应的binding代码
7. binding MobClick
创建java class MobClick
代码如下:
1 package com.tide.binding.umeng; 2 3 import org.robovm.apple.foundation.NSObject; 4 import org.robovm.apple.foundation.NSString; 5 import org.robovm.objc.annotation.Method; 6 import org.robovm.objc.annotation.NativeClass; 7 import org.robovm.rt.bro.ValuedEnum; 8 9 @NativeClass 10 public class MobClick extends NSObject { 11 12 @Method(selector = "setLogEnabled:") 13 public static native void setLogEnabled(boolean value); 14 15 @Method(selector = "startWithAppkey:") 16 public static native void startWithAppkey(NSString appKey); 17 }
注意:我只写了两个方法,试了下不报错,所以应该不用对所有方法都绑定(会蛋疼而亡的)。而<UIAlertViewDelegate>的对应实现我也没有写,我不明白为什么友盟的.h文件中写了个这个,不知干嘛用...。
8. 测试一下。
public class Sample extends UIApplicationDelegateAdapter { private static final String AppKey = "************";//你的友盟appID @Override public void didFinishLaunching (UIApplication application) { System.out.println("didFinishLaunching"); NSString a = new NSString(AppKey); MobClick.startWithAppkey(a); MobClick.setLogEnabled(true); } public static void main (String[] argv) { try (NSAutoreleasePool pool = new NSAutoreleasePool()) { UIApplication.main(argv, null, Sample.class); } } }
项目右键=》Run as =》Ios 模拟器 iphone 第一次编译时间有点长。
稍后可以看到控制台打印:
didFinishLaunching 2014-08-29 17:50:02.767 Sample[1387:4103] Umeng: (Request Applog) content={"header":{"timezone":"8","sdk_type":"iOS","resolution":"960 x 640","package_name":"umengBinding","sv":"1.0","is_jailbroken":"NO","carrier":"","access":"WiFi","channel":"App Store","os":"iOS","idfv":"565E734A-5170-412B-AF14-786FB272449D","vertical_type":1,"display_name":"umeng binding","os_version":"7.1","device_model":"x86_64","app_version":"1","country":"US","language":"en","sdk_version":"2.3.2.0","appkey":"54003b20fd98c50a1c006ee0","mc":"02:00:00:00:00:00","device_id":"3D99BEEE-7704-4697-AF5B-EEE13887F9E0","is_pirated":"NO"},"body":{"launch":[{"time":"17:50:02","session_id":"9B51DBAB0350C4FD569CE3E7AFFD97A8","date":"2014-08-29"}]}} 2014-08-29 17:50:04.159 Sample[1387:1803] Umeng: (Response Applog) {"success": "ok"}
到umeng的游戏分析中去看,是有数据的。
9. 其他方法绑定及MobClickGameAnalytics.... 略
用到哪些再弄吧。
要在libgdx的项目中使用这个binding应该有两种方法,
一是直接搬过去,
二是打包jar使用,类似于robovm-binding项目中的内购那些。
再次附:https://github.com/BlueRiverInteractive/robovm-ios-bindings
=============================================
补充:2014-09-03
如果是在IDEA+gradle环境下,在执行gradle脚本的时候注意,有一个updateRoboVMXML的命令会被执行到,
可查看ios项目下的build.gradle文件,他会把robovm.xml覆盖掉,所以在这个文件中我们加的libs就没了,运行时就找不到类。
解决办法就是参照他生成的代码,把.a文件扔到他那个build/libs/ios/路径下。