I tried to use AdMob with test ads in flutter. I always receive code 3
……………………………………………………………….
android/build.gradle
minSdkVersion 19
targetSdkVersion 33
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation platform('com.google.firebase:firebase-bom:31.2.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.android.gms:play-services-auth:20.4.1'
implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'com.google.android.gms:play-services-ads:21.5.0'
}
app/build.gradle
buildscript {
ext.kotlin_version = '1.7.20'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.15'
}
}
distributionUrl=https://services.gradle.org/distributions/gradle-7.5-all.zip
environment:
sdk: '>=2.19.2 <3.0.0'
google_mobile_ads: ^2.3.0
………………………………………………………………….
main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await MobileAds.instance.initialize().then(
(InitializationStatus status) {
MobileAds.instance.updateRequestConfiguration(
RequestConfiguration(
tagForChildDirectedTreatment:
TagForChildDirectedTreatment.unspecified,
testDeviceIds: <String>[
myDevice,
],
),
);
debugPrint('Initialization done: ${status.adapterStatuses}');
},
);
runApp(const MyApp());
}
………………………………………………………………….
home_page.dart
BannerAd? _anchoredAdaptiveAd;
bool _isLoaded = false;
late Orientation _currentOrientation;
@override
void didChangeDependencies() {
super.didChangeDependencies();
_currentOrientation = MediaQuery.of(context).orientation;
_loadAd();
mess();
}
/// Load another ad, disposing of the current ad if there is one.
Future<void> _loadAd() async {
await _anchoredAdaptiveAd?.dispose();
setState(() {
_anchoredAdaptiveAd = null;
_isLoaded = false;
});
final AnchoredAdaptiveBannerAdSize? size =
await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
MediaQuery.of(context).size.width.truncate());
if (size == null) {
print('Unable to get height of anchored banner.');
return;
}
_anchoredAdaptiveAd = BannerAd(
adUnitId: myBanner,
size: size,
request: const AdRequest(),
listener: BannerAdListener(
onAdLoaded: (Ad ad) {
print('$ad loaded: ${ad.responseInfo}');
setState(() {
// When the ad is loaded, get the ad size and use it to set
// the height of the ad container.
_anchoredAdaptiveAd = ad as BannerAd;
_isLoaded = true;
});
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('Anchored adaptive banner failedToLoad: $error');
ad.dispose();
},
),
);
return _anchoredAdaptiveAd!.load();
}
/// Gets a widget containing the ad, if one is loaded.
///
/// Returns an empty container if no ad is loaded, or the orientation
/// has changed. Also loads a new ad if the orientation changes.
Widget _getAdWidget() {
return OrientationBuilder(
builder: (context, orientation) {
if (_currentOrientation == orientation &&
_anchoredAdaptiveAd != null &&
_isLoaded) {
return Container(
color: Colors.green,
width: _anchoredAdaptiveAd!.size.width.toDouble(),
height: _anchoredAdaptiveAd!.size.height.toDouble(),
child: AdWidget(ad: _anchoredAdaptiveAd!),
);
}
// Reload the ad if the orientation changes.
if (_currentOrientation != orientation) {
_currentOrientation = orientation;
_loadAd();
}
return Container();
},
);
}
body: _getAdWidget();
Log………………………………………………………………………
.
.
.
.
I/Ads (29864): This request is sent from a test device.
Dynamic lookup for intent failed for action: com.google.android.gms.ads.service.START
W/Ads (29864): Not retrying to fetch app settings
I/Ads (29864): Ad failed to load : 3
I/flutter (29864): BannerAd failedToLoad: LoadAdError(code: 3, domain: com.google.android.gms.ads, message: No ad config., responseInfo: ResponseInfo(responseId: null, mediationAdapterClassName: , adapterResponses: [], loadedAdapterResponseInfo: null), responseExtras: {mediation_group_name: BannerGroup, mediation_ab_test_name: BannerGroup_Feb 9, mediation_ab_test_variant: Variant A})
2
Answers
Inside your android/app/src/main/androidManifest.xml add inside
<application> </application>
This line
[your id] = your own adMob id .
And to support android 12 & 13 add the line bellow after
</application>
1-Uninstall your application from your test device
2- Connect your device to the android studio
3- Install again then the ads will start loading
:-It workes for me