2

I am using j2objc to compile and use a java library in iOS. I followed the processes:

  1. http://j2objc.org/docs/Xcode-Build-Rules.html
  2. http://j2objc.org/docs/Required-Link-Flags.html

I do not get any build errors until I start importing the header files to use them:

#import "org/library/Class.h"

The files are not found. What I am missing?

On the other hand, I tried to use the manually translated library files (using the terminal j2objc commands). If I pùt the .h and .m files into the j2objc-dist/include folder they are recognized by the editor and I can use the classes without compile errors. But, when I try to build the project it finds errors of the type _OBJ_CLASS_$_Java. I tried to include the files to the compile list in Xcode and I verified the path of libjre_emul.a but I still get the error.

My library contains packages so it has multiple folders in a tree. My preference will be to use the first method (original Java sources)

DATA FOR JAVA SOURCES CASE:

  • Build rules:

Java source files Custom script:

/Users/xgibert/Desktop/Orekit_iOS/j2objc-dist/j2objc -d ${DERIVED_FILES_DIR} -sourcepath ${PROJECT_DIR}/src/ ** \ --no-package-directories ${INPUT_FILE_PATH};

Output files:

${DERIVED_FILES_DIR}/${INPUT_FILE_BASE}.h
${DERIVED_FILES_DIR}/${INPUT_FILE_BASE}.m
  • Build phases

Link Binary with libraries:

libicucore.dylib
Security.framework
libz.dylib
  • Build Settings

Linking - Other Linker Flags:

-ljre_emul -L /Users/xgibert/Desktop/Orekit_iOS/j2objc-dist/lib -force_load /Users/xgibert/Desktop/Orekit_iOS/j2objc-dist/lib/libjre_emul.a -l jre_emul -ObjC

Search Paths - Library Seargh Paths:

/Users/xgibert/Desktop/Orekit_iOS/j2objc-dist/lib

Search Paths - User Header Search Paths:

/Users/xgibert/Desktop/Orekit_iOS/j2objc-dist/include

My java library files are in Project_root/src. The tree looks like this:

root/
    src/
       org/
          orekit/
                data/
                time/
                ...
          apache/
                ...

In my ViewController.m file I try to import with the following line without success (file not found):

#import "org/orekit/data/DataProvidersManager.h"
XaviGG
  • 171
  • 2
  • 19

1 Answers1

3

Xcode assumes all sources are in a top-level directory, so imports such as you describe fail. As described in the Xcode-Build-Rules page, the --no-package-directories flag is needed, as it outputs all generated files to the specified build directory without sub-directories, and generates correct #import directives.

A sample project that demonstrates including Java sources with packages is j2objc-sample-reversi. Its UI sucks (I wrote it, sigh), but the game engine is decent; if you need a tougher opponent, crank up the engine strength here.

tball
  • 1,984
  • 11
  • 21
  • 1
    I saw the sample project and I thought it was like that just because it was simple, not because it was a constraint. That means the only way is to rename all references in java sources before building or that I should put the files as they are but at top level? Thanks – XaviGG Jul 08 '15 at 18:06
  • Neither: use the --no-package-directories flag, and both output files and their imports are generated without sub-directories. No need to move or edit the Java sources. In your example, references to org.library.Class get imported as '#include "Class.h"' with this flag, instead of '#include "org/library/Class.h"'. – tball Jul 09 '15 at 19:08
  • Sorry but I can't manage to do it with Xcode. If I understand correctly I should import my java sources under src/... maintaining the package tree. Then use --no-package-directories to have the .h and .m files without the packages and in my application I import the classes without the package prefix. But the problem is the Xcode script: with a path in -sourcepath it doesnt find any of the files so they are not translated. Using find -name '*.java' tells me a problem with the references in the java files. Can you give me an example of this script that finds all the nested java files please? – XaviGG Jul 10 '15 at 10:06
  • SOLVED! Ok, one thing I noticed is that even if we use the xcode rules script, the import has to be done with groups and not folders. We can keep the original package structure and using the --no-package-directories all will be output to the same level. Thanks a lot for your help! – XaviGG Jul 10 '15 at 11:01
  • I'll update the doc to mention that -- thanks for the feedback! – tball Jul 11 '15 at 19:15