I'm working on the application which uses some of the Android private APIs. I build it as part of full Android (CM10) sources configured for my phone. The resulting binary works great on that device but throws Segmentation faults on other devices with the same Android version. It's not a problem of missing APIs because even a simple hello world compiled that way throws Segfault. Here is a sample code:

hello-cmd.cpp

#include<stdio.h>

int main(int argc, char* argv[]) {
    printf("Hello Command Line!");
    return 0;
}

Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := hello-cmd
LOCAL_SRC_FILES := hello-cmd.cpp
include $(BUILD_EXECUTABLE)

When I build the following code with Android NDK it is portable. When I build it inside android sources it's not.

I guess the problem is a linker configuration but I don't know how to debug or fix this.

有帮助吗?

解决方案

I finally figured out what was the problem. The two devices used different Android versions with incompatible C++ compiler flags. After updating both devices to exactly the same version the problem disappeared.

While it's not possible to compile a single portable binary using private Android APIs it's possible to compile separate binary for each major Android version and ship them with an app. I described this solution in more details on my blog.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top