}
配置NDK
Linux(使用GCC编译)
编辑Home/用户 目录下的.bashrc
vim /home/wangyz/.bashrc
添加以下内容
配置NDK的目录
export NDK_HOME=/home/wangyz/NDK/android-ndk-r17c
将NDK目录加入PATH中
export PATH= P A T H : 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= 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 CMAKECXXFLAGS−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-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);
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
…(img-FpiosVf6-1715711588494)]
[外链图片转存中…(img-Rs7QYoVg-1715711588496)]
[外链图片转存中…(img-E3qRYOkT-1715711588497)]
[外链图片转存中…(img-Sw8ZT0BT-1715711588498)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!