JRE Framework inclusion issue

134 views
Skip to first unread message

Louis G

unread,
Jan 20, 2022, 7:02:47 PM1/20/22
to j2objc-discuss
Hello, I'm recently trying to translate a java project using J2OBJC. The goal is to translate a library from java to objc to include in a swift project.
When I translate my java code, I need to include JRE for some functions but I'm having issues making it impossible to correctly build the project on a Xcode simulator.

Here is how I did my setup.
First of all, I built J2OBJC with "make all_dist"
My project should be able to use a library made from my java code (translated code which needs JRE to work).
For this I decided to use XCFrameworks. The J2OBJC framework build creates automatically the XCFramework for JRE_emul. I'm using this one.
To create the XCFramework for my translated java code, here are the steps I used (it is all in a script) :
  1. j2objc -sourcepath ${SRCDIR}:${SRCDIR2} -classpath ${SRCJAVA}:${SRCJAVA2} -d ${STGDIR}/src --no-package-directories --swift-friendly `find ${SRCDIR} ${SRCDIR2} -name "*.java"`. My code needs another .jar to work, so I have 2 source directories
  2. j2objcc -arch arm64 -pipe -no-cpp-precomp -isysroot $SDKROOT -fembed-bitcode-marker -ObjC -c -I. `find . -name "*.m"`. To be honest, I'm not sure what all those flags do. I just know the -arch and -isysroot ones. Here I'm building for arm64 but I'm doing this part multiple times for my XCFramework.
  3. libtool -static -o MQ2Library.a $ofiles \ ranlib MQ2Library.a. To build a static library for an architecture
  4. xcodebuild -create-xcframework -library <...>   -library <...> -library <...>

So now I have both XCFrameworks

Now here are the steps I used to put them in a new Swift Project :

  1. Create folder «Frameworks » outside Swift folder in the project
  2. Drag and drop your XCFramework in the folder (select copy and create group)
  3. Open options
  4. In general -> Frameworks, Libraries and Embedded Content : do not embed
  5. In Build Settings -> header search path : drag framework and select recursive
  6. Create a .h objc bridge file
  7. In Build Settings -> Objective-C bridging header : drag and drop the .h file
  8. In Build Settings -> other linker flags : -l iconv
Once all this is done, I can build without issues my new swift project. I get no error. But then when I try some functions on a simulator I get errors I don't know how to fix.
I did some tries, for example I can call JRE functions with no crash. I can also call simple functions from my created framework. Finally I can also use some simple cross framework functions (functions from my framework using functions from JRE framework)
But for some of my framework's functions using JRE functions from the "java" folder, I get errors I don't understand.
Here are some examples :
-"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString byteAtIndex:]: unrecognized selector sent to instance 0x8d56a40800f7f289'"
-"Terminating app due to uncaught exception 'JavaLangArrayIndexOutOfBoundsException', reason: 'java.lang.ArrayIndexOutOfBoundsException: index out of range: 3 for array containing 3 elements'"
I can assure that the source java project works perfectly.
Those errors aren't showed when I build the project, they appear only when I try running it on simulators.

Those errors may come from the fact I'm not using the exact same SDKs when I build my libraries as the one used for JRE. I can't find the SDKs used by j2objc build.

Here are some things I tried to fix the issue :
  1. I tried taking the .o from JRE builds to use with my library's .o to build everything as one framework. I got the same issues.
  2. I tried including JRE not as a framework but by following the instructions from https://developers.google.com/j2objc/guides/the-jreemulation-project . Unfortunately when I include the JRE project, I get missing files (ex: TextWatcher.m) And I can't find those files.
  3. I also tried changing some build options, but never got it to work.
I don't know if those errors come from a bad translation of my java project, or if it is from using cross-framework (I read that it is not advised but I don't know how to do it otherwise) or if it comes from something else.
I hope some of you guys might have some ideas.
Sorry for the long post, I just tried to make it the clearer possible.
Reply all
Reply to author
Forward
0 new messages