Add Native Ads to Flutter project using Kotlin
Asked Answered
U

3

6

I'm struggling with adding native ads in my flutter project . My project is using Kotlin for android so I follow the documentation of google_mobile_ads but it keeps showing me these errors after I used the recommendation of converting to kotlin by android studio.

e: reading_app\MainActivity.kt: (15, 113): No value passed for parameter 'layoutInflater'
e: reading_app\ReadingNativeAdFactory.kt: (15, 10): Class 'NativeAdFactoryExample' is not abstract and does not implement abstract member public abstract fun createNativeAd(p0: NativeAd!, p1: (Mutable)Map<String!, Any!>!): NativeAdView! defined in io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.NativeAdFactory
e: reading_app\ReadingNativeAdFactory.kt: (20, 68): Unresolved reference: my_native_ad
e: reading_app\ReadingNativeAdFactory.kt: (21, 63): Unresolved reference: ad_headline
e: reading_app\ReadingNativeAdFactory.kt: (22, 59): Unresolved reference: ad_body

Code in my MainActivity.kt

package com.reading_app

import io.flutter.embedding.android.FlutterActivity


import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin;


class MainActivity: FlutterActivity() {
    @Override
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        flutterEngine.getPlugins().add(GoogleMobileAdsPlugin())
        super.configureFlutterEngine(flutterEngine)
        GoogleMobileAdsPlugin.registerNativeAdFactory(flutterEngine, "adFactoryExample", NativeAdFactoryExample())
    }

    @Override
    fun cleanUpFlutterEngine(flutterEngine: FlutterEngine?) {
        GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "adFactoryExample")
    }
}

code in my ReadingNativeAdFactory.kt

package com.reading_app

import android.graphics.Color
import android.view.LayoutInflater
import android.widget.TextView
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.NativeAdFactory
import java.util.Map


// my_native_ad.xml can be found at
/* https://github.com/googleads/googleads-mobile-flutter/tree/master/packages/google_mobile_ads/example/android/app/src/main/res/layout
*/
internal class NativeAdFactoryExample(layoutInflater: LayoutInflater) : NativeAdFactory {
    private val layoutInflater: LayoutInflater
    @Override
    fun createNativeAd(
            nativeAd: NativeAd, customOptions: Map<String?, Object?>?): NativeAdView {
        val adView: NativeAdView = layoutInflater.inflate(R.layout.my_native_ad, null) as NativeAdView
        val headlineView: TextView = adView.findViewById(R.id.ad_headline)
        val bodyView: TextView = adView.findViewById(R.id.ad_body)
        headlineView.setText(nativeAd.getHeadline())
        bodyView.setText(nativeAd.getBody())
        adView.setBackgroundColor(Color.YELLOW)
        adView.setNativeAd(nativeAd)
        adView.setBodyView(bodyView)
        adView.setHeadlineView(headlineView)
        return adView
    }

    init {
        this.layoutInflater = layoutInflater
    }
}
Uncircumcised answered 5/7, 2021 at 4:34 Comment(0)
U
0

Change

code in my ReadingNativeAdFactory.kt

To

package com.reading_app

import android.graphics.Color
import android.view.LayoutInflater
import android.widget.TextView
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdView
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.NativeAdFactory
import kotlin.collections.Map

internal class NativeAdFactoryLemoon(val layoutInflater: LayoutInflater) : NativeAdFactory {
    override fun createNativeAd(
        nativeAd: NativeAd, customOptions: Map<String, Any>?): NativeAdView  {
        val adView = layoutInflater.inflate(R.layout.my_native_ad, null) as NativeAdView 
        val headlineView = adView.findViewById<TextView>(R.id.ad_headline)
        val bodyView = adView.findViewById<TextView>(R.id.ad_body)
        headlineView.text = nativeAd.headline
        bodyView.text = nativeAd.body
        adView.setBackgroundColor(Color.BLUE)
        adView.setNativeAd(nativeAd)
        adView.bodyView = bodyView
        adView.headlineView = headlineView
        return adView
    }
}
Undercut answered 24/11, 2021 at 23:1 Comment(0)
S
0

I just were troubling with the same error and I'll post my found here. Your error is about LayoutInflater ; So you will need to change code in your MainActivity into something like this:

class MainActivity: FlutterActivity() {
@Override
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    GoogleMobileAdsPlugin.registerNativeAdFactory(flutterEngine, "adFactoryExample", NativeAdFactoryExample( LayoutInflater.from(context)))
}

@Override
override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {
    super.cleanUpFlutterEngine(flutterEngine)
    GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "adFactoryExample")
}
}
Sharondasharos answered 4/4, 2022 at 3:49 Comment(1)
Dear OP, I am reviewer, please edit your question and place } in code blocks.Ulrikaumeko
S
0

Struggling a bit as well after quitting Android dev for a while.

I am using a FlutterFragmentActivity for other reasons. The key for me was to use the layoutInflater getter.

class MainActivity : FlutterFragmentActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        flutterEngine.plugins.add(GoogleMobileAdsPlugin())
        super.configureFlutterEngine(flutterEngine)

        GoogleMobileAdsPlugin.registerNativeAdFactory(
            flutterEngine,
            "adFactoryExample", NativeAdFactoryExample(layoutInflater)
        )
    }

    override fun cleanUpFlutterEngine(flutterEngine: FlutterEngine) {
        GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "adFactoryExample")
        super.cleanUpFlutterEngine(flutterEngine)
    }
}
Scupper answered 9/10 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.