Overview
Java Native Access (JNA) hides all the complexities of Java Native Interface (JNI) and allows java developers to call native code using 100% pure Java without having to write C/C++ code. We can directly call native functions using plain java methods, no boilerplate or generated glue code is required.
This post covers
- Creating basic Android Studio project
- Downloading JNA AAR library
- Importing and configuring JNA AAR library
- Calling C/C++ code from Java
Requirements
- Android Studio 3.0 or higher
Difficulty
- Intermediate
Guide
1. Creating basic Android Studio project
Create new android project and change Application name you can also change Company domain and Package name according to requirements and click next
Select minimum SDK version and click next
Select Empty Activity and click next
Change Activity Name and Layout Name according to requirements and click finish to create project
Project has been created after successful gradle builld
2. Downloading JNA AAR library
Download jna.aar from here
3. Importing and configuring AAR library
Create New Module and select Import JAR/AAR Package and click next
Select jna.aar file from file system and click Finish
Now jna module added to project, open build.gradle file and add jna module under dependencies
dependencies {
compile project(':jna')
....
....
}
4. Calling C/C++ code from Java
Native method we will call is available in android C shared library (libc)
pid_t getpid(void);
getpid() returns the process ID (PID) of current running process. Return type is int and accepts no arguments.
Mapping this C function to Java is simple, Java method name will be same as C method name, return type is int with no arguments and we have to add native keyword which tells compiler that method is implemented in native code.
public static native int getpid();
Last step is to load native shared library in static block which will load shared library during class loading time.
static {
Native.register(MainActivity.class, Platform.C_LIBRARY_NAME);
}
First argument to Native.register() method is the class where we defined our native methods and 2nd argument is the name of native library in our case is C library which has getpid() method.
Calling native method is same as calling other java methods
System.out.println("process id = " + getpid());
Output
Process id of our running app is printed in logcat window, highlighted in screenshot
Conclusion
JNA hides all the complexities of Java Native Interface (JNI) and allows developers to write pure java code without writing C/C++ code. It removes the overhead of writing C/C++ code, headers files, C to Java type conversions and vice versa, memory management etc.
JNA is available on all major desktop platforms and also on android platform.
Code
Project is available on github
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thanks
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @kabooom I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thanks and i voted for you
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit