Android chromium 2
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
nativekeyword, 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.langclasses, 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
.javafiles 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.pyexposes 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
nativewill 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
@CalledByNativewill have stubs generated for them. - Just call the generated stubs defined in generated
.hfiles.
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.javafilesgenerate_jar_jni- Generates a header file with stubs for a given.jarfilegenerate_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
Android chromium 2的更多相关文章
- Android Chromium WebView学习启动篇
Android从4.4起提供基于Chromium实现的WebView.此前WebView基于WebKit实现.WebKit提供网页解析.布局和绘制以及JS执行等基础功能.Chromium在WebKit ...
- Android chromium 1
For Developers > Design Documents > Java Resources on Android Overview Chrome for Android ...
- 35 Top Open Source Companies
https://www.datamation.com/open-source/35-top-open-source-companies-1.html If you think of open sour ...
- DevTools 实现原理与性能分析实战
一.引言 从 2008 年 Google 释放出第一版的 Chrome 后,整个 Web 开发领域仿佛被注入了一股新鲜血液,渐渐打破了 IE 一家独大的时代.Chrome 和 Firefox 是 W3 ...
- Ubuntu下编译Chromium for Android
转自:http://blog.csdn.net/fsz521/article/details/18036835 下源码git clone https://chromium.googlesource.c ...
- 理解WebKit和Chromium: 调试Android系统上的Chromium
转载请注明原文地址:http://blog.csdn.net/milado_nju 1. Android上的调试技术 在Android系统上,开发人员能够使用两种不同的语言来开发应用程序,一种是Jav ...
- chromium for android v34 2dcanvas硬件渲染实现分析
这篇接着上一篇2dcanvas硬件绘制,分析保存绘制结果的texture被合成到on screen framebuffer上的过程. 1.webkit为canvas元素相应的render树节点Rend ...
- Chromium on Android: Android在系统Chromium为了实现主消息循环分析
总结:刚开始接触一个Chromium on Android时间.很好奇Chromium主消息循环是如何整合Android应用. 为Android计划,一旦启动,主线程将具有Java消息层循环处理系统事 ...
- 理解WebKit和Chromium: Android 4.4 上的Chromium WebView
转载请注明原文地址:http://blog.csdn.net/milado_nju ## 概述 相信读者已经注意到了,在最新的Android 4.4 Kitkat版本中,原本基于Android Web ...
随机推荐
- zzulioj--1799--wrz的压岁钱(贪心)
1799: wrz的压岁钱 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 407 Solved: 71 SubmitStatusWeb Boa ...
- 访问视频资源报错:Failed to load resource: net::ERR_CONNECTION_RESET
访问视频资源报错: 浏览器显示:Failed to load resource: net::ERR_CONNECTION_RESET. 原因:公司内部限制了访问外网资源,凡是视频资源都不能访问. 解决 ...
- stm8s103 EEPROM烧程序时能否保留
EEPROM的参数需要再烧录程序时保留,做试验测试是否能够保留 1.在ST Visual Develop中硬件仿真手动修改EEPROM的值. 2.在ST Visual Programmer中读取EEP ...
- ActiveMQ学习笔记(4)----JMS的API结构和开发步骤
1. JMS的API结构 其实上图中的五个API在第一节中我们都已经使用到了.本节将会讲非持久化和持久化topic的使用. 2. JMS的基本开发步骤 1. 创建一个JMS工厂, Connectio ...
- SpringBoot 获取客户端 ip
/** * 获取客户端ip地址 * @param request * @return */ public static String getCliectIp(HttpServletRequest re ...
- UVA-12083 Guardian of Decency 二分图 最大独立集
题目链接:https://cn.vjudge.net/problem/UVA-12083 题意 学校组织去郊游,选择最多人数,使得任意两个人之间不能谈恋爱 不恋爱条件是高差大于40.同性.喜欢的音乐风 ...
- FTP 无法获取目录列表的处理方法
FTP 无法获取目录列表的处理方法 1.以阿里云的服务器为例 对于阿里云的服务器是因为阿里云为了进一步保护用户的安全利益使用了安全策略组,我们要设置安全策略组对应的端口开启. 首先要设置端口范围,这个 ...
- 教你用webpack搭一个vue脚手架[超详细讲解和注释!]
1.适用人群 1.对webpack知识有一定了解但不熟悉的同学. 2.女同学!!!(233333....) 2.目的 在自己对webpack有进一步了解的同时,也希望能帮到一些刚接触webpack的同 ...
- HTTP——学习笔记(3)
HTTP报文:用于HTTP协议交互的信息,客户端的HTTP报文叫做 请求报文,响应端的叫做 响应报文 本质:是由多行(用CR+LF作换行符)数据构成的字符串文本 注:CR:回车,打印针回到行首 L ...
- HDU 1827 Summer Holiday(强连通)
HDU 1827 Summer Holiday 题目链接 题意:中文题 思路:强连通缩点,每一个点的权值为强连通中最小值,然后入度为0的点就是答案 代码: #include <cstdio> ...