Include uuid.h into Android NDK project

女生的网名这么多〃 提交于 2019-12-01 18:17:39

uuid.h isn't part of the NDK. You can check by running find /opt/android-ndk-r8b/ -name uuid.h

You can probably pull the code you need from the AOSP. I found external/e2fsprogs/lib/uuid/uuid.h in the master branch.

18446744073709551615

The article UUIDs and Linux: Everything you ever need to know suggests using

$ cat /proc/sys/kernel/random/uuid
eaf3a162-d770-4ec9-a819-ec96d429ea9f

The command indeed works in Android, although your program will have to read this (/proc/sys/kernel/random/uuid) file rather than call the library.

So if you take String getStringFromFile(String filePath) here,

getStringFromFile("/proc/sys/kernel/random/uuid")

will return an uuid that you can, for example, print to the log:

D/~~~     ( 5065): uuid=418ebd25-4f6e-4431-b31e-784703ea6093

(ran it on Samsung GS4)

I solved by picking arbitrary version of the library and compiling it as a static library. I put all the files in the folder in a directory called uuid. Then I added the following CMakeLists.txt in the external directory:

cmake_minimum_required(VERSION 3.8)

project(uuid)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set (SOURCE_FILES
    uuid/clear.c
    uuid/compare.c
    uuid/copy.c
    uuid/gen_uuid.c
    uuid/isnull.c
    uuid/pack.c
    uuid/parse.c
    uuid/unpack.c
    uuid/unparse.c
    uuid/uuid_time.c
)

add_definitions(
    -DHAVE_INTTYPES_H
    -DHAVE_UNISTD_H
    -DHAVE_ERRNO_H
    -DHAVE_NETINET_IN_H
    -DHAVE_SYS_IOCTL_H
    -DHAVE_SYS_MMAN_H
    -DHAVE_SYS_MOUNT_H
    -DHAVE_SYS_PRCTL_H
    -DHAVE_SYS_RESOURCE_H
    -DHAVE_SYS_SELECT_H
    -DHAVE_SYS_STAT_H
    -DHAVE_SYS_TYPES_H
    -DHAVE_STDLIB_H
    -DHAVE_STRDUP
    -DHAVE_MMAP
    -DHAVE_UTIME_H
    -DHAVE_GETPAGESIZE
    -DHAVE_LSEEK64
    -DHAVE_LSEEK64_PROTOTYPE
    -DHAVE_EXT2_IOCTLS
    -DHAVE_LINUX_FD_H
    -DHAVE_TYPE_SSIZE_T
    -DHAVE_SYS_TIME_H
    -DHAVE_SYS_PARAM_H
    -DHAVE_SYSCONF
)

add_library(uuid STATIC ${SOURCE_FILES})
install(TARGETS uuid DESTINATION lib)

Finally I made sure to target the NDK issuing the following call to cmake:

mkdir -p build-android-arm
cd build-android-arm
cmake ANDROID_ARGS="-DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_TOOLCHAIN_FILE=/opt/android-ndk/build/cmake/android.toolchain.cmake \
    -DANDROID_NDK=/opt/android-ndk \
    -DANDROID_NATIVE_API_LEVEL=23 \
    -DANDROID_TOOLCHAIN=clang" \
    -DANDROID_ABI=armeabi-v7a ..
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!