NDK下载
NDK 下载 | Android NDK | Android Developers
openssl编译(在NOTES.ANDROID中有提示)
export ANDROID_NDK_HOME=/workspace/soft/android-ndk-r21e
PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$ANDROID_NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
./Configure android-arm64 -D__ANDROID_API__=29
make
1. 模拟器调试
adb devices
adb -s 设备名称 shell 进入adb
获取root权限 : su
2.JNI添加新的.cpp文件
file(GLOB native_srcs "src/main/cpp/*.cpp")
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).
native-lib.cpp
${native_srcs})
3. 打印日志
#ifndef NAVTEST_NDKLOG_H
#define NAVTEST_NDKLOG_H
#include <android/log.h>
#define LOGE(logTag, ...) __android_log_print(ANDROID_LOG_ERROR, logTag, __VA_ARGS__)
#define LOGW(logTag, ...) __android_log_print(ANDROID_LOG_WARN, logTag, __VA_ARGS__)
#define LOGD(logTag, ...) __android_log_print(ANDROID_LOG_DEBUG, logTag, __VA_ARGS__)
#define LOGI(logTag, ...) __android_log_print(ANDROID_LOG_INFO, logTag, __VA_ARGS__)
#endif //NAVTEST_NDKLOG_H
#define LOG_TAG "gogoim"
LOGE(LOG_TAG, "hello world------------");
void LogLong(std::string &strLog)
{
#define MAX_PER_LOG_LEN 4
int iNum = strLog.length()/ MAX_PER_LOG_LEN;
int iRes = strLog.length() % MAX_PER_LOG_LEN;
for (int i = 0; i < iNum; i++)
{
std::string strSub = strLog.substr(i * MAX_PER_LOG_LEN, MAX_PER_LOG_LEN);
LOG(INFO) << "xiaoma88:" << strSub;
}
if (0 != iRes)
{
LOG(INFO) << "xiaoma88:" << strLog.substr(MAX_PER_LOG_LEN * iNum, iRes);;
}
}
保存日志:
adb logcat -v time >D:\log.txt
过滤日志级别: adb logcat *:W
直接显示日志信息,是会显示很多日志信息的,好像是从设备最近一次开机的日志开始显示,清除缓存:
adb logcat -c
4. 交叉工具链编译
下载NDK ,在/etc/profile中添加, 然后source /etc/profile
export NDK=/workspace/soft/android-ndk-r21e
export PATH=$PATH:$NDK
$NDK/build/tools/make-standalone-toolchain.sh --platform=android-21 --install-dir=/tmp/my-android-toolchain --arch=amd64
备注: platform 版本要和NDK一样, 如果--install-dir不指定,生成的工具会被压缩
export CC=/tmp/xxx/aarch64-linux-android/bin/aarch64-linux-android-gcc
export CXX=/tmp/xxx/aarch64-linux-android/bin/aarch64-linux-android-g++
Android NDK location不可用,手动指定NDK版本,在local.properties中添加 ndk.dir=D\:\\androidndk\\ndk\\21.0.6113669
JNA/JNI
依赖的.so文件放到 app\src\main\nativeLibs\
-DANDROID_STL=c++_shared externalNativeBuild { cmake { cppFlags "" //arguments '-DANDROID_STL=c++_shared' } }