Closed
Description
Description
I got an error "W/NativeActivity(18489): ANativeActivity_onCreate not found" in Android after building a large NativeActivity application. I tried to use LOCAL_WHOLE_STATIC_LIBRARIES, but it still didn't work. Command "nm -D -C -g libs/armeabi-v7a/libNativeActivity.so" told me ANativeActivity_onCreate had not been exported.
Solution
I think found a solution that can solve the problem in general. gcc can export functions with using version-script. NativeActivity application doesn't need any functions to be exported except ANativeActivity_onCreate, so I think using this wouldn't cause any problems. If I am right it can be committed to project tree.
native_app_glue Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= android_native_app_glue
LOCAL_SRC_FILES:= android_native_app_glue.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_EXPORT_LDFLAGS:= -Wl,--version-script=$(LOCAL_PATH)/exports.txt
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)
native_app_glue exports.txt
{
global:
ANativeActivity_onCreate;
local: *; # hide everything else
};
Please, correct me if I am not right.
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
alexcohn commentedon May 9, 2017
UPDATE: this comment does not apply to the root cause
If you follow the official sample, this won't happen: a call to
app_dummy()
will keepANativeActivity_onCreate
in your libNativeActivity.sotwaik commentedon May 9, 2017
I've seen many posts at 4pda and stackoverflow about the problem. In all the cases ANativeActivity was not exported even if it was built. That solution not for problem when the ANativeActivity was stripped but for problem where it was build as a local function.
alexcohn commentedon May 9, 2017
If your xxx_CFLAGS include
-fvisibility=hidden
(which is a good practice), it would be enough to add JNIEXPORT to its definition in android_native_app_glue.cThis is defined in
<jni.h>
:I sincerely don't understand why this __attribute__ was never added there.
DanAlbert commentedon May 9, 2017
@alexcohn's solution LGTM, but I want to make sure there isn't something weird going on first.
@twaik: do you have
APP_CFLAGS := -fvisibility=hidden
set? I was able to repro this if I did that, but without that it was exported properly as long as I usedLOCAL_WHOLE_STATIC_LIBRARIES
(which the sample doesn't say to use, sigh).I also noticed that
android_native_app_glue
doesn't export-landroid
like it should. I'll get both of these things fixed in r15 and see if I can find some time to clean up the sample.Thanks for reporting this (and thanks @alexcohn for doing the initial analysis!)
DanAlbert commentedon May 9, 2017
https://android-review.googlesource.com/392595 Export libandroid from android_native_app_glue.
https://android-review.googlesource.com/392596 Ensure that ANativeActivity_onCreate isn't hidden.
twaik commentedon May 9, 2017
android_native_app_glue.c includes <jni.h>, which defines JNIEXPORT, isn't it?
And -fvisibility=hidden flag may be not useful in project which doesn't use native_app_glue, so
DanAlbert commentedon May 9, 2017
@twaik: so is that a yes then? Did you have
APP_CFLAGS := -fvisibility=hidden
? Otherwise there may be another bug here (which those patches will conveniently hide, but not fix).Good point. I'll fix that.
That's just a test. The point is to make sure we're tolerant of people putting that in
APP_CFLAGS
.twaik commentedon May 9, 2017
Tried to add it to project. What I did:
0. Added JNIEXPORT to void ANativeActivity_onCreate definition (in both lower cases).
In both cases ANativeActivity_onCreate was not exported.
Maybe variant with
LOCAL_EXPORT_LDFLAGS:= -Wl,--version-script=$(LOCAL_PATH)/exports.txt
is the only guaranteed to work. And it is usable only for NativeActivity applications, so this line's place only in Android.mk of native_app_glue
DanAlbert commentedon May 9, 2017
What I'm asking is what you did to cause this in the first place. What did your test case that caused you to file the bug look like?
twaik commentedon May 9, 2017
I don't sure know. I tried to build X KDrive server with NDK, it has hundreds of files with thousands function definitions. Result library exports list consists of function names called from the library, but it doesn't contain function names defined in the library (I may be wrong, I just looked for ANativeActivity_onCreate) Maybe a project with the bug must contain a lot of function definitions. I can send the bug project via email.
alexcohn commentedon May 10, 2017
@twaik you probably got
android_native_app_glue.o
stripped away, if you used neitherLOCAL_EXPORT_WHOLE_STATIC_LIBRARIES
nor app_dummy() trick.@DanAlbert I don't think NDK supports
LOCAL_EXPORT_WHOLE_STATIC_LIBRARIES
; that's why the app_dummy trick is easier.twaik commentedon May 10, 2017
My android_main starts from
And my project has this line in Android.mk
LOCAL_WHOLE_STATIC_LIBRARIES := android_native_app_glue android-shmem libpixman
But ANativeActivity_onCreate still is not exported
DanAlbert commentedon May 10, 2017
Could you share your Android.mk/Application.mk?
Why is
LOCAL_EXPORT_WHOLE_STATIC_LIBRARIES
needed? I'm having trouble coming up with a test case that makes this needed (given that I've already added the__attribute__((visibility("default")))
).I was thinking maybe we didn't handle
LOCAL_WHOLE_STATIC_LIBRARIES
properly for static libraries, but it looks like we already do the smart thing and export those to consumers. To be fair, I would not have guessed that we had support for that :)53 remaining items