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
}
}
}
in code blocks. – Ulrikaumeko