前言
记录 NDK交叉编译及so库导入Android项目 的简单步骤,以备后续用到时查看。

环境
在Linux和Mac环境下,分别编译输出so库。

Red Hat Enterprise Linux 8 64 位 使用GCC编译(也可以用CLANG,这里演示用GCC)

macOS Big Sur 11.3.1 使用CLANG编译(也可以用GCC,这里演示用CLANG)

下载NDK
这里只演示下载NDK17,项目中Mac用到的NDK版本为NDK21

下载NDK

wget https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip

NDK18及之后的NDK版本,建议使用CLANG编译。

解压NDK

unzip android-ndk-r17c-linux-x86_64.zip

解压后得到android-ndk-r17c文件夹

编写头文件及c文件
GCC编译

#include "get.h"int get(){return 666;
}
#include "get.h"int get(){return 666;
}

CLANG编译

#include <stdio.h>int hi();
#include "hi.h"int hi(){return 888;
}

配置NDK
Linux(使用GCC编译)
编辑Home/用户 目录下的.bashrc

vim /home/wangyz/.bashrc

添加以下内容

# 配置NDK的目录
export NDK_HOME=/home/wangyz/NDK/android-ndk-r17c
# 将NDK目录加入PATH中
export PATH=$PATH:$NDK_HOME# x86 CPU架构的gcc
export NDK_GCC_x86=$NDK_HOME/toolchains/x86-4.9/prebuilt/linux-x86_64/bin/i686-linux-android-gcc# x86_64 CPU架构的gcc
export NDK_GCC_x64=$NDK_HOME/toolchains/x86_64-4.9/prebuilt/linux-x86_64/bin/x86_64-linux-android-gcc# ARM CPU架构的gcc
export NDK_GCC_ARM=$NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc# ARM64 CPU架构的gcc
export NDK_GCC_ARM_64=$NDK_HOME/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc# x86 CPU架构 配置sysroot,isystem,否则会找不到头文件
export NDK_GCC_CONFIG_x86="--sysroot=$NDK_HOME/platforms/android-21/arch-x86 -isystem $NDK_HOME/sysroot/usr/include -isystem $NDK_HOME/sysroot/usr/include/i686-linux-android"# x86_64 CPU架构 配置sysroot,isystem,否则会找不到头文件
export NDK_GCC_CONFIG_x64="--sysroot=$NDK_HOME/platforms/android-21/arch-x86_64 -isystem $NDK_HOME/sysroot/usr/include -isystem $NDK_HOME/sysroot/usr/include/x86_64-linux-android"# ARM CPU架构 配置sysroot,isystem,否则会找不到头文件
export NDK_GCC_CONFIG_ARM="--sysroot=$NDK_HOME/platforms/android-21/arch-arm -isystem $NDK_HOME/sysroot/usr/include -isystem $NDK_HOME/sysroot/usr/include/arm-linux-androideabi"# ARM64 CPU架构 配置sysroot,isystem,否则会找不到头文件
export NDK_GCC_CONFIG_ARM_64="--sysroot=$NDK_HOME/platforms/android-21/arch-arm64 -isystem $NDK_HOME/sysroot/usr/include -isystem $NDK_HOME/sysroot/usr/include/aarch64-linux-android"

Mac(使用CLANG编译)
修改~/.bash_profile

vim ~/.bash_profile

添加以下内容

# NDK目录
export NDK_HOME=/Users/ringle/Library/Android/sdk/ndk/21.1.6352462# CLANG目录
export CLANG=${NDK_HOME}/toolchains/llvm/prebuilt/darwin-x86_64/bin# 添加到PATH中
export PATH=${PATH}:${NDK_HOME}:${CLANG}

编译
这里编译ARM64构架的so

GCC

$NDK_GCC_ARM_64 $NDK_GCC_CONFIG_ARM_64 -fPIC -shared get.c -o libndk-linux.so

CLANG

aarch64-linux-android21-clang -fPIC -shared hi.c -o libndk-mac.so

导入Android Studio

复制so到项目中

在app/src/main 目录下新建jniLibs目录,再新建arm64-v8a目录,将编译生成的libndk-linux.so及libndk-mac.so复制到目录下

配置cmake

在app/src/main 目录下新建cpp目录,新建CMakeLists.txt,配置如下:

# 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.10.2)# Declares and names the project.project("ndk")# 包含所有CPP文件
file(GLOB allCPP *.cpp)# 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.native-lib# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).${allCPP})# 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)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}")# 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.native-lib# Links the target library to the log library# included in the NDK.${log-lib}# 链接libndk-macndk-mac# 链接libndk-linuxndk-linux)

配置gradle

配置app模块下的build.gralde文件

android {defaultConfig {//...externalNativeBuild {cmake {abiFilters "arm64-v8a"}}ndk {abiFilters "arm64-v8a"}}externalNativeBuild {cmake {path "src/main/cpp/CMakeLists.txt"version "3.10.2"}}
}

引用so中的方法

在cpp目录下,新建native-lib.cpp

#include <jni.h>
#include <string>
#include <android/log.h>#define TAG "Wangyz"#define LOG_I(...) __android_log_print(ANDROID_LOG_INFO, TAG,  __VA_ARGS__);extern "C" int get();extern "C" int hi();extern "C" JNIEXPORT jstring JNICALL
Java_com_wangyz_ndk_MainActivity_stringFromJNI(JNIEnv *env,jobject /* this */) {int a = get();LOG_I("hello:%d", a);int b = hi();LOG_I("hi:%d", b);return env->NewStringUTF("hello");
}

Activity中调用

public class MainActivity extends AppCompatActivity {// Used to load the 'native-lib' library on application startup.static {System.loadLibrary("ndk-mac");System.loadLibrary("ndk-linux");System.loadLibrary("native-lib");}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Example of a call to a native methodTextView tv = findViewById(R.id.sample_text);tv.setText(stringFromJNI());}/*** A native method that is implemented by the 'native-lib' native library,* which is packaged with this application.*/public native String stringFromJNI();
}

更多NDK相关资料可以点此处免费获取

NDK交叉编译及so库导入Android项目相关推荐

  1. Eclipse导入android项目出现很多错误

    Eclipse导入android项目出现很多错误 [问题描述]       使用Eclipse导入别人正确的android项目,会出现很多未知的错误.如下面的AndroidTest项目,在别人的Ecl ...

  2. Android开发学习:在Eclipse中导入Android项目方法

    在Eclipse中导入Android项目方法的具体步骤如下: 1.启动Eclipse,依次选择File---Import,如下图所示: 2.在弹出的Import窗口中选择Existing Projec ...

  3. myeclipse中导入android项目后无法视图下查看XML视图

    myeclipse中导入android项目后无法视图下查看XML视图 导入项目后重启myeclipse即可,打开xml文件便可视图预览.

  4. Android Studio 导入 Android 项目

    打开 Android Studio,选择 Open an Existing Project,然后选中 Android 项目的根目录即可导入 Android 项目. 如果你之前使用 Android St ...

  5. Android Studio中导入Android项目StepbyStep

    想把在eclipse的项目导入Android studio,有两种方法,但是我喜欢的是不改变项目文件结构的方法,因为这样可以兼容eclipse. 第一步: 导入的项目不能运行,需要配置运行环境.And ...

  6. 快速导入Android项目

    更新2020.8.26 更新最新的阿里云库地址,旧版依然可以使用,但是建议使用新的,不知道什么时候旧版的就不行了. 使用android studio项目导入项目,遇到的项目跑不起的大多数是gradle ...

  7. Eclipse导入Android项目的方法(转)

    原文:http://www.cnblogs.com/SkyD/archive/2010/11/25/1887219.html 看网上流传的Eclipse导入项目的方法都是在新建Android程序时使用 ...

  8. android数据库导入,Android项目中如何导入数据库

    Android项目中如何导入数据库 发布时间:2020-11-24 16:43:28 来源:亿速云 阅读:110 作者:Leah Android项目中如何导入数据库?很多新手对此不是很清楚,为了帮助大 ...

  9. 不能导入android项目备注

    2019独角兽企业重金招聘Python工程师标准>>> 如果发现导入工程的时候,出现"No projects are found to import" 的提示,首 ...

最新文章

  1. I00039 亲密数(Amicable numbers)
  2. C++ 二分查找函数 lower_bound upper_bound
  3. [多级联动下拉选择框]和[Tree to Tree]续——让他们可以设置默认值
  4. runat=server
  5. IntelliJ IDEA for Mac在MacOS模式下的动态代码模板快捷键(Live Templates Shortcut)
  6. 不积小流,何以成江海。。。积累
  7. 【知易行难】RS485组网连接示意图
  8. DOS windows PE三者有什么区别
  9. 微信小程序,仓库系统,可以扫描入库查询
  10. [Windows] 迅雷 无修改 无限制 无视封锁
  11. Overture教程之五线谱介绍
  12. 项目管理之敏捷开发总结
  13. 垂直居中对齐四种样式
  14. libxml2主要函数说明
  15. 细读《深入理解 Android 内核设计思想》(三)Binder 机制 [上]
  16. 电脑计算机界面打开后无法缩小,技术编辑演示win10系统iE网页界面大小无法缩放的办法...
  17. 多时间尺度源储荷微电网协调调度+日前日内实时+需求响应——附代码
  18. python cmd下载模块_Python cmd包_程序模块 - PyPI - Python中文网
  19. Python数据分析与机器学习45- 股票预测
  20. IE11 安装 此更新不适应计算机

热门文章

  1. python null byte_如何以“正确”的方式处理带有nullbytes的Python unicode字符串?
  2. 深入浅出Rust-Future-Part-5.md
  3. 一切尽显眼前:如何预防在虚拟化环境中丢失可视性和安全性
  4. Unity3D开发——LeRunning的人物角色信息的显示
  5. Webservice开发流程
  6. Linux系统普通用户切换省略输入用户名密码
  7. 道家遁甲式(又名道家奇门)
  8. SpringCloud Eureka Client和Server侧配置及Eureka高可用配置
  9. 关于用户表设计及多用户登入限制
  10. 配置字段(居左,居中,居右,高度自适应)