Implementing ads with AdMob

edited August 2017 in Android Mode

So I recently uploaded my app to the play store and i wanted to implement ads, so I looked up this tutorial and It said I needed the AdMob library, so I went looking for it on the AdMob sites where they say I can get it with the google play service SDK which is great since I can download it with processing

I use the code that is used in the forum post which is this one:

import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.widget.RelativeLayout;
import com.google.ads.*;

@ Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Window window = getWindow();
    RelativeLayout adsLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
    // Displays Ads at the bottom of your sketch, use Gravity.TOP to display them at the top
    adsLayout.setGravity(Gravity.BOTTOM);
    AdView adView = new AdView(this, AdSize.BANNER, "your-publisher-id");  // add your app-id
    adsLayout.addView(adView);
    AdRequest newAdReq = new AdRequest();
    // Remark: uncomment next line for testing your Ads (fake ads)
    //newAdReq.setTesting(true);
    adView.loadAd(newAdReq);
    window.addContentView(adsLayout,lp2);
}

but now the problem is that I got an error that says this: The import com.google cannot be resolved

and the console says this:

----------
1. WARNING in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\MainActivity.java (at line 9)
    import android.content.pm.PackageManager;
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import android.content.pm.PackageManager is never used
----------
2. WARNING in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\MainActivity.java (at line 11)
    import android.support.v4.content.ContextCompat;
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import android.support.v4.content.ContextCompat is never used
----------
3. WARNING in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\MainActivity.java (at line 13)
    import android.app.AlertDialog;
           ^^^^^^^^^^^^^^^^^^^^^^^
The import android.app.AlertDialog is never used
----------
4. WARNING in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\MainActivity.java (at line 14)
    import android.content.DialogInterface;
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import android.content.DialogInterface is never used
----------
5. WARNING in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\MainActivity.java (at line 15)
    import android.Manifest;
           ^^^^^^^^^^^^^^^^
The import android.Manifest is never used
----------
6. WARNING in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\MainActivity.java (at line 49)
    int check;
        ^^^^^
The value of the local variable check is not used
----------
----------
7. ERROR in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\sketch_170817a.java (at line 13)
    import com.google.ads.*; 
           ^^^^^^^^^^
The import com.google cannot be resolved
----------
8. ERROR in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\sketch_170817a.java (at line 14)
    import com.google.android.gms.ads.AdRequest; 
           ^^^^^^^^^^
The import com.google cannot be resolved
----------
9. ERROR in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\sketch_170817a.java (at line 15)
    import com.google.android.gms.ads.AdView; 
           ^^^^^^^^^^
The import com.google cannot be resolved
----------
10. ERROR in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\sketch_170817a.java (at line 16)
    import com.google.android.gms.ads.AdSize; 
           ^^^^^^^^^^
The import com.google cannot be resolved
----------
11. ERROR in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\sketch_170817a.java (at line 17)
    import com.google.android.gms.ads.MobileAds; 
           ^^^^^^^^^^
The import com.google cannot be resolved
----------
12. ERROR in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\sketch_170817a.java (at line 60)
    AdView adView = new AdView(this.getActivity());
    ^^^^^^
AdView cannot be resolved to a type
----------
13. ERROR in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\sketch_170817a.java (at line 60)
    AdView adView = new AdView(this.getActivity());
                        ^^^^^^
AdView cannot be resolved to a type
----------
14. ERROR in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\sketch_170817a.java (at line 61)
    adView.setAdSize(AdSize.BANNER);
                     ^^^^^^
AdSize cannot be resolved to a variable
----------
15. ERROR in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\sketch_170817a.java (at line 64)
    AdRequest newAdReq = new AdRequest.Builder()
    ^^^^^^^^^
AdRequest cannot be resolved to a type
----------
16. ERROR in C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\src\processing\test\sketch_170817a\sketch_170817a.java (at line 64)
    AdRequest newAdReq = new AdRequest.Builder()
                             ^^^^^^^^^
AdRequest cannot be resolved to a type
----------
16 problems (10 errors, 6 warnings)

BUILD FAILED
C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\build.xml:15: The following error occurred while executing this line:
C:\Users\sjors\AppData\Local\Temp\android7328373206848409503sketch\build.xml:28: Compile failed; see the compiler error output for details.

What did I do wrong I installed the google play service SDK through the default SDK manager.

Tagged:

Answers

  • edited August 2017 Answer ✓

    @schotsl===

    • i cannot believe that the error is only with import: your code is NOT for the 3X android processing mode, though it could work for previous versions, when it extended Activity: now PApplet extends fragment.

    • other problem can be with your Manifest: have you added your GMS value in your meta data? - Think also to add permissions (internet)

    • other problem could be with with your import statements: a) try to change them to:

      import com.google.android.gms.ads.AdRequest;
      import com.google.android.gms.ads.AdView;
      import com.google.android.gms.ads.AdSize;
      import com.google.android.gms.ads.MobileAds;
      
    • and instead of installing the SDK automatically (by processing) download it and install manually

  • @akenaton Thanks I changed the import statements but it still doesnt regonize them, where would I find the SDK though since I cant find a download online. I also saw a few posts that said you needed to move the downloaded SDK into the code folder of your sketch but mine doesnt have a folder named "code"

  • edited August 2017

    @scholtsl===

    • As for the first question google is your friend! - But you can download the SDK of course from Android, without AS. After downloading you unzip and put your folder where you want, then tells to processing to use it for SDK. Dont put your SDK folder in the android mode.

    • As for the second question it depends of the version of googleplayservices; till some version you can find it as a jar inside the SDK (folder extras/google/googleplayservices/libproject/libs; if you find it you can take the jar, CREATE a folder named "code" inside your sketch and put it inside: note that this is a standard way to add libs to an app...I cannot remember now what is the last version of SDK where you can find the jar easily, it seems to me that is the 23 or22 release. After that is more complicated because instead of one jar you find a lot of .ar; of course if you are patient you can find which one you have to use but more simple is to download an "old" version of this jar.

    • If theses 2 things are ok, you probably dont have anymore problems with the imports, but i am absolutely sure that the code you have put before cannot work as it is (i have explained why); quickly= you have to override either onCreateView (not onCreate()) and if it does not work override onStart(). And after that probably also find workaround when you add your view...

    • Then think to your manifest...

    • And finally think to use AS or Eclipse for this kind of job...

  • edited October 2017

    @akenaton I got the it 'working' with some code from this post but now when I run it on my android phone it crashes I suspect it has something to do with my manifest file so here's that

        <?xml version="1.0" encoding="UTF-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="">
            <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="26"/>
            <application android:icon="@drawable/icon" android:label="">
                <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
                <activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN"/>
                        <category android:name="android.intent.category.LAUNCHER"/>
                    </intent-filter>
                </activity>
                <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:name="com.google.ads.AdActivity"/>
            </application>
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
            <uses-permission android:name="android.permission.INTERNET"/>
        </manifest>
    

    Here's the actual code

        import android.os.Looper;//for setup() if needed
        import android.os.Bundle;
        import android.view.Window;
        import android.widget.RelativeLayout;
        import android.app.Activity;
        import com.google.ads.*;
        import com.google.android.gms.ads.AdRequest;
        import com.google.android.gms.ads.AdView;
        import com.google.android.gms.ads.AdSize;
    
        public void setup(){
          size(900,600);
         }
    
        public void draw(){
    
        }
    
        @ Override
        public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
        Window window = getActivity().getWindow();
         RelativeLayout adsLayout = new RelativeLayout(this.getActivity());
         RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);//Fill is deprecated
    
        AdView adView = new AdView(this.getActivity());
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-3940256099942544");
          adsLayout.addView(adView);
            AdRequest newAdReq = new AdRequest.Builder()
            .build();
            adView.loadAd(newAdReq);
            window.addContentView(adsLayout,lp2);
        };
    

    and last but not least here's the error

    org.gradle.tooling.BuildException: Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-4.0-bin.zip'.

    I think this is the most vital part of the error and it is insanely long, I could post it here if necessary. I changed everything as you requested and I added the metadata since processing recommended doing that so what's wrong?

Sign In or Register to comment.