Java JNI vs. Android NDK - java

Could someone explain how the Java JNI and the Android NDK differ, how they are the same and how they fit together? I haven't found anything that gives a good explanation of the differences between the two and I'm a little confused.
Thanks!

JNI is just the way that Java handles calling into native/C++ code, and calling back into Java from there. It has nothing to say about Android - it is a Java language feature.
The Android NDK is a way to write Android applications using code called by JNI. It's specific to Android and gives native code access to Android APIs at that level.

Android NDK(Native Development Kit) is basically a toolchain to reuse code written in C/C++(native code). It compiles the native code into native library. NDK is similar to a Android SDK, with primary difference of using SDK only for java codes. NDK is used in application developed for multiple platforms like iOS, Windows. Apps like Whatsapp, Instagram were developed using NDK.
Java code use JNI(Java Native Interface)to call functions from native library, like accessing the objects, methods, etc. Also the native code can access the java environment.

Related

Porting an SDL 2.0 game to Android

I have a game written in C/C++ with SDL 2.0, I would like to port it to mobile platforms (specifically Android and maybe other platforms too). I read SDL has a native support for Android but the game itself is written for Windows at first, although no Windows-dependent libraries/code were used. As for Android, I only used the Java based SDK before and never integrated it with native code. Which changes (if any) do I have to make to the C/C++ so I could use it on Android platform? How exactly do I acually compile the whole thing to an APK? Is it possible to use Java and C/C++ for the application, I mean, the game's activity will be the native one but the other activities (menus, settings, etc) can be written with the regular Android Java SDK?
The Android README file in the SDL source release outlines most of what you need to know with regards to porting your SDL App to Android. There's also an example project for Eclipse that demonstrates you can modify to suit your needs.
https://hg.libsdl.org/SDL/file/d6a8fa507a45/README-android.txt
The iOS README file is also useful, namely for the sections on events and input, as much of it carries over to the Android port.
https://hg.libsdl.org/SDL/file/d6a8fa507a45/README-ios.txt
If you need help setting up the build environment, check out this (brief) tutorial which covers installing the ADK and NDK, SDL source, building a project, programming for Android and interfacing with Java.
http://www.dinomage.com/2013/01/howto-sdl-on-android/
http://www.dinomage.com/2013/05/howto-sdl-on-android-part-2-platform-details/
(1)
I dont know anything about SDL but as long as you only make calls suported by the android NDK and what ever android supported library it should be ok. I guess you would have to set up your tool chain such that it links with SDL for android?
(2)
Yes it is possible to call c++ (ndk) from the android sdk and vice versa (see 3)
(3+ rest)
See
https://developer.android.com/tools/sdk/ndk/index.html
If you download the NDK you can also see examples of how to compile with NDK to APK

how to embed a c library in android

I have an iphone app. I made my source code as boost library. Is there any possibility to embed my library to my android app? I am using eclipse. Can any one send any link about this.
At least a simple C or C++ library embedding to android and using those functions in my android app?
You need to use Android NDK tool chain for cross compiling the library. This link shows how to use it -
"http://www.cmumobileapps.com/2011/08/31/compiling-open-source-libraries-with-android-ndk-part-1/"
If you need support of STL, RTTI, C++ exception, then there's a patched up NDK available at http://www.crystax.net/android/ndk-r4.php

Developing for Android on the phone itself

I read about AIDE and it's ability to develop and compile Java directly on Android but what about C support? I read that the PC Android NDK adds full C support only if you wrap it in a dll but a newer version would allow apps to be developed directly in C without any wrapper.
Is there anything like that for Android as well? I know of c4droid and it's gcc plugin but I assume that is for developing for x86 and not ARM.
With Android Native Development Kit (NDK) you can implement parts of your application using C. http://developer.android.com/sdk/ndk/index.html. If you write native code, your applications are still packaged into an .apk file and they still run inside of a virtual machine on the device. The fundamental Android application model does not change.
Well, you could install linux (Ubuntu, in this case) on your phone and put your development environment in there, I guess.
Check https://play.google.com/store/apps/details?id=com.zpwebsites.ubuntuinstall
you can use NDK with JNI (Java Native Interface)
the following example is simple and could help you to understand quickly the NDK and JNI:
Using NDK to Call C code from Android Apps
The following document contains more examples and details concerning the use of NDK and JNI
JNI Examples for Android
Your C code will be build as dynamic linux library (.so) and will be loaded when your application start. The use of JNI will allow your JAVA code to use the C functions from the library. The use of such solution (calling C function from JAVA) is very useful especially if you have complicated and long duration algorithms. executing such algoritms in C take much less time comparing to Java.

Using MonoDroid Library apk from Java code

If i created a apk library with monodroid. Can i u use this just in Eclispe(Java) and do my rest code in Java ?
In short: no. A DLL compiled from a Mono for Android class library relies on the Mono runtime in order to run it, which wouldn't be present in a normal Java Android application. It is possible to go in the other direction though, and use Java libraries within a Mono for Android app. Xamarin has documentation of how their architecture available here.
That said, in some cases it is possible to access C# code from Java code when you're in the scope of a Mono for Android application. This question/answer has some good details about that.

Is there a scripting language I can embed into my Java app on android

I am making a game engine that is targeted for the Android platform using Java and would like to embed a scripting language into it. I have tried Jython, LuaJava, and Groovy but none of the .jars that I include into my project to utilize the languages are working. In result, my game engine fails to run.
I am aware that the Android platform compiles the app into java bytecode and then converts it into Dalvik-compatible .dex format, since Android uses the Dalvik virtual machine. So I am quite sure that, sadly, these scripting languages likely won't work for a java app project on Android.
So my question is, does there exist a scripting language that I can embed into my Java game engine that will work on the Android platform?
Thanks.
Use DeeLang
It should work for what you are using it for or at least what it sounds like you are using it for.
ref URL: http://code.google.com/p/deelang/
Clojure would be an option - that definitely works on Android, I've even got a Clojure REPL app going on my HTC as we speak!
You might also find the answers to Java: Scripting language (macro) to embed into a Java desktop application useful.
LuaJava can be used on Android. Precisely I started with AndroLua which offers a working example.

Categories

Resources