1

So I have seen this article about it and it solves a bit of the problem I guess. Link: similar problem here

However, I am using AdMob and Firebase integration and therefore I use an AdView, AdRequest and MobileAds etc. I use these in the AndroidLauncher and that works fine, but when trying to implement an interface called AdsManager and creating a variable of type AdView it can't resolve it. I guess it is because I am out of the android module. So how can I get an "AdMob type" variable from AndroidLauncher when I'm in the Core module (MyGdxGame)?

Example: Android module

AdsManager

package com.game.mygame.monetization;

public interface AdsManager {
    void showAds(boolean show);
}

AndroidLauncher

public class AndroidLauncher extends AndroidApplication implements AdsManager {

private final String TAG = "AndroidLauncher";
private AdHandler adHandler; // Variable I want to reach from core module

static class AdHandler extends Handler {
    private final int ADS_SHOW = 1;
    private final int ADS_HIDE = 0;
    private AdView adBanner; // Variable I want to reach from core module

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        switch (msg.what) {
            case ADS_SHOW:
                adBanner.setVisibility(View.VISIBLE);
                break;

            case ADS_HIDE:
                adBanner.setVisibility(View.GONE);
                break;
        }
    }
}

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    RelativeLayout layout = new RelativeLayout(this);
    View view = initializeForView(new MyGdxGame(this), config);
    layout.addView(view);

    implementAds();
    requestAds(layout);

    // Sets the background image
    setContentView(layout);
}

public void implementAds() {
    // Implement ads
    MobileAds.initialize(this, "");

    adHandler = new AdHandler();
    adHandler.adBanner = new AdView(this);
    adHandler.adBanner.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            Log.i(TAG, "Ad loaded ...");
        }
    });

    adHandler.adBanner.setAdSize(AdSize.SMART_BANNER);
    adHandler.adBanner.setAdUnitId("");

}

public void requestAds(RelativeLayout layout) {
    AdRequest.Builder builder = new AdRequest.Builder();
    builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
    builder.addTestDevice("");

    RelativeLayout.LayoutParams adBannerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

    layout.addView(adHandler.adBanner, adBannerParams);
    adHandler.adBanner.loadAd(builder.build());
}

@Override
public void showAds(boolean show) {
    adHandler.sendEmptyMessage(show ? adHandler.ADS_SHOW : adHandler.ADS_HIDE);
}

}

When trying to create a class called AndroidAdsManager that implements AdsManager it can't resolve a private AdView variable because I'm no longer in the android module I guess.

Core module

public class AndroidAdsManager implements AdsManager {
    private AdView adBanner; // Error: Android Studio doesn't find this type
    private AdHandler adHandler; // Also needs this in here, I guess, maybe not

    @Override
    public void showAds(boolean show) {
        adHandler.sendEmptyMessage(show ? adHandler.ADS_SHOW : adHandler.ADS_HIDE);
    }
}

1 Answer 1

1

Long story short: You can't

Hello Kevvex, There´s no way you can access AdView or AdHandler from classes in the core module, what you need to do to work with them is (As shown in the link you provided) have all your logic implemented in the AndroidAdsManager class that implements your AdsManager interface

The way I would go about this is, whenever you want something to be done with types from the Android module, have a methdod in the AndroidAdsManager do that something, you will need to create the methods in the AdsManagerInterface in order to call them:
AndroidAdsManager

public class AndroidAdsManager implements AdsManager {
  private AdView adBanner;
  private AdHandler adHandler;

    @Override
    public void showAds(boolean show) {
        adHandler.sendEmptyMessage(show ? adHandler.ADS_SHOW : adHandler.ADS_HIDE);
    }
    @Override
    public void implementAds() {
        // Implement ads
        MobileAds.initialize(this, ""); // HERE YOU NEED TO GET THE CONTEXT

        adHandler = new AdHandler();
        adHandler.adBanner = new AdView(this);
        adHandler.adBanner.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                Log.i(TAG, "Ad loaded ...");
            }
        });

        adHandler.adBanner.setAdSize(AdSize.SMART_BANNER);
        adHandler.adBanner.setAdUnitId("");
    }
    // You need to get a RelativeLayout reference for this method to work
    // I would get it from AndroidLauncher
    public void requestAds() {
        AdRequest.Builder builder = new AdRequest.Builder();
        builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        builder.addTestDevice("");

        RelativeLayout.LayoutParams adBannerParams = new 
        RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT);
        adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

        layout.addView(adHandler.adBanner, adBannerParams);
        adHandler.adBanner.loadAd(builder.build());
    }
}

AdsManager

public interface AdsManager {
    void implementAds();
    void requestAds(); // Relative layout param removed from here
    void showAds(boolean show);
}

After this, if you want these operations to affect your game, simply pass the reference of your game instance (core module) to the AndroidAdsManager from the AndroidLauncher to be able to call any method you want from there, hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but I can't have AdMob types variables in the core package still? So why do these exist in the AndroidAdsManager?
That's correct, you can't have AdMob type variables in the Core module. They exist in the AndroidAdsManager because that class is in the Android module. AdsManager is an interface in the Core module. Your Game class needs to ask for a reference (in the constructor I suggest) of an AdsManager instance (That will be an AndroidAdsManager instance really).
So I've tried to figure out how to get the RelativeLayout and Context from AndroidLauncher since I need them in requestAds() and implementAds() but I can't pass a reference in requestAds(RelativeLayout layout) because I do not have access to these variables in the core module, I tried making a static layout variable but apparently that causes a memory leak according to Android Studio.
Neither you want nor you need to call requestAds(RelativeLayout layout) from core module, in fact, it would make no sense because you simply can't. What you do instead is calling requestAds() from CORE from an AndroidAdsManager INSTANCE, It's important to remember your AndroidAdsManager class is in the ANDROID module. Your AdsManager interface is in the CORE module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.