ChromiumJniGenerator - Jni Generator module extracted from Chromium project

Overview

How to use the ChromiumJniGenerator

1 Dependencies

1.1 C ++ dependencies

Copy two directories: chromium-jni-generator-native-essential and chromium-jni-generator-native-gen-script to your project pic1 The chromium-jni-generator-neutral-essential directory contains the chromium_jni_generator_native_essential .h and chromium_jni_generator_native_essential.cc pic2 The chromium-jni-generator-native-gen-script directory contains python scripts that generate native code pic3 chromium_native_code_gen.sh is an example of using python scripts to generate native code

#!/bin/bash

#create code generation directories
rm -rf ./gen
mkdir -p gen/cpp
mkdir -p gen/java

#generate native code
./jni_generator.py --input_file XXXXA.java --input_file XXXXB.java --output_file gen/cpp/XXXXA.h --output_file gen/cpp/XXXXB.h

1.2 Java dependencies

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    compileOnly 'com.github.allenxuan.ChromiumJniGenerator:chromium-jni-generator-jvm-annotations:v1.0.0'
    kapt 'com.github.allenxuan.ChromiumJniGenerator:chromium-jni-generator-jvm-compiler:v1.0.0'
}

2 Java Layer Access C/C ++ Layer ( JNI )

Define native functions in the Java layer

`ChromiumJniTestA.java`

@JNINamespace("test::your::name::space")
public class ChromiumJniTestA {
//Work
public static native boolean nativeFunctionAA(boolean param1, float param2);

    //Not work. "static" should be put in front of "native".
    public native static boolean nativeFunctionAB(boolean param1, float param2);

    //Work
    public native long nativeFunctionAC(String param1);

    //Not work. The function name should starts with "native".
    public native long functionAD(String param1);
}

Or define native methods with @NativeMethods annotations

`ChromiumJniTestB.java`

@JNINamespace("test::your::name::space")
public class ChromiumJniTestB {
@NativeMethods
interface Natives {
boolean nativeFunctionBA(boolean param1, float param2);

        float functionBB(String param1);
    }
}

Java Annotation Processor generates auxiliary classes ChromiumJniTestBJni.java and GEN_JNI.java from @NativeMethods

`ChromiumJniTestBJni.java`

class ChromiumJniTestBJni implements ChromiumJniTestB.Natives {
  @Override
  public boolean nativeFunctionBA(boolean param1, float param2) {
    return (boolean)GEN_JNI.org_chromium_chromiumjnigenerator_ChromiumJniTestB_nativeFunctionBA(param1, param2);
  }

  @Override
  public float functionBB(String param1) {
    return (float)GEN_JNI.org_chromium_chromiumjnigenerator_ChromiumJniTestB_functionBB(param1);
  }

  public static ChromiumJniTestB.Natives get() {
    return new ChromiumJniTestBJni();
  }
}
`GEN_JNI.java`

public final class GEN_JNI {

  /**
   * org.chromium.chromiumjnigenerator.ChromiumJniTestB.functionBB
   * @param param1 (java.lang.String)
   * @return (float)
   */
  public static final native float org_chromium_chromiumjnigenerator_ChromiumJniTestB_functionBB(
      String param1);

  /**
   * org.chromium.chromiumjnigenerator.ChromiumJniTestB.nativeFunctionBA
   * @param param1 (boolean)
   * @param param2 (float)
   * @return (boolean)
   */
  public static final native boolean org_chromium_chromiumjnigenerator_ChromiumJniTestB_nativeFunctionBA(
      boolean param1, float param2);
}

Generate JNI functions through jni_generator.py

./jni_generator.py --input_file ../java/org/chromium/chromiumjnigenerator/ChromiumJniTestA.java --output_file ../cpp/gen/ChromiumJniTestA.h --input_file ../java/org/chromium/chromiumjnigenerator/ChromiumJniTestB.java --output_file ../cpp/gen/ChromiumJniTestB.h
`ChromiumJniTestA.h`

static jboolean JNI_ChromiumJniTestA_FunctionAA(JNIEnv* env, jboolean param1,
    jfloat param2);

JNI_GENERATOR_EXPORT jboolean
    Java_org_chromium_chromiumjnigenerator_ChromiumJniTestA_nativeFunctionAA(
    JNIEnv* env,
    jclass jcaller,
    jboolean param1,
    jfloat param2) {
  return JNI_ChromiumJniTestA_FunctionAA(env, param1, param2);
}

static jlong JNI_ChromiumJniTestA_FunctionAC(JNIEnv* env, const
    chromium::android::JavaParamRef
   & jcaller,
    
   const chromium::android::JavaParamRef
   
    & param1);

JNI_GENERATOR_EXPORT jlong 
    Java_org_chromium_chromiumjnigenerator_ChromiumJniTestA_nativeFunctionAC(
    JNIEnv* env,
    jobject jcaller,
    jstring param1) {
  
    return 
    JNI_ChromiumJniTestA_FunctionAC(env, chromium::android::JavaParamRef
    
     (env,
      jcaller), chromium::android::JavaParamRef
     
      (env, param1));
}
     
    
   
  
`ChromiumJniTestB.h`

static jboolean JNI_ChromiumJniTestB_NativeFunctionBA(JNIEnv* env, jboolean param1,
    jfloat param2);

JNI_GENERATOR_EXPORT jboolean
    Java_org_chromium_base_natives_GEN_1JNI_org_1chromium_1chromiumjnigenerator_1ChromiumJniTestB_1nativeFunctionBA(
    JNIEnv* env,
    jclass jcaller,
    jboolean param1,
    jfloat param2) {
  return JNI_ChromiumJniTestB_NativeFunctionBA(env, param1, param2);
}

static jfloat JNI_ChromiumJniTestB_FunctionBB(JNIEnv* env, const
    chromium::android::JavaParamRef
   & param1);

JNI_GENERATOR_EXPORT jfloat
    
   Java_org_chromium_base_natives_GEN_1JNI_org_1chromium_1chromiumjnigenerator_1ChromiumJniTestB_1functionBB(
    JNIEnv* env,
    jclass jcaller,
    jstring param1) {
  
   return 
   JNI_ChromiumJniTestB_FunctionBB(env, chromium::android::JavaParamRef
   
    (env,
      param1));
}
   
  

Create ChromiumJniTestA.cc and ChromiumJniTestB.cc to implement ChromiumJniTestA.h and ChromiumJniTestB.h

`ChromiumJniTestA.cc`

#include <ChromiumJniTestA.h>

jboolean JNI_ChromiumJniTestA_FunctionAA(JNIEnv *env, jboolean param1,
                                         jfloat param2) {
    return true;
}


jlong JNI_ChromiumJniTestA_FunctionAC(JNIEnv *env, const
chromium::android::JavaParamRef
    &jcaller,
                                      
   const chromium::android::JavaParamRef
   
     &param1) {
    
    return 
    1000.
    f;
}
   
  
`ChromiumJniTestB.cc`

#include <ChromiumJniTestB.h>

jboolean JNI_ChromiumJniTestB_NativeFunctionBA(JNIEnv* env, jboolean param1,
                                                      jfloat param2){
    return true;
}

jfloat JNI_ChromiumJniTestB_FunctionBB(JNIEnv* env, const
chromium::android::JavaParamRef
   & param1){
    
   return 
   3888.
   2f;
}
  

Remember to add the chromium-jni-generator-native-essential directory to the compilation process.

add_library(chromium_jni_generator_test_jni SHARED
        jni_main.cc
        ChromiumJniTestA.cc
        ChromiumJniTestB.cc)
target_include_directories(chromium_jni_generator_test_jni
        PRIVATE ./
        PRIVATE ./gen)

add_subdirectory(../chromium-jni-generator-native-essential chromium_jni_generator_native_essential)

target_link_libraries(chromium_jni_generator_test_jni
        PRIVATE chromium_jni_generator_native_essential)

3 C/C ++ Layer Access to Java Layer

Add @CalledByNative annotations to Java methods

`ChromiumJniTestA.java`

@JNINamespace("test::your::name::space")
public class ChromiumJniTestA {
    ...
    ...

    @CalledByNative
    public static float functionAE(String param1, boolean param2){
        return 9.7f;
    }
}

Generate C/C ++ code through jni_generator.py

g_org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz(nullptr); #ifndef org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz_defined #define org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz_defined inline jclass org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz(JNIEnv* env) { return chromium::android::LazyGetClass(env, kClassPath_org_chromium_chromiumjnigenerator_ChromiumJniTestA, &g_org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz); } #endif static std::atomic g_org_chromium_chromiumjnigenerator_ChromiumJniTestA_functionAE(nullptr); static jfloat Java_ChromiumJniTestA_functionAE(JNIEnv* env, const chromium::android::JavaRef & param1, jboolean param2) { jclass clazz = org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz(env); CHECK_CLAZZ(env, clazz, org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz(env), 0); chromium::android::JniJavaCallContextChecked call_context; call_context.Init< chromium::android::MethodID::TYPE_STATIC>( env, clazz, "functionAE", "(Ljava/lang/String;Z)F", &g_org_chromium_chromiumjnigenerator_ChromiumJniTestA_functionAE); jfloat ret = env->CallStaticFloatMethod(clazz, call_context.base.method_id, param1.obj(), param2); return ret; }">
`ChromiumJniTestA.h`

JNI_REGISTRATION_EXPORT extern const char
    kClassPath_org_chromium_chromiumjnigenerator_ChromiumJniTestA[];
const char kClassPath_org_chromium_chromiumjnigenerator_ChromiumJniTestA[] =
    "org/chromium/chromiumjnigenerator/ChromiumJniTestA";
// Leaking this jclass as we cannot use LazyInstance from some threads.
JNI_REGISTRATION_EXPORT std::atomic
    
     g_org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz(
     nullptr);
#
     ifndef org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz_defined
#
     define 
     org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz_defined

     inline jclass 
     org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz(JNIEnv* env) {
  
     return 
     chromium::android::LazyGetClass(env,
      
     kClassPath_org_chromium_chromiumjnigenerator_ChromiumJniTestA,
      &g_org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz);
}
#
     endif


     static std::atomic
     
    
      g_org_chromium_chromiumjnigenerator_ChromiumJniTestA_functionAE(
      nullptr);

      static jfloat 
      Java_ChromiumJniTestA_functionAE(JNIEnv* env, 
      const
    chromium::android::JavaRef
      
       & param1,
    jboolean param2) {
  jclass clazz = 
       org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz(env);
  
       CHECK_CLAZZ(env, clazz,
      
       org_chromium_chromiumjnigenerator_ChromiumJniTestA_clazz(env), 
       0);

  chromium::android::JniJavaCallContextChecked call_context;
  call_context.
       Init<
      chromium::android::MethodID::TYPE_STATIC>(
          env,
          clazz,
          
       "functionAE",
          
       "(Ljava/lang/String;Z)F",
          &g_org_chromium_chromiumjnigenerator_ChromiumJniTestA_functionAE);

  jfloat ret =
      env->
       CallStaticFloatMethod(clazz,
          call_context.
       base.
       method_id, param1.
       obj(), param2);
  
       return ret;
}
      
     
    
You might also like...
VG-Scraper is a python program using the module called BeautifulSoup which allows anyone to scrape something off an website. This program lets you put in a number trough an input and a number is 1 news article.

VG-Scraper VG-Scraper is a convinient program where you can find all the news articles instead of finding one yourself. Installing [Linux] Open a term

An experiment to deploy a serverless infrastructure for a scrapy project.
An experiment to deploy a serverless infrastructure for a scrapy project.

Serverless Scrapy project This project aims to evaluate the feasibility of an architecture based on serverless technology for a web crawler using scra

Python Web Scrapper Project

Web Scrapper Projeto desenvolvido em python, sobre tudo com Selenium, BeautifulSoup e Pandas é um web scrapper que puxa uma tabela com as principais e

A web scraping pipeline project that retrieves TV and movie data from two sources, then transforms and stores data in a MySQL database.
A web scraping pipeline project that retrieves TV and movie data from two sources, then transforms and stores data in a MySQL database.

New to Streaming Scraper An in-progress web scraping project built with Python, R, and SQL. The scraped data are movie and TV show information. The go

This is a sport analytics project that combines the knowledge of OOP and Webscraping
This is a sport analytics project that combines the knowledge of OOP and Webscraping

This is a sport analytics project that combines the knowledge of Object Oriented Programming (OOP) and Webscraping, the weekly scraping of the English Premier league table is carried out to assess the performance of each club from the beginning of the season to the end.

Rottentomatoes, Goodreads and IMDB sites crawler. Semantic Web final project.

Crawler Rottentomatoes, Goodreads and IMDB sites crawler. Crawler written by beautifulsoup, selenium and lxml to gather books and films information an

This project was created using Python technology and flask tools to scrape a music site

python-scrapping This project was created using Python technology and flask tools to scrape a music site You need to install the following packages to

Instagram_scrapper - This project allow you to scrape the list of followers, following or both from a public Instagram account, and create a csv or excel file easily.

Instagram_scrapper This project allow you to scrape the list of followers, following or both from a public Instagram account, and create a csv or exce

Bigdata - This Scrapy project uses Redis and Kafka to create a distributed on demand scraping cluster

Scrapy Cluster This Scrapy project uses Redis and Kafka to create a distributed

Releases(v1.0.0)
Owner
allenxuan
allenxuan
Simply scrape / download all the media from an fansly account.

Simply scrape / download all the media from an fansly account. Providing updates as long as its continuously gaining popularity, so hit the ⭐ button!

Mika C. 334 Jan 01, 2023
Scrapes the Sun Life of Canada Philippines web site for historical prices of their investment funds and then saves them as CSV files.

slocpi-scraper Sun Life of Canada Philippines Inc Investment Funds Scraper Install dependencies pip install -r requirements.txt Usage General format:

Daryl Yu 2 Jan 07, 2022
A web crawler script that crawls the target website and lists its links

A web crawler script that crawls the target website and lists its links || A web crawler script that lists links by scanning the target website.

2 Apr 29, 2022
A training task for web scraping using python multithreading and a real-time-updated list of available proxy servers.

Parallel web scraping The project is a training task for web scraping using python multithreading and a real-time-updated list of available proxy serv

Kushal Shingote 1 Feb 10, 2022
Python web scrapper

Website scrapper Web scrapping project in Python. Created for learning purposes. Start Install python Update configuration with websites Launch script

Nogueira Vitor 1 Dec 19, 2021
抢京东茅台脚本,定时自动触发,自动预约,自动停止

jd_maotai 抢京东茅台脚本,定时自动触发,自动预约,自动停止 小白信用 99.6,暂时还没抢到过,朋友 80 多抢到了一瓶,所以我感觉是跟信用分没啥关系,完全是看运气的。

Aruelius.L 117 Dec 22, 2022
A tool for scraping and organizing data from NewsBank API searches

nbscraper Overview This simple tool automates the process of copying, pasting, and organizing data from NewsBank API searches. Curerntly, nbscrape onl

0 Jun 17, 2021
一款利用Python来自动获取QQ音乐上某个歌手所有歌曲歌词的爬虫软件

QQ音乐歌词爬虫 一款利用Python来自动获取QQ音乐上某个歌手所有歌曲歌词的爬虫软件,默认去除了所有演唱会(Live)版本的歌曲。 使用方法 直接运行python run.py即可,然后输入你想获取的歌手名字,然后静静等待片刻。 output目录下保存生成的歌词和歌名文件。以周杰伦为例,会生成两

Yang Wei 11 Jul 27, 2022
京东茅台抢购

截止 2021/2/1 日,该项目已无法使用! 京东:约满即止,仅限京东实名认证用户APP端抢购,2月1日10:00开始预约,2月1日12:00开始抢购(京东APP需升级至8.5.6版本及以上) 写在前面 本项目来自 huanghyw - jd_seckill,作者的项目地址我找不到了,找到了再贴上

abee 73 Dec 03, 2022
Web-Scraping using Selenium Master

Web-Scraping using Selenium What is the need of Selenium? Some websites don't like to be scrapped and in that case you need to disguise your webscrapi

Md Rashidul Islam 1 Oct 26, 2021
Danbooru scraper with python

Danbooru Version: 0.0.1 License under: MIT License Dependencies Python: = 3.9.7 beautifulsoup4 cloudscraper Example of use Danbooru from danbooru imp

Sugarbell 2 Oct 27, 2022
12306抢票脚本

12306抢票脚本

罐子里的茶 457 Jan 05, 2023
Web-scraping - A bot using Python with BeautifulSoup that scraps IRS website by form number and returns the results as json

Web-scraping - A bot using Python with BeautifulSoup that scraps IRS website (prior form publication) by form number and returns the results as json. It provides the option to download pdfs over a ra

1 Jan 04, 2022
Automated Linkedin bot that will improve your visibility and increase your network.

LinkedinSpider LinkedinSpider is a small project using browser automating to increase your visibility and network of connections on Linkedin. DISCLAIM

Frederik 2 Nov 26, 2021
Scraping followers of an instagram account

ScrapInsta A script to scraping data from Instagram Install First of all you can run: pip install scrapinsta After that you need to install these requ

Matheus Kolln 1 Sep 05, 2021
A Powerful Spider(Web Crawler) System in Python.

pyspider A Powerful Spider(Web Crawler) System in Python. Write script in Python Powerful WebUI with script editor, task monitor, project manager and

Roy Binux 15.7k Jan 04, 2023
Minecraft Item Scraper

Minecraft Item Scraper To run, first ensure you have the BeautifulSoup module: pip install bs4 Then run, python minecraft_items.py folder-to-save-ima

Jaedan Calder 1 Dec 29, 2021
Scraping web pages to get data

Scraping Data Get public data and save in database This is project use Python How to run a project 1 - Clone the repository 2 - Install beautifulsoup4

Soccer Project 2 Nov 01, 2021
Works very well and you can ask for the type of image you want the scrapper to collect.

Works very well and you can ask for the type of image you want the scrapper to collect. Also follows a specific urls path depending on keyword selection.

Memo Sim 1 Feb 17, 2022
Simple proxy scraper made by using ProxyScrape's api.

What is Moon? Moon is a lightweight and fast proxy scraper made by using ProxyScrape's api. What can i do with this? You can use proxies for varietys

1 Jul 04, 2022