0

I've been using J2ObjC for a while now to share code code between Android and iOS. Every time I come to create an Archive to deploy I have trouble with imports in -Bridging-Header.h not being found. I usually fiddle with Header Search Paths and User Header Search Paths, Cleaning and/or restarting Xcode until it eventually works. But next time I come to Archive, without changing anything, it's not working again. It works OK when building normally.

In the bridging header I import my translated Java classes using:

#import "Db.h"

and similar. I get an error:

'Db.h' file not found
Failed to import bridging header <bridging header filename> 

So, it's finding the header but not the files.

The files are there in the DerivedSources directory. I've tried having the path to the directory in Header Search Paths and/or User Header Search Paths. I've tried using ${DERIVED_FILES_DIR} as well as the absolute path.

My question: Which configuration option is used as the path to resolve these imports in the bridging header? What could be causing it not to find these files when they are there?

Xcode 10.3, Swift 4.2 J2ObjC may be a red fish, but I'm using version 2.4

Kiwi
  • 109
  • 8

1 Answers1

0

J2ObjC files require JRE (Java Runtime Environment) support classes, just like their Java sources do. The JRE framework is in j2objc-2.4/frameworks/, so open that directory and drag the JRE framework to your project's Frameworks section. If your files use any other j2objc libraries like Guava, drag them into the project, too.

Next, open Db.h and copy the #include lines into your project's Swift bridging header (it may not matter, but I would also change each #include to #import).

Finally, if some methods from your Java class are visible in Swift but not others, add any missing types from the missing method's arguments or return types to the bridging header. The Swift importer skips methods when it can't find the header for any referenced types, so make sure they're all included.

tball
  • 1,984
  • 11
  • 21
  • Thanks Tom, and thanks for your great work on J2ObjC. I've done the above, but when I build it's still not finding my Db.h file when I'm archiving. It can find it OK when building to test on a device. That file is in the ArchiveIntermediates DerivedSources folder but is not being picked up. I've even tried putting the absolute path to DerivedSources into Header Search Paths but no joy. – Kiwi Aug 06 '19 at 21:28
  • P.S. It does say #include not #import - that was a typo in the question – Kiwi Aug 06 '19 at 21:45
  • Tom, do you have a J2ObjC Swift project that Archives OK? If so, could you tell me what you have in Header Search Paths and User Header Search Paths? – Kiwi Aug 07 '19 at 21:09
  • Swift Project Builds and runs but fails to Archive: https://stackoverflow.com/questions/24536835/swift-project-builds-and-runs-but-fails-to-archive – tball Aug 08 '19 at 18:44
  • The only J2ObjC Swift project I'm aware is our HelloSwift example, though it hasn't been tested for Archiving since it's not a production app: https://github.com/google/j2objc/tree/master/examples/HelloSwift – tball Aug 08 '19 at 18:48
  • It appears that the problem was one of timing, perhaps due to parallelism going on in the compiler. My J2ObjC .h files were being generated, but perhaps intermittently not being available by the time the Swift compiler needed them. I fixed it with the horrible hack of copying the .h and .m files elsewhere, then pasting them into the DerivedSources folder while the Archive build was in progress. If someone know a better way, please say so! Maybe I could make my Java/J2ObjC files into a separate project,make a library from and to include in my main project. I don't know how to though – Kiwi Aug 11 '19 at 11:54
  • What seems to work best (in my experience) is building Java sources as a separate ObjC static library or framework, then having the Swift app depend on it. Xcode is good about building dependent targets first, but assumes that source files within a project aren't dependent upon each other. – tball Aug 12 '19 at 18:09
  • Thanks Tom, that sounds sensible. – Kiwi Aug 13 '19 at 19:32