博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android chromium 2
阅读量:7070 次
发布时间:2019-06-28

本文共 4789 字,大约阅读时间需要 15 分钟。

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:

  1. easier to write, and
  2. 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's dlsym().
    • 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.
    • 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).
  • 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 files
  • generate_jar_jni - Generates a header file with stubs for a given .jar file
  • generate_jni_registration - Generates a header file with functions to register native-side JNI methods (required only when using crazy linker).

Refer to  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

转载于:https://www.cnblogs.com/huangguanyuan/p/9985023.html

你可能感兴趣的文章
Dubbo分析之Cluster层
查看>>
Titan Framework MongoDB深入理解3
查看>>
iOS核心动画笔记2-寄宿图
查看>>
几种不同类型网站内容优化切入点
查看>>
SaltSack入门(三)Salt相关命令和Pillar应用
查看>>
文件上传相关内容
查看>>
Linux下Nodejs安装(完整详细)
查看>>
OSChina 周四乱弹 —— 想换行了怎么办,别说按回车键
查看>>
ubuntu11.04安装JDK7及配置环境
查看>>
SpringBoot+Logback+Sentry(日志监控平台)
查看>>
vmware10中开启Intel VT-x
查看>>
一般wsdl生成的类都比较大,IDEA类文件大小限制导致无法加载该类问题
查看>>
Android 垂直Tab
查看>>
Android DecorView与ViewRootImpl
查看>>
goroutine 相关知识7
查看>>
redis命令学习笔记
查看>>
原生JS实现"旋转木马"效果的图片轮播插件
查看>>
RabbitMQ镜像队列
查看>>
怎么实现ABBYY FineReader 中的图像及文字的设置
查看>>
免费人脸识别APi
查看>>