RoboVM是一个了不起的机器人,可以将Java代码输出到ARM和X86二进制文件。
我们正在计划通过将使用Java libGDX创建的Android游戏输出为RoboVM的iOS二进制文件来计划Android / iOS的多兼容策略。
但是,当我将其应用于RoboVM时,如果被告知"我不支持反射,JK"将是一个问题,因此我尝试对其进行验证。
验证是否可以暂时生成iOS应用
参考以下站点,我用Java编写了iOS应用程序的源代码,使用RoboVM生成了iOS的二进制文件,并使用模拟器执行了它。
Java RoboVM Eclipse中的iOS应用开发
奏效了!

当您单击"单击"按钮时,数字将被累加。
XCode版本为5.1.1版本
iOS模拟器的版本为7.1(463.9.41)
OS X版本10.9.2
JDK版本1.7.0_40
RoboVM版本为0.0.11
请注意,当前文件名已更改为最新版本(版本0.0.11),并且下载和安装RoboVM的步骤如下。
1 2 3 | curl http://download.robovm.org/robovm-0.0.11.tar.gz > robovm.tar.gz sudo tar xvfz robovm.tar.gz -C /opt sudo ln -s /opt/robovm-0.0.11 /opt/robovm |
主要主题:测试反射等是否起作用
验证来源
HelloWorld.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import java.io.Serializable; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class HelloWorld implements Serializable { private static final long serialVersionUID = 1L; String str = "HELLO!"; int num = 123456; int num2 = 65; float pi = 3.14159265f; public enum Sound { ARRIVAL1("se_puu10.ogg"), ARRIVAL2("se_puu37.ogg"), ARRIVAL3("se_puu26.ogg"); private String filename_; private Sound(String filename) { filename_ = filename; } public String getFilename() { return filename_; } }; public static void main(String[] args) { System.out.println("Hello world!"); HelloWorld hello = new HelloWorld(); // serialize test System.out.println("serialize and sate serialize.dat"); try { FileOutputStream fos = new FileOutputStream("serialize.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(hello); oos.close(); } catch (Exception ex) {} // reflection test for (Field field: hello.getClass().getDeclaredFields()) { if (!Modifier.isStatic(field.getModifiers())) { System.out.println("name:" + field.getName() + " type:" + field.getType()); } } // enum test for (Sound sound: Sound.values()) { System.out.println("sound filename:" + sound.getFilename()); } } } |
执行结果
有效。
评论
序列化
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class HelloWorld implements Serializable { private static final long serialVersionUID = 1L; String str = "HELLO!"; int num = 123456; int num2 = 65; float pi = 3.14159265f; (中略) // serialize test System.out.println("serialize and sate serialize.dat"); try { FileOutputStream fos = new FileOutputStream("serialize.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(hello); oos.close(); } catch (Exception ex) {} |
HelloWorld类被序列化并输出到文件" serialize.dat"。
输出结果如下。
1 2 3 4 5 6 7 | 0000000 ac ed 00 05 73 72 00 0a 48 65 6c 6c 6f 57 6f 72 0000020 6c 64 00 00 00 00 00 00 00 01 02 00 04 49 00 03 0000040 6e 75 6d 49 00 04 6e 75 6d 32 46 00 02 70 69 4c 0000060 00 03 73 74 72 74 00 12 4c 6a 61 76 61 2f 6c 61 0000100 6e 67 2f 53 74 72 69 6e 67 3b 78 70 00 01 e2 40 0000120 00 00 00 41 40 49 0f db 74 00 06 48 45 4c 4c 4f 0000140 21 |
显然,结果与以Java运行时相同。
倒影
1 2 3 4 5 6 | // reflection test for (Field field: hello.getClass().getDeclaredFields()) { if (!Modifier.isStatic(field.getModifiers())) { System.out.println("name:" + field.getName() + " type:" + field.getType()); } } |
反射列出并显示HelloWorld类的实例" hello"的字段。 (字段名称和类型)
1 2 3 4 | name:pi type:float name:num2 type:int name:num type:int name:str type:class java.lang.String |
与在Java中运行时得到的结果相同。
枚举
中原始方法的实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public enum Sound { ARRIVAL1("se_puu10.ogg"), ARRIVAL2("se_puu37.ogg"), ARRIVAL3("se_puu26.ogg"); private String filename_; private Sound(String filename) { filename_ = filename; } public String getFilename() { return filename_; } }; (略) // enum test for (Sound sound: Sound.values()) { System.out.println("sound filename:" + sound.getFilename()); } |
向枚举Sound添加了构造函数和成员变量,并以字符串形式添加了filename_成员变量。
添加的getFilename()方法获取文件名成员的值并显示它。
1 2 3 | sound filename:se_puu10.ogg sound filename:se_puu37.ogg sound filename:se_puu26.ogg |
与在Java中运行时得到的结果相同。
后记:弱引用
1 2 3 4 5 6 7 8 | import java.lang.ref.WeakReference; (略) // WeakReference WeakReference<HelloWorld> helloRef = new WeakReference<HelloWorld>(hello); HelloWorld hello2 = helloRef.get(); System.out.println("hello2.str:" + hello2.str); |
弱引用也可以!
1 | hello2.str:HELLO! |
目前看来,您可以放心使用Android版本。