Native interfaces allow developers to invoke platform native methods/functions/libraries and even widgets directly from within Codename One without having to adapt your code to every platform. They are a very powerful tool when bridging between Codename One and OS specific features to access functionality that might not yet be exposed in the Codename One platform.
The first step is creating an interface that extends NativeInterface. The methods that you define here will be generated to the various native platforms, notice that you can only use very specific types for arguments/return types specifically: Primitive types, arrays of primitive types, String and PeerComponent.
Once you defined your native interface you can right click and generate the actual native code. This will create the appropriate native code in the native directories including Objective-C code in the iOS directory, Java Dalvik code in the Android directory, C# code in the Windows Phone directory and Java SE code in the Java SE directory.
You can now implement the native interfaces for every platform by default we create an isSupported method in NativeInterface to allow you to ignore a specific platform. If you wish to make use of a JAR or an iOS static library you can just put these libraries in the appropriate native directory next to the native sources and then just use the library as if its included.
Notice that the native interface implementation doesn't implement the actual native interface definition except for the case of Java SE. This is because the server needs to bridge the native interface calls during build, however in the case of Java SE we need to load the compiled class locally without a build server so this is a special case.You can use the native interfaces from code very easily just lookup the native interface type and you shall receive a new implementation instance if its available. Its good practice to check if a native interface is supported before invoking it to avoid a case where a specific platform isn't implemented.
Some platforms need additional build hints in order to support various native code requirements for instance iOS often needs the build argument ios.add_libs to include additional frameworks and android sometimes needs manifest changes via the build hints. These things are pretty easy to debug and customize if you use the include sources option of Codename One.
I hope you found this tutorial useful thanks for watching
Android 3rd Party Libraries & JNI
Learn about integrating Android native libraries and even JNI code into native interfaces thru the andlib system Read More
Our cn1lib section features many open source libraries that contain a lot of sample code you can use for reference or as a starting point Read More
How To Use Include Source
The include source feature is remarkably useful when working with native code Read More