[android] Admob Custom Video Rewarded/interstitial/banner Tutorial

Discussion in 'Advertising' started by lqur, Apr 30, 2019.

  1. lqur

    lqur Boxer

    Joined:
    Nov 25, 2015
    Messages:
    23
    Likes Received:
    19
    I have looked and didn't found a way to properly implement Custom ad network for Rewarded Video so I decide to do it and I am sharing the result with you.

    So, in this super fast tutorial, we are going to create custom code for Banner, Interstitial and Rewarded Video using AdMob and Android Studio.

    1. Buildbox Setup
    There is no need to setup Advertiser ID under Advertisement tab in the Project Setup.

    1.1 Banner and Interstitial UI (on Game Over UI for example)
    Set "Custom" for both. Set frequency as you wish:
    Screen Shot 2019-04-30 at 2.57.49 PM.png
    1.2 Rewarded Video (on Purchase Button inside a Revive UI for example):
    Screen Shot 2019-04-30 at 5.02.17 PM.png

    1.3 Export your project for Android

    2. Android Studio
    Before anything else, create you App ID and 3 Ad Units with AdMob.
    Then you need to modify AndroidManifest.xml and AdIntegrator.java files.

    2.1 In AndroidManifest.XML, you need to add the highlighted lines an replace with you AdMob App ID (under Application element).
    Screen Shot 2019-04-30 at 5.12.21 PM.png

    Code:
     <meta-data
                android:name="com.google.android.gms.ads.APPLICATION_ID"
                android:value="[PUT YOUR APP ID HERE]"/>
    2.2 In AdIntegrator.java replace everything with this code and replace with your Ad Unit ID where stated in code:
    Code:
    package com.buildbox;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.RelativeLayout;
    
    
    import org.cocos2dx.lib.Cocos2dxActivity;
    import java.lang.ref.WeakReference;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdSize;
    import com.google.android.gms.ads.AdView;
    import com.google.android.gms.ads.InterstitialAd;
    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.reward.RewardedVideoAd;
    import com.google.android.gms.ads.MobileAds;
    import com.google.android.gms.ads.reward.RewardedVideoAdListener;
    import com.google.android.gms.ads.reward.RewardItem;
    
    public class AdIntegrator {
        public static native boolean rewardedVideoDidEnd();
        public static AdView mAdView;
        public static InterstitialAd mInterstitialAd;
        public static RewardedVideoAd mRewardedVideoAd;
    
        private static WeakReference<Cocos2dxActivity> activity;
    
        public static void initBridge(Cocos2dxActivity act){
            activity = new WeakReference<Cocos2dxActivity>(act);
        }
    
        public static void initAds(){
            activity.get().runOnUiThread( new Runnable() {
                public void run() {
    
                    //Admob AppID
                    MobileAds.initialize(activity.get(), "[REPLACE WITH ADMOB APP ID]");
    
                    //banner
                    FrameLayout frameLayout = (FrameLayout)activity.get().findViewById(android.R.id.content);
                    RelativeLayout layout = new RelativeLayout( activity.get() );
                    frameLayout.addView( layout );
                    layout.setVerticalGravity(Gravity.BOTTOM);
                    mAdView = new AdView( activity.get());
                    mAdView.setAdSize(AdSize.SMART_BANNER);
                    mAdView.setAdUnitId("[REPLACE WITH ADMOB BANNER ADD UNIT ID]");
                    mAdView.setVisibility(View.INVISIBLE);
                    AdRequest adRequest = new AdRequest.Builder().build();
                    mAdView.loadAd(adRequest);
                    layout.addView(mAdView);
    
                    //interstitial
                    mInterstitialAd = new InterstitialAd(activity.get());
                    mInterstitialAd.setAdUnitId("[REPLACE WITH ADMOB INTERSTITIAL ADD UNIT ID]");
                    mInterstitialAd.loadAd(new AdRequest.Builder().build());
                    mInterstitialAd.setAdListener(new AdListener() {
                        @Override
                        public void onAdClosed() {
                            // Load the next interstitial.
                            mInterstitialAd.loadAd(new AdRequest.Builder().build());
                        }
    
                    });
                 
                    //rewarded
                    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(activity.get());
                    mRewardedVideoAd.loadAd("[REPLACE WITH ADMOB REWARDED VIDEO ADD UNIT ID]", new AdRequest.Builder().build());
                    mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
                            @Override
                            public void onRewardedVideoAdClosed () {
                                AdIntegrator.rewardedVideoDidEnd();
                                mRewardedVideoAd.loadAd("[REPLACE WITH ADMOB REWARDED VIDEO ADD UNIT ID]", new AdRequest.Builder().build());
                            }
                            @Override
                            public void onRewardedVideoAdOpened() {
    
                            }
                            @Override
                            public void onRewardedVideoStarted() {
    
                            }
                            @Override
                            public void onRewardedVideoCompleted() {
    
                            }
                            @Override
                            public void onRewardedVideoAdLeftApplication() {
    
                            }
                            @Override
                            public void onRewarded(RewardItem reward) {
    
                            }
                            @Override
                            public void onRewardedVideoAdFailedToLoad(int errorCode) {
    
                            }
                            @Override
                            public void onRewardedVideoAdLoaded() {
    
                            }
                        }
                    );
                }
            });
    
    
        }
    
        public static void showBanner(){
            activity.get().runOnUiThread( new Runnable() {
                public void run() {
                    mAdView.setVisibility(View.VISIBLE);
                }
            });
        }
    
        public static void hideBanner(){
            activity.get().runOnUiThread( new Runnable() {
                public void run() {
                    mAdView.setVisibility(View.INVISIBLE);
                }
            });
        }
    
        public static boolean isBannerVisible(){
            return true;
        }
    
        public static boolean isRewardedVideoAvialable(){
            return true;
        }
    
        public static void showInterstitial(){
            activity.get().runOnUiThread( new Runnable() {
                public void run() {
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    }
                }
            });
        }
    
        public static void showRewardedVideo(){
            activity.get().runOnUiThread( new Runnable() {
                public void run() {
                    if (mRewardedVideoAd.isLoaded()) {
                        mRewardedVideoAd.show();
                    }
                }
            });
        }
    
    }
    
    Et voilĂ .
     
  2. George L.

    George L. Boxer

    Joined:
    Feb 23, 2019
    Messages:
    33
    Likes Received:
    3
    Dude you are awesome !!!!
     
  3. George L.

    George L. Boxer

    Joined:
    Feb 23, 2019
    Messages:
    33
    Likes Received:
    3
    Do you know how to do this for IOS as well ? If yes could you please share that also :)

    Thank you !
     
  4. George L.

    George L. Boxer

    Joined:
    Feb 23, 2019
    Messages:
    33
    Likes Received:
    3
    i dont know if this is an issue in BB but the rewarded vid button does not dissapear if there is no video
     
    Last edited: May 4, 2019
  5. Alex809

    Alex809 Boxer

    Joined:
    Apr 29, 2019
    Messages:
    9
    Likes Received:
    1
    The same tutorial but for iOS would be greatly appreciated. Thanks!
     
  6. nishant Kumar

    nishant Kumar Boxer

    Joined:
    Mar 24, 2019
    Messages:
    5
    Likes Received:
    1
    Dude, I try this code but getting app crash on my device, in Andorid studio showing successfully installed but giving warning message


    Note: Some input files use or override a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    Note: E:\bb\voodo\cjnew\hmg1\android\app\src\main\java\org\cocos2dx\lib\Cocos2dxReflectionHelper.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    Please help me!
     
  7. nishant Kumar

    nishant Kumar Boxer

    Joined:
    Mar 24, 2019
    Messages:
    5
    Likes Received:
    1
    @George L
    I follow your Tutorial and apply code, The app installed in my device, opens Google Play Games and than crash.
    Plz help me
     
  8. George L.

    George L. Boxer

    Joined:
    Feb 23, 2019
    Messages:
    33
    Likes Received:
    3
    Hi, the above is not my code :D @lqur posted it :) and it works fine for me. The warning you are getting is not from the rewarded video it is from something else , I don't know the source (I have the same warning. check to make sure you test with admob test ids and that they are correctly placed. I only use the rewarded video part not everything.
     
  9. nishant Kumar

    nishant Kumar Boxer

    Joined:
    Mar 24, 2019
    Messages:
    5
    Likes Received:
    1
    @lqur,
    I try to integrate your Admob code in Android Studio. and the app install in my device but crash immediately showing google play game install
    I am using BB3 Virsion- 3 B#3044
    Please help me, here is the file donwload link https://we.tl/t-xRRcY42tdX

    Also i got error while installing in my device from android studio, later on other try it disappear
     

    Attached Files:

  10. Bilz636

    Bilz636 Avid Boxer

    Joined:
    Oct 5, 2015
    Messages:
    313
    Likes Received:
    182
    Hi man
    I followed your method and its working fine.
    i used ads test units id
    int and banner ads popup normally.
    But when i click on reward video button No ad show up
    is it normal or its not working?
     
  11. imenload

    imenload Boxer

    Joined:
    May 25, 2019
    Messages:
    2
    Likes Received:
    0
    I have bb2 and can u tell me if this works on bb2.3.3?
     
  12. pelloempire

    pelloempire Boxer

    Joined:
    Oct 28, 2018
    Messages:
    13
    Likes Received:
    0
    any update?
     
  13. musicaRoca

    musicaRoca Boxer

    Joined:
    Jul 27, 2019
    Messages:
    1
    Likes Received:
    1
    Thanks for this Tutorial :).
    But change this:

    Code:
    @Override
    public void onRewarded(RewardItem reward) {
                AdIntegrator.rewardedVideoDidEnd();
    }
    
    The Game will only be loaded when the video has been completely watched.
    In the tutorial above, it is also loaded when canceled.
     
    Last edited: Aug 11, 2019
    Forgive likes this.
  14. shahar.m192

    shahar.m192 Avid Boxer

    Joined:
    Oct 7, 2018
    Messages:
    204
    Likes Received:
    44
    its working on buildbox 3?
     
  15. tonymartz2

    tonymartz2 Avid Boxer

    Joined:
    Jun 9, 2016
    Messages:
    198
    Likes Received:
    46
    tried in on BB3 no good for me,
     
  16. Forgive

    Forgive Boxer

    Joined:
    Apr 16, 2019
    Messages:
    58
    Likes Received:
    4
    Last edited: Oct 29, 2019
  17. Forgive

    Forgive Boxer

    Joined:
    Apr 16, 2019
    Messages:
    58
    Likes Received:
    4
    OMG It is working well!!!!!
     
  18. Forgive

    Forgive Boxer

    Joined:
    Apr 16, 2019
    Messages:
    58
    Likes Received:
    4
    Yes, you are good at programming. This way is the correct way!

    User should be rewarded after onRewarded() method not
    onRewardedVideoAdClosed ()
     
  19. youcef

    youcef Boxer

    Joined:
    Jul 6, 2018
    Messages:
    8
    Likes Received:
    3
    Hi.. Thanks for the tutorial.. I have a problem.. The game crashes right after i close the rewarded video ad.. Any help?
     
  20. mytab2mendu

    mytab2mendu Boxer

    Joined:
    Mar 19, 2017
    Messages:
    3
    Likes Received:
    1
    Hi @Iqur

    I was able to get banner and interstitial ads without reward code .. if I add a reward code app getting crashed, and in BB3D how to have a reward action button... it would greatly help if you could respond if you have any solution
     

Share This Page