C++ Firebase linking error in android project

2019-05-01 11:36发布

I'm trying to add firebase c++ sdk to my cocos2d-x 3.14 game. So far I've create Android.mk in firebase_cpp_sdk directory:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := firebase-prebuilt
LOCAL_SRC_FILES := libs/android/$(TARGET_ARCH_ABI)/c++/libapp.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)


include $(CLEAR_VARS)
LOCAL_MODULE := firebase-analytics
LOCAL_SRC_FILES := libs/android/$(TARGET_ARCH_ABI)/c++/libanalytics.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

Then in my project in Android.mk I've added:

LOCAL_C_INCLUDES += /Users/piotr/Documents/pierdoly/firebase_cpp_sdk/include

LOCAL_STATIC_LIBRARIES += firebase-prebuilt firebase-analytics

$(call import-add-path, /Users/piotr/Documents/pierdoly/firebase_cpp_sdk)

I can sync gradle and build project. I also can

#include <firebase/app.h> 

in AppDelegate.cpp (or h) and it works fine. Even Android studio can see all firebase headers and I can inspect them.

Now, in applicationDidFinishLaunching I've added this:

#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
    ::firebase::App* app = ::firebase::App::Create(::firebase::AppOptions());
#else
    ::firebase::App* firebaseApp = ::firebase::App::Create(::firebase::AppOptions(), cocos2d::JniHelper::getEnv(), cocos2d::JniHelper::getActivity());
#endif

There's also alternative version via JNICALL from AppActivity.java (to pass correct activity as some say solution above doesn't work):

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_initFirebase(JNIEnv* env, jobject thiz)
{
    ::firebase::App* app = ::firebase::App::Create(::firebase::AppOptions(), env, thiz);
}
#endif

Yet my problem is: it won't even compile. There's linker error:

Error:(141) undefined reference to 'firebase::App::Create(firebase::AppOptions const&, _JNIEnv*, _jobject*)'

I can clearly "go" into this function in Android Studio and I've rechecked twice parameters I'm giving. They're fine. Yet linker yells at me.

How can I fix linker? What am I missing here?

1条回答
走好不送
2楼-- · 2019-05-01 11:51

I had the same problem but found the answer:

It's about the order of the libraries being linked. You just have to rearrange it like this:

LOCAL_STATIC_LIBRARIES += firebase-analytics firebase-prebuilt

And it should work.

Say thanks to the guys in this github issue

查看更多
登录 后发表回答