Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method for android to call the JNI SO dynamic library

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the method of android calling JNI SO dynamic library". In daily operation, I believe that many people have doubts about what the method of android calling JNI SO dynamic library is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of android calling JNI SO dynamic library"! Next, please follow the editor to study!

First, static is mainly c (.c) or C++ (cpp) source files directly added to the project to call, and then configured in CMakeLists.txt. Second, dynamic call

1. Dynamic invocation uses the compiled dynamic library .so file

2. Android calls the ndk class

Generated so file

Public class SerialPort {p*/public static native int GetSOVer (String ar); static {System.loadLibrary ("serialport"); / / initialize the so library (note that lib and .so need to be removed when added here)}}

3. C files add

/ * * Copyright 2009-2011 Cedric Priscal * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "ASIS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * / # include # include "M1900_drv.h" # include "SerialPort.h" # include "include/tinyalsa/audio_i2s.h" # include "include/tinyalsa/asoundlib.h" # include "android/log.h" # include "newland_linux_so.h" static const char * TAG = "serial_port" # define LOGI (fmt, args...) _ android_log_print (ANDROID_LOG_INFO, TAG, fmt, # # args) # define LOGD (fmt, args...) _ android_log_print (ANDROID_LOG_DEBUG, TAG, fmt, # # args) # define LOGE (fmt, args...) _ android_log_print (ANDROID_LOG_ERROR, TAG, fmt # # args) / / Test Java_ (fixed) _ com_littt_util_SerialPort (original android package name. Change to _, the string parameter passed in by string ar, JNIEnv * env, jclass fixed spelling) JNIEXPORT jint JNICALL Java_com_littt_util_SerialPort_GetSOVer (JNIEnv * env, jclass clazz, jstring ar) {/ / TODO: implement GetSOVer () return 9 / return a value of 9}

4. Declaration in the .h header file

/ * DO NOT EDIT THIS FILE-it is machine generated * / # include / * Header for class android_serialport_api_SerialPort * / # ifndef _ Included_android_serialport_api_SerialPort#define _ Included_android_serialport_api_SerialPort#ifdef _ cplusplusextern "C" {# endifJNIEXPORT jint JNICALLJava_com_littt_util_SerialPort_GetSOVer (JNIEnv * env, jclass clazz,jstring v); # ifdef _ cplusplus} # endif#endif

5. Once the header file and c file are written, you need to add .c and .h to CMake.

# For more information about using CMake with Android Studio, read the# documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. Cmake_minimum_required (VERSION 3.4.1) # Creates and names a library, sets it as either STATIC# or SHARED, and provides the relative paths to its source code.# You can define multiple libraries, and CMake builds them for you.# Gradle automatically packages shared libraries with your APK. Add_library (# Sets the name of the library. Serialport # Sets the library as a shared library. SHARED # Provides a relative path to your source file (s). M1900_drv.c M1900_drv.h audio_i2s.c linux_so.cpp mixer.c include/tinyalsa/asoundlib.h include/tinyalsa/audio_i2s.h) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by# default, you only need to specify the name of the public NDK library# you want to add. CMake verifies that the library exists before# completing its build. Find_library (# Sets the name of the path variable. Log-lib # Specifies the name of the NDK library that # you want CMake to locate. Log) # Specifies libraries CMake should link to your target library. You# can link multiple libraries, such as libraries you define in this# build script, prebuilt third-party libraries, or system libraries. Target_link_libraries (# Specifies the target library. Serialport # Links the target library to the log library # included in the NDK. ${log-lib}) set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}-s") set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}-s")

6. In build.gradle, you also need to configure

Plugins {id 'com.android.application'} android {compileSdkVersion 28 buildToolsVersion "28.0.3" defaultConfig {applicationId "com.littt.interphone" minSdkVersion 17 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" / / cmake externalNativeBuild {cmake {cppFlags "" abiFilters' armeabi-v7a' 'arm64-v8a',' x8634'} externalNativeBuild {cmake {path "src/main/cpp/CMakeLists.txt" version "3.10.2"}} buildTypes {release {minifyEnabled false proguardFiles getDefaultProguardFile ('proguard-android-optimize.txt') 'proguard-rules.pro'}} sourceSets {main {jniLibs.srcDirs = [' libs']}} compileOptions {sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8} ndkVersion '22.1.7171670'} dependencies {/ / implementation' androidx.appcompat:appcompat:1.2.0'// implementation 'com.google. Android.material:material:1.2.1'}

7. If the static call is successful, then the dynamic so library file can be generated

Click the hammer in the figure to compile.

Generated so file in red box

7.1. the generated .so file (project folder mode) directory is app/build/intermediates/ndk/lib, and copy it to the app/lib directory of another project.

7.2. to use the above .so file, you must change the package name of the project to the package name when the .so file was generated, otherwise the compilation will pass, but app will not work properly. Logcat prompts that the implementation of the called function cannot be found.

Copy the so file to the desired path.

7.4. Add the last line in the gradle.properties: android.useDeprecatedNdk=true.

At this point, the study on "what is the method of android calling JNI SO dynamic library" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report