[Chromium文档转载,第007章]JNI on Chromium for Android
Overview
JNI (Java Native Interface) is the mechanism that enables Java code to call native functions, and native code to call Java functions.
- Native code calls into Java using apis from
<jni.h>
, which basically mirror Java's reflection APIs. - Java code calls native functions by declaring body-less functions with the
native
keyword, and then calling them as normal Java functions.
jni_generator
generates boiler-plate code with the goal of making our code:
- easier to write, and
- typesafe.
jni_generator
uses regular expressions to parse .Java files, so don't do anything too fancy. E.g.:
- Classes must be either explicitly imported, or are assumed to be in the same package. To use
java.lang
classes, add an explicit import. - Inner classes need to be referenced through the outer class. E.g.:
void call(Outer.Inner inner)
The presense of any JNI within a class will result in ProGuard obfuscation for the class to be disabled.
Exposing Native Methods
Without Crazy Linker:
- Java->Native calls are exported from the shared library and lazily resolved by the runtime (via
dlsym()
).
With Crazy Linker:
- Java->Native calls are explicitly registered with JNI on the native side. Explicit registration is necessary because crazy linker provides its own
dlsym()
, but JNI is hardcoded to use the system'sdlsym()
.- The logic to explicitly register stubs is generated by
jni_registration_generator.py
.- This script finds all native methods by scanning all source
.java
files of an APK. Inefficient, but very convenient.
- This script finds all native methods by scanning all source
- Since
dlsym()
is not used in this case, we use a linker script to avoid the cost of exporting symbols from the shared library (refer to//build/config/android:hide_all_but_jni_onload
).
- The logic to explicitly register stubs is generated by
jni_registration_generator.py
exposes two registrations methods:RegisterNonMainDexNatives
- Registers native functions needed by multiple process types (e.g. Rendereres, GPU process).RegisterMainDexNatives
- Registers native functions needed only by the browser process.
Exposing Java Methods
Java methods just need to be annotated with @CalledByNative
. The generated functions can be put into a namespace using @JNINamespace("your_namespace")
.
Usage
Because the generator does not generate any source files, generated headers must not be #included
by multiple sources. If there are Java functions that need to be called by multiple sources, one source should be chosen to expose the functions to the others via additional wrapper functions.
Calling Java -> Native
- Methods marked as
native
will have stubs generated for them that forward calls to C++ function (that you must write). - If the first parameter is a C++ object (e.g.
long mNativePointer
), then the bindings will automatically generate the appropriate cast and call into C++ code (JNI itself is only C).
Calling Native -> Java
- Methods annotated with
@CalledByNative
will have stubs generated for them. - Just call the generated stubs defined in generated
.h
files.
Java Objects and Garbage Collection
All pointers to Java objects must be registered with JNI in order to prevent garbage collection from invalidating them.
For Strings & Arrays - it's common practice to use the //base/android/jni_*
helpers to convert them to std::vectors
and std::strings
as soon as possible.
For other objects - use smart pointers to store them:
ScopedJavaLocalRef<>
- When lifetime is the current function's scope.ScopedJavaGlobalRef<>
- When lifetime is longer than the current function's scope.JavaObjectWeakGlobalRef<>
- Weak reference (do not prevent garbage collection).JavaParamRef<>
- Use to accept any of the above as a parameter to a function without creating a redundant registration.
Additional Guidelines / Advice
Minimize the surface API between the two sides. Rather than calling multiple functions across boundaries, call only one (and then on the other side, call as many little functions as required).
If a Java object “owns” a native one, store the pointer via "long mNativeClassName"
. Ensure to eventually call a native method to delete the object. For example, have a close()
that deletes the native object.
The best way to pass “compound” types across in either direction is to create an inner class with PODs and a factory function. If possible, make mark all the fields as “final”.
Build Rules
generate_jni
- Generates a header file with stubs for given.java
filesgenerate_jar_jni
- Generates a header file with stubs for a given.jar
filegenerate_jni_registration
- Generates a header file with functions to register native-side JNI methods (required only when using crazy linker).
Refer to //build/config/android/rules.gni for more about the GN templates.
Changing jni_generator
- Python unit tests live in
jni_generator_tests.py
- A working demo app exists as
//base/android/jni_generator:sample_jni_apk
[Chromium文档转载,第007章]JNI on Chromium for Android的更多相关文章
- [Chromium文档转载,第003章]Proposal: Mojo Synchronous Methods
Proposal: Mojo Synchronous Methods yzshen@chromium.org 02/02/2016 Overview Currently there are quite ...
- [Chromium文档转载,第002章]Mojo C++ Bindings API
Mojo C++ Bindings API This document is a subset of the Mojo documentation. Contents Overview Getting ...
- [Chromium文档转载,第001章] Mojo Migration Guide
For Developers > Design Documents > Mojo > Mojo Migration Guide 目录 1 Summary 2 H ...
- [Chromium文档转载,第006章]Chrome IPC To Mojo IPC Cheat Sheet
For Developers > Design Documents > Mojo > Chrome IPC To Mojo IPC Cheat Sheet 目录 1 O ...
- [Chromium文档转载,第005章]Calling Mojo from Blink
For Developers > Design Documents > Mojo > Calling Mojo from Blink Variants Let's as ...
- [Chromium文档转载,第004章]Mojo Synchronous Calls
For Developers > Design Documents > Mojo > Synchronous Calls Think carefully before ...
- 用R创建Word和PowerPoint文档--转载
https://www.jianshu.com/p/7df62865c3ed Rapp --简书 Microsoft的Office软件在办公软件领域占有绝对的主导地位,几乎每个职场人士都必须掌握Wor ...
- java实现支付宝接口--文档..转载
//实现java支付宝很简单,只要从支付宝官方下载 http://help.alipay.com/support/index_sh.htm下载程序,配置一下参数就OK了: 1.先到http:/ ...
- iOS开发主要参考文档(转载)
Objective-C,语言的系统详细资料.这是做iOS开发的前题与基础.https://developer.apple.com/library/ios/#documentation/Cocoa/Co ...
随机推荐
- 【图灵杯 A】谷神的赌博游戏
[题目链接]:http://oj.acmclub.cn/problem.php?cid=1164&pid=0 [题意] [题解] 把每个数字都%3处理; 会发现最后1的个数为n+1 2和0的个 ...
- MYSQL锁表问题的解决方法
本文实例讲述了MYSQL锁表问题的解决方法.分享给大家供大家参考,具体如下: 很多时候!一不小心就锁表!这里讲解决锁表终极方法! 案例一 mysql>show processlist; 参看sq ...
- Mock+Proxy在SDK项目的自己主动化測试实战
项目背景 广告SDK项目是为应用程序APP开发者提供移动广告平台接入的API程序集合,其形态就是一个植入宿主APP的jar包.提供的功能主要有以下几点: - 为APP请求广告内容 - 用户行为打点 - ...
- C - The C Answer (2nd Edition) - Exercise 1-12
/* Write a program that prints its input one word per line. */ #include <stdio.h> #define IN 1 ...
- 经常使用传感器协议1:CJ/T-188 水表协议解析1
本文以实例说明CJ/T-188水表协议的解析过程,下面数据未经特殊说明,均指十六进制. 数据发送: FE FE FE FE 68 10 44 33 22 11 00 33 ...
- Ext4.1 chart的使用
var reportsPanel = Ext.create('Ext.panel.Panel', { id:'reportsPanel', layout: 'fit', tbar: [{ ...
- Linux 经常使用快捷键
桌面下: Alt+F5 取消最大化窗体 Alt+F9 最小化窗体 Alt+F10 最大化窗体 Alt+空格 打开窗体的控制菜单 (点击窗体左上角图标出现的菜单) ctl+r ...
- ASP.NET Application and Page Life Cycle
ASP.NET Application Life Cycle Overview for IIS 7.0 https://msdn.microsoft.com/en-us/library/bb47025 ...
- 41.AngularJS 服务(Service)
转自:https://www.cnblogs.com/best/tag/Angular/ 什么是服务? 在 AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用. A ...
- java9新特性-5-Java的REPL工具: jShell命令
1.官方Feature 222: jshell: The Java Shell (Read-Eval-Print Loop) 2.产生背景 像Python 和 Scala 之类的语言早就有交互式编程环 ...