SlideShare a Scribd company logo
How do I?
Create a Native Interface
What’s a Native Interface?
✦Native interfaces allow us to invoke OS native code
✦We can use it directly in the app or wrap it in a library
(cn1lib) for a common abstraction
✦Native in Codename One is different from native in Java.
In Java native is always C, in Codename one it’s
something else
© Codename One 2017 all rights reserved
Native in Codename One
✦Android - Java (Dalvik/Art) code, you can still wrap
NDK code with AAR
✦iOS - Objective-C code and native libraries
✦Windows - C# code
✦JavaScript - JavaScript code
✦Desktop Port - Java SE code
© Codename One 2017 all rights reserved
package com.codename1.app.nativedemo;
import com.codename1.system.NativeInterface;
public interface CallNativeCode extends NativeInterface {
public String doThisInNative(String arg1, int arg2);
}
Hello World Native Interface
Generate Native Interface
✦The Generate Native Access action automatically
generates required native code stubs
✦Stubs will appear under the “native” directory
© Codename One 2017 all rights reserved
package com.codename1.app.nativedemo;
public class CallNativeCodeImpl {
public String doThisInNative(String param, int param1) {
return null;
}
public boolean isSupported() {
return false;
}
}
Android Stub
package com.codename1.app.nativedemo;
public class CallNativeCodeImpl {
public String doThisInNative(String param, int param1) {
return "Dummy";
}
public boolean isSupported() {
return true;
}
}
Android Implementation
#import "com_codename1_app_nativedemo_CallNativeCodeImpl.h"
@implementation com_codename1_app_nativedemo_CallNativeCodeImpl
-(NSString*)doThisInNative:(NSString*)param param1:(int)param1{
return nil;
}
-(BOOL)isSupported{
return NO;
}
@end
iOS Stub
#import "com_codename1_app_nativedemo_CallNativeCodeImpl.h"
@implementation com_codename1_app_nativedemo_CallNativeCodeImpl
-(NSString*)doThisInNative:(NSString*)param param1:(int)param1{
return @"Dummy";
}
-(BOOL)isSupported{
return YES;
}
@end
iOS Implementation
namespace com.codename1.app.nativedemo{
public class CallNativeCodeImpl : ICallNativeCodeImpl {
public string doThisInNative(String param, int param1) {
return null;
}
public bool isSupported() {
return false;
}
}
}
Windows Stub
(function(exports){
var o = {};
o.doThisInNative__java_lang_String_int = function(param1, param2, callback) {
callback.error(new Error("Not implemented yet"));
};
o.isSupported_ = function(callback) {
callback.complete(false);
};
exports.com_codename1_app_nativedemo_CallNativeCode= o;
})(cn1_get_native_interfaces());
JavaScript Stub
CallNativeCode cn = NativeLookup.create(CallNativeCode.class);
if(cn != null && cn.isSupported()) {
String returnVal = cn.doThisInNative("String", 1);
}
Using the Code
Implementation
✦Code doesn’t compile on the client & won’t have code
completion
✦A neat trick is using “include source” and then opening
the native source in xcode or Android Studio and
implementing the native code there
✦You can’t use all types. You can’t pass objects!
✦Use PeerComponent to represent a native UI widget
✦You can use callbacks from the native code
© Codename One 2017 all rights reserved
Build Hints
✦You can use build hints to inject many native interface
behaviors
✦Native code on Android often requires a 3rd party library
thru gradle, native code on iOS often uses cocoapods
we support both thru build hints
✦You can inject values into the manifest or plist to make
use of this
✦You can look thru existing cn1libs for sample code
© Codename One 2017 all rights reserved
Thank You!

More Related Content

PDF
Bootstrap |> Elixir - Easy fun for busy developers
PDF
Elixir and OTP
PDF
Ask The Expert - Typescript: A stitch in time saves nine
PPTX
PHP Introduction and Training Material
PPTX
Flying under the radar
PDF
interface
PPT
PHP and COM
PPT
TDD, BDD, RSpec
Bootstrap |> Elixir - Easy fun for busy developers
Elixir and OTP
Ask The Expert - Typescript: A stitch in time saves nine
PHP Introduction and Training Material
Flying under the radar
interface
PHP and COM
TDD, BDD, RSpec

What's hot (20)

PDF
Zeppelin Helium: Spell
PPTX
Clean Code
PDF
Diving into HHVM Extensions (PHPNW Conference 2015)
PPT
jQuery Plugin
PDF
C++ for Java Developers (SwedenCpp Meetup 2017)
PDF
Anti Object-Oriented Design Patterns
PPT
C++basics
PPT
C++basics
PPTX
Pure virtual function and abstract class
ODP
Incredible Machine with Pipelines and Generators
PDF
TDD with PhpSpec - Lone Star PHP 2016
PPT
PPTX
Php string function
PDF
Protocol in Swift
PDF
Exporter Proxy
PDF
4.2 PHP Function
PDF
Hug presentation for android tech talks #14
PDF
vienna.js - Automatic testing of (RESTful) API documentation
PPT
02 Php Vars Op Control Etc
PDF
Conscious Decoupling - Lone Star PHP
Zeppelin Helium: Spell
Clean Code
Diving into HHVM Extensions (PHPNW Conference 2015)
jQuery Plugin
C++ for Java Developers (SwedenCpp Meetup 2017)
Anti Object-Oriented Design Patterns
C++basics
C++basics
Pure virtual function and abstract class
Incredible Machine with Pipelines and Generators
TDD with PhpSpec - Lone Star PHP 2016
Php string function
Protocol in Swift
Exporter Proxy
4.2 PHP Function
Hug presentation for android tech talks #14
vienna.js - Automatic testing of (RESTful) API documentation
02 Php Vars Op Control Etc
Conscious Decoupling - Lone Star PHP
Ad

Similar to How do i - create a native interface (20)

PDF
How do I - Create a Native Interface.pdf
PDF
How do I - Create a Native Interface - Transcript.pdf
PPTX
July 2015 Android Taipei - Anti-Decompiler by SUKI
PPTX
Lies Told By The Kotlin Compiler
PDF
Best Practices in apps development with Titanium Appcelerator
PDF
BEST PRACTICES PER LA SCRITTURA DI APPLICAZIONI TITANIUM APPCELERATOR - Aless...
PPTX
NativeScript and Angular
PDF
Javascript Best Practices
PDF
One Step Ahead of Cheaters -- Instrumenting Android Emulators
KEY
Titanium appcelerator best practices
PDF
Extension and Evolution
PPTX
Angular 2 and NativeScript
PDF
Hacking the Codename One Source Code - Part IV - Transcript.pdf
PDF
PIL - A Platform Independent Language
PPTX
Android RenderScript
PPTX
Ignite your app development with Angular, NativeScript and Firebase
PDF
JavaScript Core
PDF
Your Business. Your Language. Your Code - dpc13
PDF
"Xapi-lang For declarative code generation" By James Nelson
PDF
Code and Conquer with Globe Labs, October 27, 2012
How do I - Create a Native Interface.pdf
How do I - Create a Native Interface - Transcript.pdf
July 2015 Android Taipei - Anti-Decompiler by SUKI
Lies Told By The Kotlin Compiler
Best Practices in apps development with Titanium Appcelerator
BEST PRACTICES PER LA SCRITTURA DI APPLICAZIONI TITANIUM APPCELERATOR - Aless...
NativeScript and Angular
Javascript Best Practices
One Step Ahead of Cheaters -- Instrumenting Android Emulators
Titanium appcelerator best practices
Extension and Evolution
Angular 2 and NativeScript
Hacking the Codename One Source Code - Part IV - Transcript.pdf
PIL - A Platform Independent Language
Android RenderScript
Ignite your app development with Angular, NativeScript and Firebase
JavaScript Core
Your Business. Your Language. Your Code - dpc13
"Xapi-lang For declarative code generation" By James Nelson
Code and Conquer with Globe Labs, October 27, 2012
Ad

More from Shai Almog (10)

PDF
The Duck Talks Back - Secrets of The Debugging Masters
PDF
Logging Best Practices - Avoid the rinse repeat CI/CD Process
PDF
Cloud Native Debugging in Production - Dig Deep into your agents
PDF
How do i - localization and internationalization (l10n, i18n)
PPTX
5 Amazing Features in Codename One 3.7
PDF
Understand Properties in Codename One
PDF
What is codename one
PDF
How To Use The Codename One Sources
PDF
Codename one Cordova/PhoneGap Support
PPTX
Public showcase
The Duck Talks Back - Secrets of The Debugging Masters
Logging Best Practices - Avoid the rinse repeat CI/CD Process
Cloud Native Debugging in Production - Dig Deep into your agents
How do i - localization and internationalization (l10n, i18n)
5 Amazing Features in Codename One 3.7
Understand Properties in Codename One
What is codename one
How To Use The Codename One Sources
Codename one Cordova/PhoneGap Support
Public showcase

Recently uploaded (6)

PDF
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
PDF
heheheueueyeyeyegehehehhehshMedia-Literacy.pdf
PPTX
ASMS Telecommunication company Profile
DOC
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
DOC
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
PDF
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
heheheueueyeyeyegehehehhehshMedia-Literacy.pdf
ASMS Telecommunication company Profile
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf

How do i - create a native interface