Showing posts with label cocos2dx: admob. Show all posts
Showing posts with label cocos2dx: admob. Show all posts

Thursday, May 14, 2015

Cocos2dx: Admob Android

After doing the steps for iOS Admob, continue with the steps below for Android.

Copy the files JNIHelpers.cpp and JNIHelpers.h from folder External Cocos Helper Android Frameworks/Classes to your project's Classes folder:



Copy the files SonarFrameworks.cpp and SonarFrameworks.h to the Classes folder of your cocos project:



Copy the file SonarFrameworkSettings.xml file from res/values to the res/values of your proj.android:
( Do not overwrite the res folder else you will lose the drawable folders and also the strings.xml )



Then, copy the src folder and overwrite the src folder in proj.android:



And copy the AndroidManifest.xml and replace the same file in proj.android
Open the AndroidManifest.xml file in proj.android and make the following changes.
Change the package name to your packagename.


Edit the Android.mk file in proj.android jni folder:


to add the additional classes as follows:

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/Bird.cpp \
                   ../../Classes/GameOverScene.cpp \
                   ../../Classes/GameScene.cpp \
                   ../../Classes/MainMenuScene.cpp \
                   ../../Classes/SplashScene.cpp \
                   ../../Classes/Pipe.cpp \
                   ../../Classes/JNIHelpers.cpp \
                   ../../Classes/SonarFrameworks.cpp 

The added files are:

   ../../Classes/JNIHelpers.cpp \
   ../../Classes/SonarFrameworks.cpp 

Then build the proj.android by running build_native.py.

Open Eclipse and import libcocos2dx and your HelloCocos project. Then import google-play-services-lib:



Then, add it as a library:

Then add the google-play-services-lib.jar:



And also make sure to Order and Export it by checking the checkbox:



Then, open the AndroidManifest.xml file and make the following changes.
Enabled the following tags (by uncommenting them ), below is what it should look like:

<!-- Required for Google Play Services 
<meta-data
            android:name="com.google.android.gms.games.APP_ID"
            android:value="@string/google_play_game_app_id" /> -->
<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" /> 
<!-- End here requirement for Google Play Services -->


<!-- Required for AdMob -->
        <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
<!-- End here requirement for AdMob -->


Enable only the following permissions and disable the rest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Then open SonarFrameworkSettings.xml and enter the admob ids:



Copy the AdMob folder from Frameworks folder to folder proj.android/src/sonar/systems/frameworks



Click File, Refresh the Eclipse project to see the new Admob framework:


Open the file SonarFrameworkSettings.java and set Admob to true:




                  public static final boolean USE_ADMOB = true;

That's it. Just run and it will work just like the iOS version.








Cocos2dx: Admob iOS

This is based on the frameworks developed by Sonar Systems.
Download the SonarCocosHelper files from:

https://github.com/SonarSystems/Sonar-Cocos-Helper

You can follow the youtube instructions therein.

After downloading the zip file, unzip it and you get three main folders within Sonar-Cocos-Helper:



1. SonarCocosHelper
2. External Cocos Helper Android Frameworks
3. External Cocos Helper iOS Frameworks

Within the External Cocos Helper iOS Frameworks, unzip the GoogleMobileAdsSdkiOS-7.1.0.zip file to get the GoogleMobileAds.framework file.


Then, copy and paste it into the proj.ios_mac/ios folder:



Then, drag the GoogleMobileAds.framework from the above folder and drop it into the corresponding iOS folder in Xcode:


In above, be sure to select CreateGroups and nothing else.  Having completed this step, it should look like this:




Then copy the SonarCocosHelper folder into the Classes folder of your cocos project:


Then, drag and drop SonarCocosHelper folder from the Classes folder above to your Xcode Classes folder:


Note that you only select CreateGroups. No need to Copy files if needed because you already copied it into the Classes folder. Also no need to create folder references.

You should now get:


Note that the SonarCocosHelper folder is brown instead of blue, and, the SonarCocosHelper folder is within the Classes folder.

Then, open the SCHSettings.h and edit the admob fields as follows:

/*
 Copyright (C) 2015 Sonar Systems - All Rights Reserved
 You may use, distribute and modify this code under the
 terms of the MIT license

 Any external frameworks used have their own licenses and
 should be followed as such.
*/
//
//  SCHSettings.h
//  Sonar Cocos Helper
//
//  Created by Sonar Systems on 03/03/2015.
//
//

#ifndef __SCHSettings_h__
#define __SCHSettings_h__

#define SCH_IS_iADS_ENABLED false // iAd.framework
#define SCH_IS_AD_MOB_ENABLED true // AdSupport.framework, AudioToolbox.framework, AVFoundation.framework, CoreGraphics.framework, CoreMedia.framework, CoreTelephony.framework, EventKit.framework, EventKitUI.framework, MessageUI.framework, StoreKit.framework, SystemConfiguration.framework
#define SCH_IS_CHARTBOOST_ENABLED false // StoreKit.framework, Foundation.framework, CoreGraphics.framework, UIKit.framework
#define SCH_IS_REVMOB_ENABLED false // SystemConfiguration.framework, StoreKit.framework, MediaPlayer.framework, AdSupport.framework
#define SCH_IS_SOCIAL_ENABLED false // Social.framework
#define SCH_IS_GAME_CENTER_ENABLED false // Social.framework, GameKit.framework
//#define SCH_IS_EVERYPLAY_ENABLED false // AdSupport (iOS 6+, set to Optional link for pre-iOS 6 compatibility).framework, AssetsLibrary.framework, AudioToolbox.framework, AVFoundation.framework, CoreGraphics.framework, CoreImage (iOS 5+, set to Optional link for pre-iOS 5 compatibility).framework, CoreMedia.framework, CoreVideo.framework, Foundation.framework, MessageUI.framework, MobileCoreServices.framework, OpenGLES.framework, QuartzCore.framework, Security.framework, Social (iOS 6+, set to Optional link for pre-iOS 6 compatibility).framework, StoreKit.framework, SystemConfiguration.framework, Twitter (iOS 5+, set to Optional link for pre-iOS 5 compatibility).framework, UIKit.framework

#define SCH_IS_MOPUB_ENABLED false // AdSupport.framework,CoreGraphics.framework,CoreLocation.framework,CoreTelephony.framework,EventKit.framework,EventKitUI.framework,Foundation.framework,MediaPlayer.framework,QuartzCore.framework,StoreKit.framework†,SystemConfiguration.framework,UIKit.framework

#define SCH_MOPUB_BANNER_AD_UNIT @""
#define SCH_MOPUB_LAUNCH_INTERSTITIAL_AD_UNIT @""
#define SCH_MOPUB_ENDLEVEL_INTERSTITIAL_AD_UNIT @""

#define SCH_CHARTBOOST_APP_ID @""
#define SCH_CHARTBOOST_APP_SIGNATURE @""

#define SCH_REVMOB_MEDIA_ID @""

#define SCH_AD_MOB_BANNER_AD_UNIT_ID @"ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"
#define SCH_AD_MOB_FULLSCREEN_AD_UNIT_ID @"ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"
#define SCH_AD_MOB_TEST_DEVICE @""

//#define SCH_EVERYPLAY_CLIENT_ID @""
//#define SCH_EVERYPLAY_CLIENT_SECRET @""

#endif


Take note of these lines:

#define SCH_IS_AD_MOB_ENABLED true

#define SCH_AD_MOB_BANNER_AD_UNIT_ID @"ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"
#define SCH_AD_MOB_FULLSCREEN_AD_UNIT_ID @"ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"

Note that the SCH_AD_MOB_ENABLED is set to true. Make sure all the others are set to false. At time of writing MoPub is set to true by default - be sure to set it to false. Also, fill in the Admob banner and Fullscreen ad units. Fullscreen ad unit is also known as Interstitial ad in Admob terms. The location of the SCHSettings.h file is shown below:



Then, add these frameworks to Xcode:

// AdSupport.framework, AudioToolbox.framework, AVFoundation.framework, CoreGraphics.framework, CoreMedia.framework, CoreTelephony.framework, EventKit.framework, EventKitUI.framework, MessageUI.framework, StoreKit.framework, SystemConfiguration.framework

Alternatively, instead of adding the above frameworks one-by-one by hand, you can do it automatically by enabling modules:



Once you enable modules, Xcode  will automatically add the necessary frameworks needed by Admob.


Then, to show ads, call the functions in the file SonarFrameworks.cpp. But before we can do that,
we need to init Admob before calling the show ads. This is best done in the GameScene. Don't put it in Splash or Menu scene because they are not always being accessed. GameScene is always accessed.

Open your GameScreen.cpp file and add:

#include "SonarFrameworks.h"


And in the init method, do this:

SonarCocosHelper::IOS::Setup();

Now, you can show ads in any scene you like, for example, I show ads in my GameOverScene.
In your GameOverScene.cpp, add the following include:

 #include "SonarFrameworks.h" 

Then, to show  banner and interstitial,  put this in the init method of GameOverScene.cpp:

//--- Admob ---
//SonarCocosHelper::AdMob::hideBannerAd();
//SonarCocosHelper::AdMob::showBannerAd();
//Default is top banner, if no param specified
SonarCocosHelper::AdMob::showBannerAd(SonarCocosHelper::AdBannerPosition::eBottom);
    
this->scheduleOnce(schedule_selector(GameOverScene::showAdmobInterstitial), 3);


Here is the implementation for showAdmobInterstitial:

void GameOverScene::showAdmobInterstitial(float dt){
    SonarCocosHelper::AdMob::showFullscreenAd();
}


And the corresponding .h file:

class GameOverScene : public cocos2d::Layer
{
public:
    
    static cocos2d::Scene* createScene(unsigned int tempScore);
    virtual bool init();
   
    CREATE_FUNC(GameOverScene);
    
private:

    //--- Admob ---
    void showAdmobInterstitial(float dt);
};


When run, the iOS simulator will show Admob banner and interstitial Test ads.