5

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!

Makram Saleh
  • 8,613
  • 4
  • 27
  • 44
yellow
  • 463
  • 1
  • 6
  • 16

3 Answers3

7

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.

Frohnzie
  • 3,559
  • 1
  • 21
  • 24
  • Thanks for the help! I guess I'll have to incorporate that code into the project then... Any advice for the best way to do so? – yellow Aug 09 '12 at 17:27
  • Just pull the code directly from the AOSP. Looks like all you need is the `uuid` folder which already contains a Android.mk file. Good luck. – Frohnzie Aug 09 '12 at 18:33
  • Hey thanks. I've pulled the folder using the GIT, and I've added it to my project and run the ndk-build. It spits out a "libext2_uuid.so" file; to incorporate this, do I need to create a header file "uuid.h" to reference the methods and include the .so in the Android.mk source list? – yellow Aug 09 '12 at 18:37
  • 1
    Include the .so using `LOCAL_SHARED_LIBRARIES` and add the path to the `uuid` folder using `LOCAL_C_INCLUDES`. – Frohnzie Aug 09 '12 at 18:41
  • When the source code which is not published as public API are subject to change in the future. Better aware of that before using anything like that. – Suman Aug 13 '12 at 07:32
3

I solved by picking an arbitrary version of the library from the Android repository 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 -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_TOOLCHAIN_FILE=/opt/android-ndk/build/cmake/android.toolchain.cmake \
    -DANDROID_NDK=/opt/android-ndk \
    -DANDROID_PLATFORM=android-23 \
    -DANDROID_STL=c++_static \
    -DANDROID_ABI=armeabi-v7a ..
ceztko
  • 14,736
  • 5
  • 58
  • 73
1

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)

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • Did anyone check that it actually works on an Android device? – Dinh Viêt Hoà Dec 04 '14 at 00:31
  • It works but unfortunately libuuid provides a few more functions that just the generation. So i think we have to go the take the source code of linux and compile it way – Lothar Jan 14 '22 at 01:39