UnsatisfiedLinkError when compiling with API21

2019-05-18 15:56发布

问题:

My project uses NDK r10d for the c++ code. When I compile the project with API19 it works great, but when I compile it with API21 it crashes on runtime.

When the c lib is loaded I get:

dlopen("/data/app-lib/com.my.app-2/libMyCode.so") failed: dlopen failed: cannot locate symbol "stpcpy" referenced by "libMyCode.so"...

Then it crashes on:

java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "stpcpy" referenced by "libMyCode.so"...

I use OnePlus One running Android4.4.4 to test it on both cases.

Any ideas?

回答1:

Yes - the android libc headers have changed in API 21. Some functions that didn't exist previously were redirected to other functions in the older headers. Therefore you can't really build with API 21 if you want to run on older devices, unless you take great care to work around such issues. If you need to use newer native APIs from API 21 but still be compatible with older devices, you need to do manual work to achieve this anyway.

If you only want the newer API for the java side, just set a separate APP_PLATFORM=19 in Application.mk, while building the java side with a newer SDK.

See Cannot load library: reloc_library[1285]: cannot locate 'rand' for more details on this issue.



回答2:

stpcpy was added to bionic on API 21. This means that the binaries compiled for API 21 may fail to run on earlier platforms. It looks like the gcc compiler can optimize to call stpcpy even if it is not explicitly used in your code. The linked issue also has a suggested workaround:

size_t src_len = strlen(src);
return memcpy(dst, src, src_len) + src_len;


回答3:

The problem was that probably because of security issues, the deprecated procedures strcpy and strlen where removed.

I replaced them with strncpy and strnlen and it works great.