I'm porting a C program onto Android using the NDK. The program uses the uuid.h
or uuid/uuid.h
library depending on which is available. When I compile the program, gives the error message uuid.h: No such file or directory
.
I'm new to the NDK, so I'm not entirely sure what the problem is. I'm using cygwin on Windows; does the computer not have the uuid.h
library or Android doesn't support it? Is there a workaround- can I include it somehow in the compiler settings?
Finally, the program only uses the library like so:
char *s;
uuid_t uu;
uuid_create(&uu, NULL);
uuid_to_string(&uu, &s, 0);
Perhaps I could emulate this behaviour with my own C code?
Thanks for any help in advance!
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.
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 ..
来源:https://stackoverflow.com/questions/11888055/include-uuid-h-into-android-ndk-project