Saturday 30 November 2013

Adding ADMOB to Appery Android Projects

Admob in Android Appery Apps, Step-by-Step


Adding Admob adverts to your Appery applications can be daunting.  Below you will find a walkthrough for adding the SDK, and modifying the source code to enable you to quickly and easily master this art.

Currently Appery is using Phonegap V2.4.0, so this guide relates to that version and will be updated if necessary when Phonegap is updated.
 


Exporting from Appery and Importing into Eclipse - the first steps.





  • Firstly, you will need to export your Appery project by clicking Export -> Eclipse Project.
  • Once you have the project, unzip it a a folder, then open Eclipse and Import as "Existing Android Code into the Workspace".
  • Point to your folder where you unzipped the file from Appery, and when given the opportunity, untick the Barcode Scanner as we don't need that and click finish.
  • Right click on your project and select Android Tools-> click Fix Project Properties.
  • Right click on your project and select Build Path -> Configure Build Path.
  • Click Libraries -> Add External Jars and then point to the Google Admon SDK jar.
  • Click Order & Export, then tick all unticked grey boxes and click ok. 


Manifest File Changes.




  • Open the Android Manifest file, and view the .xml file.
  • Add the lines below to the AndroidManifest.xml file above the line "<------ receiver...."
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
  • Find and remove the line below:
    <uses-permission android:name="android.permission.READ_INPUT_STATE" />



Source Code Changes - Admob Code




  • Now open your source code for you project, you need the code that has the OnCreate code. (PhoneGapActivity.java)
  • Add this line under the last import statement
    import com.google.ads.*;
  • Add this the line below under public class PhoneGapActivity extendsorg.apache.cordova.DroidGap {
    private AdView adView;
  • Place the code below directly under these 2 lines;
    onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);


    adView = new AdView(this, AdSize.BANNER, "YourAdMobIDHere");   
    LinearLayout layout = super.root;
    layout.addView(adView);
    layout.setHorizontalGravity(android.view.Gravity.CENTER_HORIZONTAL);          adView.loadAd(new AdRequest());
  • Hover over the [LinearLayout] error that is shown on the line below;
    LinearLayout layout = super.root;
    and select the option: import 'LinearLayout' (Android Widget)
  • Test app (Run As Android app).
  • When ready to create APK for market, edit the AndroidManifest.xml file:
    Look for android:debuggable="true"> and change it to android:debuggable="false">

No comments:

Post a Comment