Ben Lefebvre
Ben Lefebvre

Reputation: 379

Creating a dll for Objective-c and java functions

I would like to create a dll that would contain definition for some functions. I would be able to use the functions in both Objective-c and java environnement.

Is it even possible to do this?

Thanks!

Upvotes: 0

Views: 745

Answers (2)

Seva Alekseyev
Seva Alekseyev

Reputation: 61331

Write in C or C++. You'll be able to link that to Objective C/Cocoa via the magic of Objective C++, and to Java on Android via NDK and JNI. That's what I do in my project. The compiler is GCC in both cases, the RTL is not identical but similar enough.

Avoid hairy data structures in the interface, stick to primitives and primitive arrays. And, naturally, you'll probably need to abstract away some of the platform.

You might want to compile your code with -fshort-wchar. It happens so that short is the native character format in both Cocoa and JNI. You'll lose widestring functions of the RTL though, but they're no use with Cocoa strings and Java strings anyway. Or you can use UTF-8 on the library/platform boundary. Conversion overhead on every call, yadda yadda.

Note: if you just want to reuse some minor helper functions, it's just easier to write them twice, or copy/paste then adjust the syntax. Debugging NDK code is notoriously tricky. Only go this way if the shared bits constitute 25-30% of the project or more. In my case, it's more like 60% shared.

EDIT: if you go this way, some further porting to other mobile platforms will be a snap and some - not so much. Specifically:

  • Samsung bada - snap (also C++ with GCC, yay)
  • Mobile Qt (Meego, etc) - snap (same as above)
  • Windows Mobile 6.5 and under - relatively easy (compiler difference between GCC and MSVC might get in the way)
  • Windows 8 tablets (AKA WinRT, Metro) - same as above
  • Blackberry Playbook - possible in theory, never tried
  • Old school Blackberry - impossible, it's all Java
  • Windows Phone 7 - impossible, it's all C#/VB.NET

Upvotes: 1

Lorenzo B
Lorenzo B

Reputation: 33428

If you want to create an application for iOS and Android and you want to reuse the business logic of your application (not the UI), take a look to Xamarin.

You can develop with C# and create Android and/or iOS apps.

From Xamarin web site:

Save time by sharing data structures and non-UI code between iOS and Android.

Hope it helps.

Upvotes: 1

Related Questions