libgdx robovm binding umeng

我想在libgdx的ios項目中集成友盟統計分析。java

友盟目前沒有libgdx的插件,so,本身把它搞進來吧。ios

咱們經過robovm binding 在java代碼和友盟ios SDK之間搭個橋就能夠了。git

請看這裏:https://github.com/BlueRiverInteractive/robovm-ios-bindingsgithub

 

==========================session

1. 首先咱們給eclipse(ADT)安裝個robovm插件,插件地址:http://download.robovm.org/eclipse/app

2. 下載umeng的game分析SDK解壓備用eclipse

3. 建立RoboVM iOS project:New=》Other=》RoboVM=>RoboVM iOS Projectiphone

Next=>ide

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/路徑下。

相關文章
相關標籤/搜索