All Projects → xbmc → libandroidjni

xbmc / libandroidjni

Licence: GPL-2.0 license
Android JNI bindings library

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to libandroidjni

Androidsecurity
Android安全实践
Stars: ✭ 150 (+127.27%)
Mutual labels:  ndk, native
webviewhs
🌐 A Haskell binding to the webview library created by Serge Zaitsev.
Stars: ✭ 109 (+65.15%)
Mutual labels:  native, bindings
webgl-raub
WebGL bindings to desktop OpenGL
Stars: ✭ 66 (+0%)
Mutual labels:  native, bindings
FFmpeg-3.4-Android
This repository helps to compile FFmpeg 3.4 Version for Android using Android NDK Standalone Toolchain.
Stars: ✭ 23 (-65.15%)
Mutual labels:  ndk, androidndk
Android Luajit Launcher
Android NativeActivity based launcher for LuaJIT, implementing the main loop within Lua land via FFI
Stars: ✭ 87 (+31.82%)
Mutual labels:  ndk, native
Camerakit Android
Library for Android Camera 1 and 2 APIs. Massively increase stability and reliability of photo and video capture on all Android devices.
Stars: ✭ 5,131 (+7674.24%)
Mutual labels:  ndk, native
Lambda Lantern
🧙 ‎‎ A 3D game about functional programming patterns. Uses PureScript Native, C++, and Panda3D.
Stars: ✭ 122 (+84.85%)
Mutual labels:  native, bindings
Stunning Signature
Native Signature Verification For Android (with example)
Stars: ✭ 139 (+110.61%)
Mutual labels:  ndk, native
Jpegkit Android
Efficient JPEG operations for Android without the risk of an OutOfMemoryException.
Stars: ✭ 154 (+133.33%)
Mutual labels:  ndk, native
x264-rs
x264 bindings
Stars: ✭ 32 (-51.52%)
Mutual labels:  bindings
rust-flutter-reactive
This is a sample app to improve consistency over Mobile App Development.
Stars: ✭ 25 (-62.12%)
Mutual labels:  native
ti.coremotion
Support for the native iOS CoreMotion framework in Appcelerator Titanium
Stars: ✭ 15 (-77.27%)
Mutual labels:  native
OpenGL.NET
This repository contains low-level bindings for OpenGL and OpenGLES used in Evergine.
Stars: ✭ 29 (-56.06%)
Mutual labels:  bindings
vsphere-automation-sdk-.net
[DEPRECATED] Please see README. C# samples, language bindings, and API reference documentation for vSphere, VMC, and NSX-T using the VMware REST API
Stars: ✭ 67 (+1.52%)
Mutual labels:  bindings
ult
The Ultimate Dev Stack
Stars: ✭ 54 (-18.18%)
Mutual labels:  native
kindaVim.theapp
Ultimate Vim Mode for macOS
Stars: ✭ 372 (+463.64%)
Mutual labels:  bindings
titanium-vue
Use Vue.js to easily create native mobile apps with Axway Appcelerator Titanium.
Stars: ✭ 45 (-31.82%)
Mutual labels:  native
UseCmakeBuildLib
用Cmake的方式构建a静态库,so动态库,且集成到项目中
Stars: ✭ 46 (-30.3%)
Mutual labels:  ndk
cl-liballegro
Common Lisp bindings and interface to the Allegro 5 game programming library
Stars: ✭ 44 (-33.33%)
Mutual labels:  bindings
interop
Python/C/Go/Rust/Haskell interop examples
Stars: ✭ 24 (-63.64%)
Mutual labels:  bindings
libandroidjni
=============

Quick and dirty readme.

1. What is this?

It is a native wrapper for JNI specifically designed to provide easy access
to the Android API. The goal is to create a 1:1 mapping of java functionality
to native apps.

2. Is that possible?

No. But we can try :)
Java and c++ have some fundamental differences that make an exact mapping
impossible. But we can come close.

3. How does it work?

The native classes use JNI to call into java while hiding the gory details from
the user. Under the hood it's quite un-elegant. Typical JNI objects created and
passed around using a thin wrapper. This wrapper is a heavily modified version
of one found in libcrystax. Thanks crystax!

4. What does it look like?

  Here's a quick example:

  void MyFunction()
  {
    // Find the launch intent for a package and start its activity
    CJNIPackageManager manager = getPackageManager();
    CJNIIntent intent = manager.getLaunchIntentForPackage("org.xbmc.kodi");
    startActivity(sendIntent);
  }

This tiny bit of functionality would require dozens of lines if using JNI
directly, but here it looks just like java code.

5. How do I use it in my app?
It is assumed that you are using a NativeActivity. Apps should create a main
class that inherits from CJNIContext, and passes android_app->state->clazz to
the constructor. This class now acts like a standard NativeActivity which
inherits from Context. This class can now call the same functions that a Java
activity can. Additionally, a virtual OnReceive function will catch broadcast
events.

TODO: Rename and make the distinction between NativeActivity and Context
classes.

6. How do I interact with the classes?

Just as you would with java. There is no documentation for usage, because the
Android API documentation should be sufficient.

7. What are the caveats?

With any luck, very few. This library is still very new and has not been
exposed to many use-cases yet. Here are a few:

a. Java can return NULL objects (not null-pointers).
workaround: objects can be tested as a bool.

Here's an example of a function that would crash:

  void MyFunction()
  {
    std::string externalDir = getExternalFilesDir("foo").getAbsolutePath();
    return externalDir;
  }

The Android API specifies that getExternalFilesDir("") can return NULL if the
path is not found. Thus getAbsolutePath() has no instance and would crash when
called.

The fix:

  std::string MyFunction()
  {
    std::string externalDir;
    CJNIFile myFile = getExternalFilesDir("foo");
    if (myFile)
      externalDir myFile.getAbsolutePath();
    retrun externalDir;
  }

b. Java has an understanding of arrays like Type[] which contain size info.
   C-style arrays don't carry size information, so passing a c-array as a
   parameter does not provide enough info to work with it realistically.

   workaround: Data should be passed in/out of Java arrays as std::vectors of
   primitive types or jni classes.
   Care should be taken to avoid making needless copies of objects in the
   process. To automate this, jcast() can be used to convert a j(h)objectArray
   directly to a vector of native objects. For example:

  std::vector<CJNIFoo> example()
  {
    return jcast<std::vector<CJNIFoo> >(call_method<jhobjectArray>(object, "function", "()[Lsome/Class;"))
  }

c. Probably lots more.

8. Is the entire API implemented?
Not even close! Classes and individual functions have been added as-needed.

9. OK, how do I add xyz class?
Each (non-static) class inherits from CJNIBase. This class stores an object
for the subclass, handles copying, etc. The class is expected to provide
functions and members of the same name as the Android api. Helper functions
are used to make JNI usage less painful.

Simplified Example:

  #include "JNIBase.h"
  class CJNISomeClass : public CJNIBase
  {
  public:
    CJNISomeClass();
    CJNISomeClass(const jni::jhobject &object) : CJNIBase(object){};
    std::string getSomeString();
  };

  CJNISomeClass::CJNISomeClass() : CJNIBase("android/some/ClassName")
  {
    m_object = new_object(GetClassName());
  }

  std::string CJNISomeClass::getSomeString()
  {
    return jcast<std::string>(call_method<jhstring>(m_object, "getSomeString", "()Ljava/lang/String;"));
  }

When a CJNISomeClass is created, it will use the hard-coded classname to
construct a new instance (<init> is called on the class).
If a CJNISomeClass is created from another CJNISomeClass, its underlying object
will be used to create a new copy.

Note that jhobjects can be cast to and from their parent classes. This is to
facilitate easy chaining of functions.

new_object and call_method are helper functions that do auto-lookups and
casting of params and the result. See jutils/jutils-details.hpp for more
helpers.

10. What is the lifetime of the created objects?
All native objects are returned as Global refs. This is overkill, but it was
done in order to reduce complexity. All refs are automatically destroyed when
objects go out of scope. Because of this, all objects should be passed as
const ref whenever possible to avoid unnecessary reference creation.

CJNISomeClass objects should almost never be long-lived, as some environments
may have hard-limits for the amount that can be allocated.

11. Why bother? Aren't there programs to generate these wrappers automatically?
Sure, but none of them seemed to do it in a way that reduced complexity to a
nominal level.

12. Is it stable?
It works as-tested, but hasn't seen much real-world exposure. Time will tell.
Don't use it in production.

13. What's with the CJNI naming?
This library was created for XBMC and classes were created using XBMC
naming conventions. If it is adopted elsewhere, the names should be changed to
something more standard.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].