Search code examples
j2objc

Using j2objc generated static library in a regular iOS app


I’ve created a static library for IOS using j2objc from our java sdk. Besides the j2obc transpired files, I’ve created an objective-c file from scratch with some static methods to be used (with the .a file) in our customers’ apps:

@implementation SdkLiteInterface
+ (void) initSdkIOS {
    ComInterfacomSdkTaximeterTaximeterSdk_init__();
    jboolean started = ComInterfacomSdkTaximeterTaximeterSdk_isStarted();
    NSLog(@"DEBUG SDK TaximeterSdkLiteLib initSdkIOS started = %i", started);
}

The IOS static lib project builds without errors (after creating the build rule as explained in the documentation) and generates the .a file. But when I try to use the SdkLiteInterface in a test App (with Objective-c, not Swift), the build fails with the following errors: Undefined symbols:

  _ComInterfacomSdkTaximeterTaximeterSdk_init__, referenced from:
      +[SdkLiteInterface initSdkIOS] in libTaximeterSdkLiteLib.a[2](SdkLiteInterface.o)
  _ComInterfacomSdkTaximeterTaximeterSdk_isStarted, referenced from:
      +[SdkLiteInterface initSdkIOS] in libTaximeterSdkLiteLib.a[2](SdkLiteInterface.o)

I don't understand why Xcode can't find those methods, if they are defined in SdkLiteInterface.h, that it's included in the app. Do I need to create some build rule in the app in order to use the j2objc generated static library?

The code to call the SdkLiteInterface is:

#import "SdkLiteInterface.h"
    [SdkLiteInterface initSdkIOS];

Some advise about how to use an j2objc static library in a regular IOS app?

I've followed the instructions in j2objc documentations but I haven't been able to find information about using the static library in a regular app


Solution

  • There's nothing non-standard about j2objc-created static libraries, since j2objc translates the Java sources into normal Objective-C .h and .m files with dependencies on j2objc's JRE emulation library (libjre_emul.a) and the iOS Foundation Framework.

    If Xcode can find these functions in a generated header, than they will certainly be in the static library associated with that header. To verify this, you can dump the static library using nm -gU <library>, and search the output for any missing symbols. This will both verify that the symbols are indeed present.

    Since the two functions that your test file calls are both missing, my guess is that the library isn't being linked into the app. In your build log, check the application link command to make sure it includes your library using the -l <name> flag, such as -lfoo or -l foo (either form is accepted). If your j2objc-generated static library isn't built by Xcode, than this flag is set in your Xcode project's "Other Linker Flags" setting.

    One important thing to check is that the library is named correctly; It needs to be named lib + name + .a. For example, if an app is linked with a -lfoo flag and there isn't a libfoo.a static library, linking will fail.

    Linking will also fail if the directory of the static library isn't specified, which in Xcode is done in its "Library Search Paths" build setting.