How to add Admob with Firebase

Firebase is a new tools that allow developer to grow user and monetise the app in one stop solution. You can link your existing Admob apps. Just click Analyize tab and select the existing aps and click link to firebase.
NewImage
Download the google-service config file and link it to your Android Studio project.

NewImage

Next , Configure the Gradle file in the Android Studio.

  1. Project-level build.gradle (<project>/build.gradle):
    buildscript { dependencies {
        // Add this line
        classpath 'com.google.gms:google-services:3.0.0' } }
  2. App-level build.gradle (<project>/<app-module>/build.gradle):
    ...
    // Add to the bottom of the file
    apply plugin: 'com.google.gms.google-services'
    includes Firebase Analytics by default

Next add Individual dependency in your <project>/<app-module>/build.gradle and click sync now. Gradle will refresh the project library and add the dependency that you just added.

...
    dependencies {
            compile fileTree(dir:'libs', include:['*.jar'])
            compile 'com.android.support:appcompat-v7:xx.x.x'
            compile 'com.google.firebase:firebase-ads:9.6.0'
        }
...

apply plugin:'com.google.gms.google-services'



NewImage
Open the activity_main.xml in your res/layout folder and add the following code.
<com.google.android.gms.ads.AdViewandroid:id="@+id/ad_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_alignParentBottom="true"ads:adSize="BANNER"ads:adUnitId="@string/banner_ad_unit_id" />

Next, Open the main activity java files add add the following code to init and display the ads.

NewImage
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

/** * Main Activity. Inflates main activity xml and child fragments. */public class MyActivity extends ActionBarActivity {

    private AdView mAdView;

    @Overrideprotected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        // Initialize the Mobile Ads SDK.MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

        // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in // values/strings.xml.mAdView = (AdView) findViewById(R.id.ad_view);

        // Create an ad request. Check your logcat output for the hashed device ID to // get test ads on a physical device. e.g. // "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();

        // Start loading the ad in the background.mAdView.loadAd(adRequest);
    }
 
}
Now you can add a bunch of Firebase tools such as realtime database, analytics, cloud messaging and user authentication.

No comments:

Post a Comment