CN111966427B - Implementation method for calling Java API (Java application program interface) by Android native - Google Patents

Implementation method for calling Java API (Java application program interface) by Android native Download PDF

Info

Publication number
CN111966427B
CN111966427B CN202010825679.3A CN202010825679A CN111966427B CN 111966427 B CN111966427 B CN 111966427B CN 202010825679 A CN202010825679 A CN 202010825679A CN 111966427 B CN111966427 B CN 111966427B
Authority
CN
China
Prior art keywords
jni
layer
java
api
native
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202010825679.3A
Other languages
Chinese (zh)
Other versions
CN111966427A (en
Inventor
曾亮
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Sichuan Changhong Electric Co Ltd
Original Assignee
Sichuan Changhong Electric Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Sichuan Changhong Electric Co Ltd filed Critical Sichuan Changhong Electric Co Ltd
Priority to CN202010825679.3A priority Critical patent/CN111966427B/en
Publication of CN111966427A publication Critical patent/CN111966427A/en
Application granted granted Critical
Publication of CN111966427B publication Critical patent/CN111966427B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/448Execution paradigms, e.g. implementations of programming paradigms
    • G06F9/4488Object-oriented
    • G06F9/449Object-oriented method invocation or resolution
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/545Interprogram communication where tasks reside in different layers, e.g. user- and kernel-space

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Telephone Function (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The invention discloses a method for realizing calling Java API by Android native, belonging to the technical field of computer application. The invention comprises the following steps: the Java layer calls JNI to provide an independent registration interface, and the independent registration interface stores JNI environment variables corresponding to Java layer API when the Java layer calls and calls a registration interface of a native layer in the independent registration interface; the JNI provides JNI callback functions to a native layer for registration; when a native layer program runs, calling a JNI callback function at a specific moment in the program; and calling the previously stored JNI environment variable in a JNI callback function provided by the JNI to the native layer to finish the calling of the Java layer API by the JNI. The method and the device enable the registration and callback processes of calling Java API by Android native to be completely separated, and local programs can autonomously master the callback opportunity, so that the method and the device are more practical in embedded application.

Description

Implementation method for calling Java API (Java application program interface) by Android native
Technical Field
The invention relates to the technical field of computer application, in particular to a method for realizing Java API call by Android native.
Background
The Android operating system is a Linux-based operating system and is mainly used for portable equipment. The upper layer application in the Android system is generally developed and completed based on Java, but when some services relate to local services or hardware and the like, C needs to be used for realization, and C programs run in a native layer.
JNI (Java Native Interface; Java Native Interface) provides a mechanism for Java layer and Native layer to call each other.
However, in the calling mode of the native layer to the Java layer API in the prior art, two parts are often required to be repeatedly called through JNI, or the calling of the native layer to the Java layer API needs to be triggered through the calling of the Java layer API, so that the efficiency is low, the problems of memory leakage or program crash and the like are easily caused, the independence between two modules is also reduced, and the Java layer API needs to be actively called at some specific time in the embedded development, and such solutions are not mentioned in the previous patents.
Disclosure of Invention
The invention provides a method for realizing calling of Java API by Android native, aiming at solving the technical problem.
In order to achieve the purpose, the invention adopts the following technical scheme:
a realization method for calling Java API by Android native includes the following steps:
the Java layer calls JNI to provide an independent registration interface, and when the Java layer calls the JNI, the independent registration interface stores JNI environment variables corresponding to the API of the Java layer and calls a registration interface of a native layer in the independent registration interface;
the JNI provides JNI callback functions to a native layer for registration; thus, completing the registration process from the Java layer to the native layer;
when a native layer program runs, calling a JNI callback function at a specific moment in the program;
and calling the previously stored JNI environment variable in a JNI callback function provided by the JNI to the native layer to finish the calling of the Java layer API by the JNI, so as to finish the callback from the native layer to the Java layer.
Furthermore, the JNI provides an independent registration interface, and comprises a calling interface provided by the JNI for a Java layer, the calling interface has no additional parameter, and the JNI takes the environment variable corresponding to the API of the Java layer through the default parameter of the calling interface.
Furthermore, the JNI environment variables corresponding to the Java layer API are a plurality of parameters in the JNI for marking the Java layer API.
Further, the JNI environment variables corresponding to the Java layer API are three parameters used for marking the Java layer API in the JNI, which are: java VM, JOBject object, Java API's MethodeID.
Further, the registration interface of the native layer refers to a registration interface which actually needs to register a JNI callback function in the native layer, and the registration interface needs a JNI callback function pointer as a parameter.
Furthermore, the JNI provides a JNI callback function which is a JNI callback function really registered to the native layer, and the JNI callback function calls the Java layer API by using the environment variable of the Java layer API stored during registration so as to complete the callback of the Java layer API from the native layer.
Further, the specific time is a condition that the embedded device triggers a callback in the whole program running process.
Compared with the prior art, the invention has the beneficial effects that:
according to the method, the relevant Java layer API is triggered and called through ZigBee equipment of the native layer in combination with the actual situation of embedded development, the registration and the callback are separated, and programs of the native layer and the Java layer are independent, so that the native layer can actively call back the Java layer API according to the progress of the programs.
Drawings
Fig. 1 is a flowchart in example 1 of the present application.
Fig. 2 is a flowchart of embodiment 2 of the present application.
Detailed Description
The present invention will be further described with reference to the following examples, which are intended to illustrate only some, but not all, of the embodiments of the present invention. Based on the embodiments of the present invention, other embodiments used by those skilled in the art without any creative effort belong to the protection scope of the present invention.
Example 1:
as shown in fig. 1, an implementation method for calling Java API by Android native includes the following steps:
s1, providing an independent registration interface for calling a Java layer by the JNI, storing JNI environment variables corresponding to a Java layer API when the Java layer is called by the independent registration interface, calling a registration interface of a native layer in the independent registration interface, and performing step S2;
s2, providing a JNI callback function by the JNI to register the native layer, realizing the butt joint of the native layer and the JNI, ensuring the independence of a local program, and completing the registration process from the Java layer to the native layer;
s3, when the native layer program runs, calling JNI callback functions at a specific moment in the program, and performing step S4;
and S4, calling the previously stored JNI environment variable in a JNI callback function provided by the JNI to the native layer to finish calling the Java layer API by the JNI, so as to finish the callback from the native layer to the Java layer.
The JNI provides an independent registration interface, namely a calling interface provided by the JNI part for a Java layer, the calling interface has no additional parameter, but the JNI can take the environment variable corresponding to the API of the Java layer through the default parameter of the calling interface, so that the Java part can control the initiative of the registration function, and the running safety of the program is ensured by calling back after registration.
In this embodiment, the environment variables are multiple parameters used for marking a Java layer API in the JNI, and preferably, the JNI may locate the API of the Java environment by using three parameters; respectively as follows: java VM, JOBject object, Java API's MethodeID.
The registration interface of the native layer is a registration interface which actually needs to register a JNI callback function on the native layer, the registration interface needs a JNI callback function pointer as a parameter, and the Java layer does not have the concept of the JNI callback function pointer, so that the whole callback function registration from the Java layer to the native layer is completed by adding registration step by taking the JNI as a middle part.
The JNI callback function provided by the JNI is a JNI callback function which is really registered to a native layer, the JNI callback function used by the native layer is the function, the JNI callback function calls a Java layer API by using the environment variable of the Java layer API which is saved during registration, and then the callback of the Java layer API from the native layer is completed.
The specific moment is the condition that the embedded device triggers callback in the whole program running process, and generally, the conditions include online, offline, status report and the like.
Example 2:
fig. 2 shows a flowchart of an implementation method for calling Java API by Android native according to the present invention, in this embodiment, registration and callback are separated, and callback to Java API is triggered by an on-line device message received at the bottom layer, as shown in fig. 2, the method includes the following steps:
s100, calling a local interface packaged by the JNI by the Java layer during running, providing a registration function from the Java layer to the JNI part, and storing related information such as environment variables of a JNI callback function;
s200, registering a native layer registration interface through a JNI callback function provided by a JNI layer to realize the butt joint of the native layer and the JNI, and actively calling the JNI callback function when the equipment is on line;
s300, when a native layer runs and equipment online information is processed, actively calling a JNI callback function to realize active callback from the native layer to the JNI;
s400, when the JNI callback function of the JNI is called, the JNI environment variables stored during registration can be used in the function, and the online JNI callback function of the Java layer can be called back through the environment variables, so that the active callback from a native layer to a Java layer API is achieved.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents and improvements made within the spirit and principle of the present invention are intended to be included within the scope of the present invention.

Claims (6)

1. An implementation method for calling Java API by Android native is characterized by comprising the following steps:
the Java layer calls JNI to provide an independent registration interface, and when the Java layer calls the JNI, the independent registration interface stores JNI environment variables corresponding to the API of the Java layer and calls a registration interface of a native layer in the independent registration interface;
the JNI provides JNI callback functions to a native layer for registration; thus, completing the registration process from the Java layer to the native layer;
when a native layer program runs, calling a JNI callback function at a specific moment in the program;
the JNI environment variable saved before being called in a JNI callback function provided by the JNI to the native layer is called, the calling of the JNI to a Java layer API is completed, and the callback from the native layer to the Java layer is completed; the JNI provides an independent registration interface and comprises a calling interface provided by the JNI for a Java layer, the calling interface has no additional parameter, and the JNI takes the environment variable corresponding to the API of the Java layer through the default parameter of the calling interface.
2. The method for realizing Android native calling Java API as claimed in claim 1, wherein JNI environment variables corresponding to the Java layer API are a plurality of parameters in JNI for marking the Java layer API.
3. The method for realizing Android native calling Java API as claimed in claim 2, wherein JNI environment variables corresponding to the Java layer API are three parameters in JNI for marking the Java layer API, which are respectively: JavaVM, jobject object, MethodID of Java API.
4. The method for implementing the Android native calling Java API as claimed in claim 1, wherein the registration interface of the native layer is a registration interface which actually needs to register a JNI callback function on the native layer, and the registration interface needs a JNI callback function pointer as a parameter.
5. The method for realizing Android native calling Java API as claimed in claim 1, wherein the JNI provides JNI callback function as JNI callback function really registered to native layer, the JNI callback function uses saved environment variables of Java layer API when registering to call Java layer API, and then finishes callback to Java layer API from native layer.
6. The method for implementing the Android native calling Java API, according to claim 1, wherein the specific time is a condition that the embedded device triggers a callback in the whole program running process.
CN202010825679.3A 2020-08-17 2020-08-17 Implementation method for calling Java API (Java application program interface) by Android native Active CN111966427B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010825679.3A CN111966427B (en) 2020-08-17 2020-08-17 Implementation method for calling Java API (Java application program interface) by Android native

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010825679.3A CN111966427B (en) 2020-08-17 2020-08-17 Implementation method for calling Java API (Java application program interface) by Android native

Publications (2)

Publication Number Publication Date
CN111966427A CN111966427A (en) 2020-11-20
CN111966427B true CN111966427B (en) 2022-06-21

Family

ID=73389061

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010825679.3A Active CN111966427B (en) 2020-08-17 2020-08-17 Implementation method for calling Java API (Java application program interface) by Android native

Country Status (1)

Country Link
CN (1) CN111966427B (en)

Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2006110829A2 (en) * 2005-04-08 2006-10-19 The Mathworks, Inc. System and method for using an rmi activation system daemon with non-java applications
CN104035809A (en) * 2014-04-29 2014-09-10 哈尔滨工程大学 Method for optimizing performance of interface functions in JNI mechanism based on Dalvik virtual machine
CN104236579A (en) * 2014-10-09 2014-12-24 武汉大学 High-precision satellite navigation implementing method based on Android kernel layer
CN104598809A (en) * 2015-02-13 2015-05-06 北京奇虎科技有限公司 Program monitoring method and defending method thereof, as well as relevant device
CN104778075A (en) * 2015-04-03 2015-07-15 北京奇虎科技有限公司 Method and device for calling Java layer API (Application Program Interface) by native layer in Android system
CN105743955A (en) * 2014-12-11 2016-07-06 中国科学院声学研究所 Method for JavaScript object expansion
CN106502668A (en) * 2016-10-20 2017-03-15 武汉斗鱼网络科技有限公司 A kind of interface package method for realizing Android JNI reflections and system
CN108763060A (en) * 2018-04-27 2018-11-06 广州华多网络科技有限公司 Native layers of collapse source tracing method, device, storage medium and terminal in android system

Family Cites Families (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105589701B (en) * 2014-10-21 2019-03-08 阿里巴巴集团控股有限公司 A kind of client application local updating method and device
CN106355049B (en) * 2016-08-19 2020-02-11 北京奇虎科技有限公司 Method and device for reinforcing SO file of dynamic link library of android installation package
CN110309630B (en) * 2019-06-28 2023-05-30 南京冰鉴信息科技有限公司 Java code encryption method and device
CN111400757B (en) * 2020-03-13 2022-09-27 西安电子科技大学 Method for preventing native code in android third-party library from revealing user privacy

Patent Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2006110829A2 (en) * 2005-04-08 2006-10-19 The Mathworks, Inc. System and method for using an rmi activation system daemon with non-java applications
CN104035809A (en) * 2014-04-29 2014-09-10 哈尔滨工程大学 Method for optimizing performance of interface functions in JNI mechanism based on Dalvik virtual machine
CN104236579A (en) * 2014-10-09 2014-12-24 武汉大学 High-precision satellite navigation implementing method based on Android kernel layer
CN105743955A (en) * 2014-12-11 2016-07-06 中国科学院声学研究所 Method for JavaScript object expansion
CN104598809A (en) * 2015-02-13 2015-05-06 北京奇虎科技有限公司 Program monitoring method and defending method thereof, as well as relevant device
CN104778075A (en) * 2015-04-03 2015-07-15 北京奇虎科技有限公司 Method and device for calling Java layer API (Application Program Interface) by native layer in Android system
CN106502668A (en) * 2016-10-20 2017-03-15 武汉斗鱼网络科技有限公司 A kind of interface package method for realizing Android JNI reflections and system
CN108763060A (en) * 2018-04-27 2018-11-06 广州华多网络科技有限公司 Native layers of collapse source tracing method, device, storage medium and terminal in android system

Non-Patent Citations (3)

* Cited by examiner, † Cited by third party
Title
Android JNI学习(三)——Java与Native相互调用;隔壁李老头;《https://www.jianshu.com/p/b71aeb4ed13d》;20180509;第1-9页 *
Benchmarking Java application using JNI and native C application on Android;Yeong-Jun Kim 等;《The 2012 12th International Conference on Control, Automation and Systems》;20121231;第284-288页 *
JNI技术实现跨平台开发的研究;任俊伟 等;《计算机应用研究》;20050710(第7期);第180-184页 *

Also Published As

Publication number Publication date
CN111966427A (en) 2020-11-20

Similar Documents

Publication Publication Date Title
US8490070B2 (en) Unified mobile platform
US10705850B2 (en) Stack frame unwinding for exception handling
TWI496081B (en) Isa bridging with callback
CN113467980B (en) Log output method, device and system and embedded equipment
CN114610404B (en) Component calling method and device based on application framework and computer equipment
CN108664343B (en) State calling method and device for micro-service
CN111786823A (en) Fault simulation method and device based on distributed service
CN111966427B (en) Implementation method for calling Java API (Java application program interface) by Android native
WO2020006911A1 (en) Hybrid app interface calling method and device, and computer-readable storage medium
WO2021098257A1 (en) Service processing method based on heterogeneous computing platform
CN107438097B (en) Network request processing method and device
US5724585A (en) Method for processing an application termination initiated from a pre-initialized computer language execution environment
EP0417916B1 (en) Procedure state descriptor system for digital data processors
CN106446671A (en) Method for intercepting dynamic link library injection
CN115994004A (en) Application program interface calling method and device
US20100031256A1 (en) Method for establishing virtual operating system
US20030208605A1 (en) System and method of communication between java components in different namespaces
WO2013039800A1 (en) Simulation of static members and parameterized constructors on an interface-based api
CN113805911A (en) Multi-version program management method, device and system of server side
CN113778458B (en) Data processor function development system, method and computing device
CN113992941B (en) Cloud edge collaborative video analysis system and method based on server-free function computing
CN107463438A (en) Information processing method, device and system for more Openstack environment
CN112947897A (en) Cross-platform API (application program interface) sharing method, device and system and storage medium
CN115964103A (en) TEE specification-based remote function calling method in security application
CN117971435A (en) Task execution method, device, electronic equipment and computer readable storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant