简体   繁体   English

在iOS项目中使用Java库

[英]Using Java library in iOS project

I am developing a project that requires business logic calculations based on a relative complex rule set. 我正在开发一个项目,该项目需要基于相对复杂的规则集进行业务逻辑计算。 The calucations are to be executed on the following platforms: 计算将在以下平台上执行:

  • Android app Android应用
  • iOS app iOS应用
  • Java EE application container Java EE应用程序容器

The business logic does not depend on any platform specific details but is strictly about "number crunching". 业务逻辑不依赖于任何平台特定的细节,而是严格地与“数字处理”有关。

To avoid double implementation for Java and Objective C, which would require elaborate testing and so forth, I would like to create a Java library cotaining the implementation and use it within the iOS app. 为了避免Java和Objective C的双重实现(这需要进行详尽的测试等),我想创建一个包含实现的Java库,并在iOS应用中使用它。

What would be the best way to do this? 最好的方法是什么? Would it be better to do a native port for Objective C? 为Objective C做一个本地端口会更好吗? Would it be better to do it the other way around and write the library in Objective C and use it on Android/Java? 以另一种方式做起来并在Objective C中编写该库并在Android / Java上使用它会更好吗?

There is no way how you can use a Java library in an iOS application. 无法在iOS应用程序中使用Java库。 Java requires runtime to interpret (JRE) and you don't have that on iOS. Java需要运行时来解释(JRE),而iOS上没有。

Technically, you could compile a Java library into native code (there are tools for that) but I don't recommend it. 从技术上讲,您可以将Java库编译为本机代码(有用于该工具的工具),但我不建议这样做。

For sharing bussiness code between Android and iOS the best solution is to use pure C libraries. 为了在Android和iOS之间共享业务代码,最好的解决方案是使用纯C库。 You can call C libraries from Java using JNI (NDK for Android). 您可以使用JNI(适用于Android的NDK)从Java调用C库。

Since Obj-C is only a small object layer over pure C, you can use C libraries easily from Obj-C. 由于Obj-C只是纯C之上的一个很小的对象层,因此可以从Obj-C轻松使用C库。

You can compile Obj-C for NDK, too, but it is much more complicated than just using C. 您也可以为NDK编译Obj-C,但是它比仅使用C复杂得多。

You could try parsing the java library to Objective C using j2objc https://code.google.com/p/j2objc/ 您可以尝试使用j2objc https://code.google.com/p/j2objc/将Java库解析为Objective C

Update: This Swift Java bridge is in development https://github.com/SwiftJava/SwiftJava/blob/master/README.md 更新:此Swift Java桥正在开发中https://github.com/SwiftJava/SwiftJava/blob/master/README.md

Kotlin Native now has a Swift integration. Kotlin Native现在具有Swift集成。 So with some build process overhead you could share business logic written in Kotlin with iOS and Android. 因此,通过一些构建过程开销,您可以与iOS和Android共享用Kotlin编写的业务逻辑。

https://blog.jetbrains.com/kotlin/2017/12/kotlinnative-v0-5-released-calling-kotlin-from-swift-and-c-llvm-5-and-more/ https://blog.jetbrains.com/kotlin/2017/12/kotlinnative-v0-5-released-calling-kotlin-from-swift-and-c-llvm-5-and-more/

In this case you should write C implementation of your logic and use JNI for android application. 在这种情况下,您应该编写逻辑的C实现,并将JNI用于android应用程序。

So you wouldn't repeat yourself 所以你不会重复自己

http://en.wikipedia.org/wiki/Java_Native_Interface http://en.wikipedia.org/wiki/Java_Native_Interface

There is no way you can use any java-related code or application in iOS. 您无法在iOS中使用任何与Java相关的代码或应用程序。 You have to work on a native port. 您必须在本机端口上工作。 iOS doesn't have a jvm. iOS没有jvm。

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

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