简体   繁体   English

如何在Java中创建面向移动的多平台共享库?

[英]How to create a mobile oriented, multiplatform, shared library in Java?

I have a Java application that runs on BlackBerry (JDE 4.5). 我有一个在BlackBerry上运行的Java应用程序(JDE 4.5)。 I want to port this application to Android, and be able to maintain the 2 applications simultaneously. 我想将此应用程序移植到Android,并能够同时维护这两个应用程序。 I may also want to port this application to other Java platforms (J2ME ?). 我可能还想将此应用程序移植到其他Java平台(J2ME?)。

I understand that a good part of the code will have to be specific to each platform (UI and other stuff). 我知道代码的很大一部分必须特定于每个平台(UI和其他东西)。 But I also feel that a lot of the code could (should) be shared (domain related classes). 但我也觉得很多代码可以(应该)共享(域相关类)。

What is the best way to achieve this, and what are the pitfalls to avoid? 实现这一目标的最佳方法是什么?要避免哪些陷阱?

I have been able so far to create a JAR with all my shared classes, that I have been able to integrate into my BlackBerry application (using preverify and rapc ). 到目前为止,我已经能够创建一个包含所有共享类的JAR,我已经能够集成到我的BlackBerry应用程序中(使用preverifyrapc )。 But: 但:

This is likely to be difficult: 这可能很困难:

  • As you have already identified, the UI code will have to be different for each platform. 正如您已经确定的那样,每个平台的UI代码必须不同。
  • There are major differences between Java SE / Android and Java ME-based platforms. Java SE / Android和基于Java ME的平台之间存在重大差异。 For example, ME doesn't have the Collections framework, or the java.io or java.nio stacks. 例如,ME没有Collections框架,也没有java.iojava.nio堆栈。

It is hard to predict from the information you've provided, but there's a fair chance that you'll spend more time fighting the platform dependencies than you are saving by sharing the code-base. 很难从你提供的信息中预测出来,但是你很可能花费更多的时间来对抗平台依赖,而不是通过共享代码库来节省。

Rather than prepackage your shared library, I would consider sharing the library project and having it as a dependency in your mobile applications' build process. 我不会预先打包您的共享库,而是考虑共享库项目并将其作为移动应用程序构建过程中的依赖项。 That would allow you to share the code base, but have it built by the appropriate builders for your target devices. 这将允许您共享代码库,但由目标设备的相应构建器构建。 With a bit of IDE magic and some attention to detail, you should be able to pick up errors before anything is shipped out. 通过一些IDE魔术和对细节的一些关注,您应该能够在发货之前发现错误。

Alternatively, set up your library project to use two separate builders to pick up errors. 或者,设置库项目以使用两个单独的构建器来获取错误。 That would allow cleaner distribution, but you may run into problems trying to convince your IDE to treat the project as being device specific in order to identify problem areas. 这将允许更清晰的分发,但是您可能会遇到问题,试图说服您的IDE将项目视为特定于设备以识别问题区域。

It would be likely that you would end up supporting the lowest common denominator device ( cough Blackberry), and forgoing the additional facilities of the more extensive Java implementation on Android. 你可能会最终支持最低的公分母设备( 咳嗽黑莓),并放弃Android上更广泛的Java实现的附加功能。

Unfortunately the answer will be one of experimentation. 不幸的是,答案将是实验之一。 Try it and see what happens. 尝试一下,看看会发生什么。

These days, the biggest stumbling block to sharing code this way is that the BlackBerry VM and Android VM both support different versions of the Java language. 如今,以这种方式共享代码的最大障碍是BlackBerry VM和Android VM都支持不同版本的Java语言。 BlackBerry uses a subset of Java 1.3, Android uses a subset of Java 1.5. BlackBerry使用Java 1.3的子集,Android使用Java 1.5的子集。 (As an aside, neither platform implements a Java VM, both use their own VMs. Java is used as the programming language. Java bytecodes must be transformed to the appropriate native VM format before they can run on the platform.) (另外,两个平台都没有实现Java VM,都使用自己的VM.Java用作编程语言.Java字节码必须转换为适当的本机VM格式才能在平台上运行。)

The biggest difference you will find as a library implementor is that the BlackBerry lacks the things that were introduced in 1.5, very important things like generics and enums. 作为图书馆实施者,您将发现最大的不同之处在于BlackBerry缺少1.5中引入的东西,非常重要的东西,如泛型和枚举。 Even worse, the Collections classes are missing from the BlackBerry. 更糟糕的是,BlackBerry缺少收藏类。 It is unfortunate, but that is the way it has been for a long time now. 这是不幸的,但现在已经很久了。

This means that to be truly portable you have to write to the lowest-common denominator, which means using (very) old-style classes like Hashtable and Vector, not having generics, rolling your own enums (as in the 1st edition of Effective Java) and so on. 这意味着要真正可移植,你必须写入最低公分母,这意味着使用(非常)旧式的类,如Hashtable和Vector,没有泛型,滚动你自己的枚举(如第1版的Effective Java) ) 等等。

Or you build two libraries, a modern version for Android and a stripped-down version (with just the bare stuff you need) for the BlackBerry. 或者你构建了两个库,一个适用于Android的现代版本和一个适用于BlackBerry的精简版本(只需要你需要的东西)。

Hard to say what`s right for you. 很难说什么适合你。

文章将Android代码移植到BlackBerry有一些关于如何使用两个平台之间共享代码的详细信息。

it will be very difficult to create shared library for blackberry and android. 为blackberry和android创建共享库将非常困难。 if you want simple method, create your application as web app. 如果您想要简单的方法,请将您的应用程序创建为Web应用程 using 运用

phonegap with jQtouch PhoneGap的jQtouch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM