2017-08-31 160 views
2

我已經安裝了Firebase,做了整個設置,但是當我在我的android上加載我的應用程序時說:「不幸的是,(APP)已停止」。該應用程序只是默認的Cocos HelloWorldScene。我使用的是cocos2dx 3.15.1,Visual Studio 2013,Android NDK r10e和SDK版本26.0.1。Cocos2d Firebase Admob崩潰

proj.android工作室\程序\ JNI \ Android.mk:

FIREBASE_CPP_SDK_DIR := ../../../firebase_cpp_sdk 

APP_ABI := armeabi-v7a x86 
STL := $(firstword $(subst _, ,$(APP_STL))) 
FIREBASE_LIBRARY_PATH := $(FIREBASE_CPP_SDK_DIR)/libs/android/$(TARGET_ARCH_ABI)/$(STL) 

include $(CLEAR_VARS) 
LOCAL_MODULE := firebase_app 
LOCAL_SRC_FILES := $(FIREBASE_LIBRARY_PATH)/libapp.a 
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FIREBASE_CPP_SDK_DIR)/include 
include $(PREBUILT_STATIC_LIBRARY) 

include $(CLEAR_VARS) 
LOCAL_MODULE := firebase_feature 
LOCAL_SRC_FILES := $(FIREBASE_LIBRARY_PATH)/libadmob.a 
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(FIREBASE_CPP_SDK_DIR)/include 
include $(PREBUILT_STATIC_LIBRARY) 

include $(CLEAR_VARS) 

$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d) 
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/external) 
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos) 
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos/audio/include) 

LOCAL_MODULE := MyGame_shared 

LOCAL_MODULE_FILENAME := libMyGame 

LOCAL_SRC_FILES := hellocpp/main.cpp \ 
        ../../../Classes/AppDelegate.cpp \ 
        ../../../Classes/HelloWorldScene.cpp \ 
        ../../../Classes/FirebaseHelper.cpp 


LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../Classes 

# _COCOS_HEADER_ANDROID_BEGIN 
# _COCOS_HEADER_ANDROID_END 


LOCAL_STATIC_LIBRARIES := cocos2dx_static 
LOCAL_STATIC_LIBRARIES += firebase_app 
LOCAL_STATIC_LIBRARIES += firebase_feature 

# _COCOS_LIB_ANDROID_BEGIN 
# _COCOS_LIB_ANDROID_END 

include $(BUILD_SHARED_LIBRARY) 

$(call import-module,.) 

# _COCOS_LIB_IMPORT_ANDROID_BEGIN 
# _COCOS_LIB_IMPORT_ANDROID_END 

AppDelegate.cpp:

#include "AppDelegate.h" 
#include "HelloWorldScene.h" 
#include "firebase/app.h" 
#include "firebase/admob.h" 

// #define USE_AUDIO_ENGINE 1 
// #define USE_SIMPLE_AUDIO_ENGINE 1 

#if USE_AUDIO_ENGINE && USE_SIMPLE_AUDIO_ENGINE 
#error "Don't use AudioEngine and SimpleAudioEngine at the same time. Please just select one in your game!" 
#endif 

#if USE_AUDIO_ENGINE 
#include "audio/include/AudioEngine.h" 
using namespace cocos2d::experimental; 
#elif USE_SIMPLE_AUDIO_ENGINE 
#include "audio/include/SimpleAudioEngine.h" 
using namespace CocosDenshion; 
#endif 

USING_NS_CC; 

static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); 
static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); 
static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); 
static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); 

AppDelegate::AppDelegate() 
{ 
} 

AppDelegate::~AppDelegate() 
{ 
#if USE_AUDIO_ENGINE 
    AudioEngine::end(); 
#elif USE_SIMPLE_AUDIO_ENGINE 
    SimpleAudioEngine::end(); 
#endif 
} 

// if you want a different context, modify the value of glContextAttrs 
// it will affect all platforms 
void AppDelegate::initGLContextAttrs() 
{ 
    // set OpenGL context attributes: red,green,blue,alpha,depth,stencil 
    GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; 

    GLView::setGLContextAttrs(glContextAttrs); 
} 

// if you want to use the package manager to install more packages, 
// don't modify or remove this function 
static int register_all_packages() 
{ 
    return 0; //flag for packages manager 
} 

bool AppDelegate::applicationDidFinishLaunching() { 
    // initialize director 
    auto director = Director::getInstance(); 
    auto glview = director->getOpenGLView(); 
    if(!glview) { 
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) 
     glview = GLViewImpl::createWithRect("Shaokang", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); 
#else 
     glview = GLViewImpl::create("Shaokang"); 
#endif 
     director->setOpenGLView(glview); 
    } 

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) 
    // Initialize Firebase for Android. 
    firebase::App* app = firebase::App::Create(
     firebase::AppOptions(), JniHelper::getEnv(), JniHelper::getActivity()); 
    // Initialize AdMob. 
    firebase::admob::Initialize(*app, "INSERT_YOUR_ADMOB_ANDROID_APP_ID"); 
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 
    // Initialize Firebase for iOS. 
    firebase::App* app = firebase::App::Create(firebase::AppOptions()); 
    // Initialize AdMob. 
    firebase::admob::Initialize(*app, "INSERT_YOUR_ADMOB_IOS_APP_ID"); 
#endif 
    // Initialize AdMob. 
    firebase::admob::Initialize(*app); 

    // turn on display FPS 
    director->setDisplayStats(true); 

    // set FPS. the default value is 1.0/60 if you don't call this 
    director->setAnimationInterval(1.0f/60); 

    // Set the design resolution 
    glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); 
    auto frameSize = glview->getFrameSize(); 
    // if the frame's height is larger than the height of medium size. 
    if (frameSize.height > mediumResolutionSize.height) 
    {   
     director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); 
    } 
    // if the frame's height is larger than the height of small size. 
    else if (frameSize.height > smallResolutionSize.height) 
    {   
     director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); 
    } 
    // if the frame's height is smaller than the height of medium size. 
    else 
    {   
     director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); 
    } 

    register_all_packages(); 

    // create a scene. it's an autorelease object 
    auto scene = HelloWorld::createScene(); 

    // run 
    director->runWithScene(scene); 

    return true; 
} 

// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked. 
void AppDelegate::applicationDidEnterBackground() { 
    Director::getInstance()->stopAnimation(); 

#if USE_AUDIO_ENGINE 
    AudioEngine::pauseAll(); 
#elif USE_SIMPLE_AUDIO_ENGINE 
    SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 
    SimpleAudioEngine::getInstance()->pauseAllEffects(); 
#endif 
} 

// this function will be called when the app is active again 
void AppDelegate::applicationWillEnterForeground() { 
    Director::getInstance()->startAnimation(); 

#if USE_AUDIO_ENGINE 
    AudioEngine::resumeAll(); 
#elif USE_SIMPLE_AUDIO_ENGINE 
    SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 
    SimpleAudioEngine::getInstance()->resumeAllEffects(); 
#endif 
} 

HelloWorldScene.cpp:

#include "HelloWorldScene.h" 
#include "SimpleAudioEngine.h" 
#include "FirebaseHelper.h" 
#include "firebase/admob.h" 
#include "firebase/admob/types.h" 
#include "firebase/app.h" 
#include "firebase/future.h" 
#include "firebase/admob/banner_view.h" 

USING_NS_CC; 

firebase::admob::BannerView* banner_view; 

Scene* HelloWorld::createScene() 
{ 
    return HelloWorld::create(); 
} 

// on "init" you need to initialize your instance 
bool HelloWorld::init() 
{ 
    ////////////////////////////// 
    // 1. super init first 
    if (!Scene::init()) 
    { 
     return false; 
    } 

    auto visibleSize = Director::getInstance()->getVisibleSize(); 
    Vec2 origin = Director::getInstance()->getVisibleOrigin(); 

    ///////////////////////////// 
    // 2. add a menu item with "X" image, which is clicked to quit the program 
    // you may modify it. 

    // add a "close" icon to exit the progress. it's an autorelease object 
    auto closeItem = MenuItemImage::create(
              "CloseNormal.png", 
              "CloseSelected.png", 
              CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 

    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 
           origin.y + closeItem->getContentSize().height/2)); 

    // create menu, it's an autorelease object 
    auto menu = Menu::create(closeItem, NULL); 
    menu->setPosition(Vec2::ZERO); 
    this->addChild(menu, 1); 

    ///////////////////////////// 
    // 3. add your codes below... 

    // add a label shows "Hello World" 
    // create and initialize a label 

    auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); 

    // position the label on the center of the screen 
    label->setPosition(Vec2(origin.x + visibleSize.width/2, 
          origin.y + visibleSize.height - label->getContentSize().height)); 

    // add the label as a child to this layer 
    this->addChild(label, 1); 

    // add "HelloWorld" splash screen" 
    auto sprite = Sprite::create("HelloWorld.png"); 

    // position the sprite on the center of the screen 
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 

    // add the sprite as a child to this layer 
    this->addChild(sprite, 0); 


#if defined(__ANDROID__) 
    // Android ad unit IDs. 
    const char* kBannerAdUnit = "ca-app-pub-3940256099942544/6300978111"; 
#else 
    // iOS ad unit IDs. 
    const char* kBannerAdUnit = "ca-app-pub-3940256099942544/2934735716"; 
#endif 

    // Create and initialize banner view. 
    firebase::admob::BannerView* banner_view; 
    banner_view = new firebase::admob::BannerView(); 
    firebase::admob::AdSize ad_size; 
    ad_size.ad_size_type = firebase::admob::kAdSizeStandard; 
    ad_size.width = 320; 
    ad_size.height = 50; 
    banner_view->Initialize(getAdParent(), kBannerAdUnit, ad_size); 

    // Schedule updates so that the Cocos2d-x update() method gets called. 
    this->scheduleUpdate(); 

    return true; 
} 


void HelloWorld::menuCloseCallback(Ref* pSender) 
{ 
    //Close the cocos2d-x game scene and quit the application 
    Director::getInstance()->end(); 

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 
    exit(0); 
#endif 

    /*To navigate back to native iOS screen(if present) without quitting the application ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/ 

    //EventCustom customEndEvent("game_scene_close_event"); 
    //_eventDispatcher->dispatchEvent(&customEndEvent); 


} 

void HelloWorld::update(float delta) { 
    // Check that the banner has been initialized. 
    if (banner_view->InitializeLastResult().status() == 
     firebase::kFutureStatusComplete) { 
     // Check that the banner hasn't started loading. 
     if (banner_view->LoadAdLastResult().status() == 
      firebase::kFutureStatusInvalid) { 
      // Make the banner visible and load an ad. 
      CCLOG("Loading a banner."); 
      banner_view->Show(); 
      firebase::admob::AdRequest my_ad_request = {}; 
      banner_view->LoadAd(my_ad_request); 
     } 
    } 
} 

我的應用程序是這實際上是默認項目,只有Firebase教程中的所有內容都添加到該項目中,並且崩潰。我已經安裝了firebase_cpp文件,FirebaseHelper頭文件和源代碼都是它們在教程中的樣子,至少我認爲Cocos2d應用程序中的所有其他內容都是默認的東西。我該怎麼辦?會是什麼呢?

當我運行Android Studio中的應用程序,它說的錯誤:E/firebase: Unable to check Google Play services availablity as the com.google.android.gms.common.GoogleApiAvailability class is not present in this application.

回答

0

我認爲谷歌Play服務不可用。您可以通過以下方法檢查: -

/** * 用於檢查谷歌播放服務是否可用 * @參數方面 * @返回 */

public boolean isGooglePlayServicesAvailable(Context context) { 
    GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); 
    int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context); 
    return resultCode == ConnectionResult.SUCCESS; 
} 
+0

什麼代碼文件我是否把它放進去,如果是或者不可用,我該怎麼辦? – TheRoomDiedWithIt

+0

其實我認爲你需要在做Firebase整合之前安裝遊戲服務。否則你應該向用戶展示某種類型的彈出窗口。 你也應該在安裝了GPS的設備上運行這段代碼。 –