总结一下:

android调用 android 调用JNI 分为静态调用与动态调用(不论动态还是静态前提都是NDK环境已经配置好的前提下)文章底部附上DEMO

一、静态主要就是将c(.c)或者c++(cpp)的源文件直接加到项目中进行调用,

然后在CMakeLists.txt中进行配置。

二、动态调用

1、动态调用使用已经编译好的动态库.so文件

2、android调用ndk类

生成后的so文件

public class SerialPort {p*/public static native int GetSOVer(String ar);static {System.loadLibrary("serialport");//初始化so库(注意这里添加是需要去掉lib与.so)}
}

3、.c文件添加

/** 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 "AS IS" 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 <jni.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#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)//测试  Java_(固定)_com_littt_util_SerialPort(android包名原来的.更改为_,string ar 传入的字符串参数,JNIEnv *env, jclass固定写法)
JNIEXPORT jint JNICALL
Java_com_littt_util_SerialPort_GetSOVer(JNIEnv *env, jclass clazz, jstring ar) {// TODO: implement GetSOVer()return 9;//返回一个9的值
}

4、.h头文件中声明

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class android_serialport_api_SerialPort */#ifndef _Included_android_serialport_api_SerialPort
#define _Included_android_serialport_api_SerialPort
#ifdef __cplusplus
extern "C" {
#endifJNIEXPORT jint JNICALL
Java_com_littt_util_SerialPort_GetSOVer(JNIEnv *env, jclass clazz,jstring v);#ifdef __cplusplus
}
#endif
#endif

5、头文件与c文件写好了,就需要在CMake 中添加.c与.h都需要添加

# 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.cM1900_drv.haudio_i2s.clinux_so.cppmixer.cinclude/tinyalsa/asoundlib.hinclude/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、在build.gradle同样需要配置

plugins {id 'com.android.application'
}android {compileSdkVersion 28buildToolsVersion "28.0.3"defaultConfig {applicationId "com.littt.interphone"minSdkVersion 17targetSdkVersion 28versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"// cmakeexternalNativeBuild {cmake {cppFlags ""abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'}}}externalNativeBuild {cmake {path "src/main/cpp/CMakeLists.txt"version "3.10.2"}}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}sourceSets {main {jniLibs.srcDirs = ['libs']}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility 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、如果静态调用可以成功,那么就可以生成动态so库文件

点击图中锤子会进行编译。完成后可以打开如下路径查看

红框中生成后 so文件

7.1、生成的.so文件(工程文件夹模式)目录为app/build/intermediates/ndk/lib,将其复制到另一个工程的app/lib目录下。

7.2、要使用上述的.so文件 ,必须将工程的包名改为生成.so文件时的包名,要不然 编译能通过,但是app不能正常运行。logcat会提示找不到所调用函数的实现。

7.3、将so文件复制到需要的路径下。
7.4、在gradle.properties中最后加一行:android.useDeprecatedNdk=true。

demo下载

-END

android 调用JNI SO动态库相关推荐

  1. android编译boost,使用android ndk编译boost动态库

    由于以往我写过不少使用boost库开发的项目,而最近准备移植一些代码到android上(上层界面以及jni层我不管,也没研究过,现在只完成代码的移植编译,具体如何调用,由其它人负责),所以先要解决的就 ...

  2. mac java jni_Mac OS上编译JNI的动态库

    JNI是Java Native Interface的缩写,是为了在Java上能够调用本地语言尤其是C和c++设计的. 下面就看下如何在Mac OS上面如何使用JNI的调用C语言. 编写Java类 先定 ...

  3. Android调用JNI的实现方法

    目录 概述 调用JNI接口的方法 后记 概述 Android调用JNI库大致包括两种情况: 提供Java接口和so库: 这种类型的调用比较简单,要做的只是把so库放到APK或者Android系统中,之 ...

  4. JAVA如何调用C/C++动态库

    一.调用方式: JAVA调用C/C++动态库有很多方法,常用的有JNI(Java Native Interface).JNA(Java Native Access). JNI:早在JAVA1.1版本就 ...

  5. 【Android 逆向】修改运行中的 Android 进程的内存数据 ( 运行环境搭建 Android 模拟器安装 | 拷贝 Android 平台可执行文件和动态库到 /data/system )

    文章目录 一.运行环境搭建 Android 模拟器安装 二.拷贝 Android 平台可执行文件和动态库到 /data/system 目录下 一.运行环境搭建 Android 模拟器安装 使用低版本的 ...

  6. Rust应用调用C语言动态库

    外部功能接口FFI 虽然高级(脚本)编程语言的功能丰富,表达能力强,但对底层的一些特殊操作的支持并不完善,就需要以其他编程语言来实现.调用其他编程语言的接口,被称为Foreign Function I ...

  7. QT 调用Bartender C#动态库接口

    一.制作可供程序调用的条码标签 ********** 测试使用Bartender 软件版本信息 ********* 1.条码设置 (1)打开bartender建立一个模板文件 (2)标签界面布局 a. ...

  8. Android调用JNI本地方法跟踪目标代码

    正如Android调用JNI本地方法经过有点改变章所说跟踪代码是可行的,但是跟踪某些代码会出现anr,点击取消,还是不好运,有提高办法吗?回答是有(gdb还没试过,本文只讨论ida). 下面是我使用  ...

  9. C#调用C/C++动态库dll异常:对 PInvoke 函数调用导致堆栈不对称问题

    结论:如果你是用C#调用C的动态库,如果出现"对 PInvoke 函数调用导致堆栈不对称问题",建议优先调整CallingConvention的值,建议改为CallingConve ...

最新文章

  1. 【EXLIBRIS】随笔记 011
  2. 如何在 ASP.Net Core 使用 内存缓存
  3. openssl版本信息和支持的命令
  4. Oracle数据库配置方案,oracle数据库各项参数参考配置方案
  5. Kali Linux 网络扫描秘籍 第七章 Web 应用扫描(三)
  6. 投放屏幕upnp协议探究抓包
  7. 02-linux安装nodejs
  8. 【小型JavaFx项目】文字小冒险游戏
  9. 自制计算机语言,3个步骤实现简单语言解释器(自制简易编程语言)
  10. 数据库系统概论第五版(第 5 章数据库完整性)习题答案
  11. 防火墙如何打开和关闭某个端口
  12. ★【博弈论】【贝蒂定理】取棋子游戏
  13. MATLAB-SIMULINK-二极管搭建整流电路(1)
  14. winxp网络找不到计算机,WinXP系统电脑打开WiFi搜索不到无线网络的解决方法
  15. PNP三极管和NPN三极管的开关电路(EC极性接线判断简单明了)简单的技巧:三极管上箭头所在方向的二极管,只要二极管正向导通,那么三极管上下就能导通。
  16. react项目中播放音频时扬声器图标动画效果
  17. [USACO18JAN] Lifeguards S
  18. 将远程仓库的项目克隆到本地
  19. 11月24日学习笔记_map/reduct的应用于使用
  20. (附源码)计算机毕业设计ssm爱音乐网站

热门文章

  1. 跨境电商注册义乌个体户结汇怎么开户
  2. php测试网络连接,电脑怎么测网速
  3. 2021年全球与中国集成抽油烟机行业市场规模及发展前景分析
  4. Temporal Convolutional Network with Frequency Dimension AdaptiveAttention for Speech Enhancement
  5. (更新时间)2021年6月3日 商城高并发秒杀系统(.NET Core版) 26-性能优化-nginx负载均衡优化
  6. 【排版软件系列】 LaTeX文档类 Beamer
  7. TMS320F280025的时钟
  8. 在深圳南山科技园的两年
  9. micropython stm32f401_在STM32F401RE(小钢炮CANNON开发板)移植MicroPython
  10. Android知识点 405 -- Dropbox