Unable to link <hardware/gps.h> with ndk-build

1,472 views
Skip to first unread message

techxpl...@gmail.com

unread,
Mar 3, 2017, 10:48:01 AM3/3/17
to android-ndk
Hi,

I am encountering an issue when trying to include gps.h into my project. Kept having an error while compiling (fatal error: 'hardware/gps.h' file not found).

Below is a sample of the Android.mk and source file used.

.c file
===================================
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <hardware/gps.h>

const GpsInterface* gGpsInterface = NULL;
const AGpsInterface* gAGpsInterface = NULL;
...
===================================

Android.mk file
===================================
LOCAL_PATH             := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_C_INCLUDES       := \
        ../include

LOCAL_SRC_FILES        := \
        ../src/sample.c

LOCAL_SHARED_LIBRARIES := \
        libcutils libhardware

LOCAL_MODULE           := sample

LOCAL_MODULE_TAGS      := optional
include $(BUILD_EXECUTABLE)
===================================

The project folder root folder has 3 sub-folders (src, include, libs).
Any help is much appreciated.



Philippe Simons

unread,
Mar 3, 2017, 11:40:42 AM3/3/17
to android-ndk

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/30a73534-c5c9-4994-8f0e-b2400b370a45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

techxpl...@gmail.com

unread,
Mar 5, 2017, 12:44:43 PM3/5/17
to android-ndk

Hi Philippe Simons,

Thanks for your response. Actually was trying to add libhardware.so from Android's AOSP to my ndk project.
That was the reason for using the gps api.

Manage to resolve the previously header issues, but now am not able to instruct ndk-build where to find the
library (.so) files. This caused linker errors while compiling.

Any assistance is greatly appreciated.
 

Alex Cohn

unread,
Mar 5, 2017, 12:44:43 PM3/5/17
to android-ndk
You are expected to pull https://android.googlesource.com/platform/hardware/libhardware/+/android-4.2.2_r1 (or different branch that matches your device). If you want to run your executable on other devices, you should use dlopen(libhardware) and tune your API usage to the platform.

BR,
Alex

techxpl...@gmail.com

unread,
Mar 5, 2017, 12:44:45 PM3/5/17
to android-ndk
* Edit: I am able to instruct ndk-build in locating the headers files, but am not able to instruct it to locate the library files. The (ASOP) library files are currently located in a libs folder in my project root directory.

Alex Cohn

unread,
Mar 6, 2017, 10:13:06 AM3/6/17
to android-ndk
To use the AOSP libraries, I suggest the following change to your Android.mk:

LOCAL_PATH             := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_C_INCLUDES       := \
        ../include

LOCAL_SRC_FILES        := \
        ../src/sample.c

LOCAL_LDLIBS := -Llibs \
        -lcutils -lhardware

LOCAL_MODULE           := sample

LOCAL_MODULE_TAGS      := optional
include $(BUILD_EXECUTABLE)

You could define libcutils and libhardware them as PREBUILT_SHARED_LIBRARIES, but this is mostly irrelevant for BUILD_EXECUTABLE (you don't rely on ndk-build to copy the libraries to your APK), and you really don't want to copy system libraries, but rather to use the ones actually installed on your device.

Note that ndk-build may complain about using non-official libraries in LOCAL_LDLIBS, but you can ignore these warnings.

BR,
Alex Cohn

techxpl...@gmail.com

unread,
Mar 6, 2017, 10:13:06 AM3/6/17
to android-ndk
Thanks Alex. Will try as adviced. Originally thought Android.mk would be similar to a C makefile and able to link shared libraries (using LOCAL_SHARED_LIBRARIES) while compiling.

Dan Albert

unread,
Mar 6, 2017, 2:00:59 PM3/6/17
to android-ndk
Note that if you do rely on loading the libraries from the device, your app isn't going to work on N or newer: https://developer.android.com/about/versions/nougat/android-7.0-changes.html#ndk

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

Alex Cohn

unread,
Mar 14, 2017, 10:35:53 AM3/14/17
to android-ndk
Does this apply also to executables that run outside an app context, most likely from 'shell' prompt for UID=2000?


On Monday, March 6, 2017 at 9:00:59 PM UTC+2, Dan Albert wrote:
Note that if you do rely on loading the libraries from the device, your app isn't going to work on N or newer: https://developer.android.com/about/versions/nougat/android-7.0-changes.html#ndk
On Mar 6, 2017 07:13, "Alex Cohn" <sasha...@gmail.com> wrote:
To use the AOSP libraries, I suggest the following change to your Android.mk:

LOCAL_PATH             := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_C_INCLUDES       := \
        ../include

LOCAL_SRC_FILES        := \
        ../src/sample.c

LOCAL_LDLIBS := -Llibs \
        -lcutils -lhardware

LOCAL_MODULE           := sample

LOCAL_MODULE_TAGS      := optional
include $(BUILD_EXECUTABLE)

You could define libcutils and libhardware them as PREBUILT_SHARED_LIBRARIES, but this is mostly irrelevant for BUILD_EXECUTABLE (you don't rely on ndk-build to copy the libraries to your APK), and you really don't want to copy system libraries, but rather to use the ones actually installed on your device.

Note that ndk-build may complain about using non-official libraries in LOCAL_LDLIBS, but you can ignore these warnings.

BR,
Alex Cohn

On Sunday, March 5, 2017 at 7:44:45 PM UTC+2, techxpl...@gmail.com wrote:
* Edit: I am able to instruct ndk-build in locating the headers files, but am not able to instruct it to locate the library files. The (ASOP) library files are currently located in a libs folder in my project root directory.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

Alex Cohn

unread,
Mar 14, 2017, 10:35:55 AM3/14/17
to android-ndk
Dan, it's not clear: are you blocking dlopen("/system/lib/libicuuc.so")?

Thanks,

Alex


On Monday, March 6, 2017 at 9:00:59 PM UTC+2, Dan Albert wrote:
Note that if you do rely on loading the libraries from the device, your app isn't going to work on N or newer: https://developer.android.com/about/versions/nougat/android-7.0-changes.html#ndk
On Mar 6, 2017 07:13, "Alex Cohn" <sasha...@gmail.com> wrote:
To use the AOSP libraries, I suggest the following change to your Android.mk:

LOCAL_PATH             := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_C_INCLUDES       := \
        ../include

LOCAL_SRC_FILES        := \
        ../src/sample.c

LOCAL_LDLIBS := -Llibs \
        -lcutils -lhardware

LOCAL_MODULE           := sample

LOCAL_MODULE_TAGS      := optional
include $(BUILD_EXECUTABLE)

You could define libcutils and libhardware them as PREBUILT_SHARED_LIBRARIES, but this is mostly irrelevant for BUILD_EXECUTABLE (you don't rely on ndk-build to copy the libraries to your APK), and you really don't want to copy system libraries, but rather to use the ones actually installed on your device.

Note that ndk-build may complain about using non-official libraries in LOCAL_LDLIBS, but you can ignore these warnings.

BR,
Alex Cohn

On Sunday, March 5, 2017 at 7:44:45 PM UTC+2, techxpl...@gmail.com wrote:
* Edit: I am able to instruct ndk-build in locating the headers files, but am not able to instruct it to locate the library files. The (ASOP) library files are currently located in a libs folder in my project root directory.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

techxpl...@gmail.com

unread,
Mar 14, 2017, 10:35:55 AM3/14/17
to android-ndk
Found the solution to the problem, thought of sharing it as a closure.

1) Check out a aosp version
2) Place the project folder into the aosp folder
3) Compile according to source.android.com
4) Grab the ELF binary from the respective sub-folder in "out"
5) Push binary to device and execute

Thanks all for your help.

Dan Albert

unread,
Mar 14, 2017, 3:13:19 PM3/14/17
to android-ndk
Dan, it's not clear: are you blocking dlopen("/system/lib/libicuuc.so")?

No, that's still available. We're actually pretty close to having a wrapper for it available for the NDK. The ABI of that isn't actually constant because ICU versions all of its function names. The wrapper will deal with the mess of figuring out what you need to dlsym for you.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

techxpl...@gmail.com

unread,
Mar 14, 2017, 7:50:59 PM3/14/17
to android-ndk
Hi Alex,

Just to clarify, dlopen was not used in the solution project, only declared in Android.mk as shared library.
Encountered issues when using dlopen, keeps having an error that libvendorconn is not found.

Alex Cohn

unread,
Mar 15, 2017, 1:41:44 PM3/15/17
to android-ndk
Yes, I understand that you don't use dlopen. Actually, I don't see -ldl in your Android.mk at all. Your approach to build a binary in the framework of platform build, instead of using NDK build, is absolutely legitimate, and what makes it so tempting is that historically NDK build was introduced as a fork of the platform build infrastructure. Even 10 years later, the configuration files are mostly compatible.

OTOH, in many cases, e.g. for your executable, it is an overkill. Pulling the necessary 'dependency' libraries from a physical device or from the emulator is not more dangerous than rebuilding them in AOSP. libgps and libhardware depend on the device, but you can assume that the gps.h API is always supported.

BR,
Alex

NoAngel

unread,
Mar 16, 2017, 4:30:49 AM3/16/17
to android-ndk
I'm sorry, but what topic starter trying to achieve by using native GPS API?
GPS will be slow anyway to exceed all JNI overheads possible. My advice is to use compatible and stable Java API via JNI if possible. It should be supported in future Android versions. Using unstable native API is a bad idea and may break things in future even if it works now somehow.

techxpl...@gmail.com

unread,
Mar 17, 2017, 2:43:43 AM3/17/17
to android-ndk
Hi Alex,

Did tried dlopen with -ldl in Android.mk, after seeing your earlier comments but later decided to build within aosp. You mentioned about libgps, can't seem to locate this on the devices I'm testing, only able to locate libhardware. Could you advice me where this library may be located?

Tested on a range of devices, seems ok.

Alex Cohn

unread,
Mar 20, 2017, 6:13:01 PM3/20/17
to android-ndk
libgps.so was used sometimes on older devices, see e.g. https://groups.google.com/forum/#!topic/android-porting/xnVQN4gjoNQ
Reply all
Reply to author
Forward
0 new messages