NDK交叉编译及so库导入Android项目(1)

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= P A T H : {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 “ C M A K E C X X F L A G S − L {CMAKE_CXX_FLAGS} -L CMAKECXXFLAGSL{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-mac

ndk-mac

链接libndk-linux

ndk-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

#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”);

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Example of a call to a native method

TextView 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();

最后

希望大家能有一个好心态,想进什么样的公司要想清楚,并不一定是大公司,我选的也不是特大厂。当然如果你不知道选或是没有规划,那就选大公司!希望我们能先选好想去的公司再投或内推,而不是有一个公司要我我就去!还有就是不要害怕,也不要有压力,平常心对待就行,但准备要充足。最后希望大家都能拿到一份满意的 offer !如果目前有一份工作也请好好珍惜好好努力,找工作其实挺累挺辛苦的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。


《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
的公司要想清楚,并不一定是大公司,我选的也不是特大厂。当然如果你不知道选或是没有规划,那就选大公司!希望我们能先选好想去的公司再投或内推,而不是有一个公司要我我就去!还有就是不要害怕,也不要有压力,平常心对待就行,但准备要充足。最后希望大家都能拿到一份满意的 offer !如果目前有一份工作也请好好珍惜好好努力,找工作其实挺累挺辛苦的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

[外链图片转存中…(img-1E2CAnMM-1715583419708)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 15
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值