-
Notifications
You must be signed in to change notification settings - Fork 132
Compiling for iOS using j2objc
j2objc compiles Java source files into Objective-C source files, and uses similar options to javac. A squidb-ios-sample
project exists to provide a real-world example of how to set up a project using j2objc with SquiDB, and the j2objc User's Guide provides lots of helpful documentation on using j2objc. This wiki page will focus on some of the SquiDB-specific things to be aware of when compiling with j2objc.
For now, j2objc support requires building SquiDB from source when running on iOS, so it is assumed that you have cloned the SquiDB repository into a location accessible by your project. This section focuses on some of the additional setup required.
Typically, you would be compiling SquiDB as a dependency to some shared Java library containing database and business logic that you wanted to share, and this library would also be compiled from source when running on iOS. By setting up this library as an Android library, you can take advantage of using gradle to run the SquiDB code generation. (You would just need to take care that this shared library didn't use any Android classes not supported by j2objc). For example, when building with Xcode, you could add a build rule to run a gradle build on this shared library, and then copy the generated SquiDB models into a location expected by your Xcode project. The squidb-ios-sample
project uses javac to accomplish this rather than gradle, but the basic idea is the same (see here).
When using j2objc with Xcode, Java files are added to the project just like Objective-C files. A special build rule is needed to compile these files. You can see this j2objc doc for information on how to set up these build rules. The squidb-ios-sample
uses such a build rule. All SquiDB files needed at runtime, along with any other Java source files you intend to cross compile, will need to be added to the Xcode project. Using file references rather than copying the files is recommended, so that changes to the library will affect both platforms.
Note too that Squibb's generated model classes will not be visible until code generation is run, so you may need to invoke code generation manually and add the generated files to Xcode the first time you set up the project (or when new generated files are added).
When compiling for iOS, you will need to build at minimum the following modules for SquiDB:
squidb
squidb-annotations
-
squidb-ios
(both Java files in thesrc
directory and native source files in thenative
directory)
You will need to set up your SquidDatabase subclass so that it provides the low-level SQLite module that is appropriate to the platform at runtime. On Android, this is probably an instance of AndroidOpenHelper
found in the squidb-android
module; on iOS, this is an instance of IOSOpenHelper
found in the squidb-ios
module. For an example of how to do this, see the SquiDB sample projects. Files of interest:
-
TasksDatabase
, a SquidDatabase subclass -
OpenHelperCreator
, a platform-agnostic mechanism for providing ISQLiteOpenHelper instances to a SquidDatabase -
AppDelegate.m
, where the OpenHelperCreator is initialized at runtime for iOS