Admob Implementation Error

Admob implementation Error

I had exactly the same problem with getting the "you must have AdActivity declared in AndroidManifest.xml with configChanges." error message after integrating the latest AdMob SDK. Even though I have found this and the other two related discussions (see links at the bottom of this article) at StackOverflow, they didn't help me to solve the issue, as -- for me -- they did not differentiate clear enough between the targetSdkVersion attribute in the manifest and the build target. This answer describes what solved the issue for me and what caused trouble.

Solution

The easiest part at first: you are missing a few flags in the configChanges attribute of the definition of the AdActivity in your AndroidManifest.xml. As shown in the AdMob SDK Docs the definition needs to look like this:

<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

The second -- and more complicated part -- is related to the SDK target: The only solution that really seems to work is to use the SDK manager to install the SDK for at least Android 3.2 (API level 13). After you have installed this SDK version you need to configure your IDE to build your project using this SDK. The exact setting depends on the IDE you are using. In my case it is IntelliJ IDEA and there you will find the option in the project settings on the Project page below the headline Project SDK.

You should also adjust the target property in your project's project.properties. This is at least important if you are building your release using ANT. The line should look like this:

target=android-13

The configuration above alone does the trick. There are no changes required to the <uses-sdk> element in the AndroidManifest. Read the pitfalls below to learn why this may cause trouble.

Explanation

The build target and the <uses-sdk> elements have completely different scopes.

The build target is evaluated only at build time by your build tools to determine which version of the SDK tools on your system should be used to build your app. The newer the SDK the more API features does it know. Out of whatever reason Google forces us to specify some configChanges which aren't available before API level 13 and so we need to build our app using at least the SDK tools 13 because a previous version of the SDK tools does not know these new configChanges and will report an error. At runtime the build target does not have any meaning and Android will ignore all elements (e.g. in configChanges) which it does not know.

The targetSdkVersion element specified on the <uses-sdk> element in the android manifest in contrast is only evaluated at run time -- not at compile time. In fact you can specify any value you want here without the compiler changing anything or bringing up an error message. This is why changing this attribute does not help us to solve our AdMob problem. On the other hand at runtime the attribute may be evaluated by android to support some compatibility features for apps which have been build for older android versions. See the pitfalls section below to see a topic which caused trouble for me.

Pitfalls

  • Do not change set targetSdkVersion to a higher version if you are not able to test your app on this android version: Because I have misunderstood the existing answers regarding this admob topic I have also set the android:targetSdkVersion attribute of the <uses-sdk> element to API level 13 in my app which caused a fatal side effect: As Android 3 thought my app would natively support honeycomb, it did no longer show the menu button in the soft button bar at the bottom border and as my app hides the native title bar to show it's own implementation the user did not have any possibility to access the menu any more on honeycomb. So for me leaving the targetSdkVersion at level 10 helped to bring back the menu button and worked fine.
  • Handling backward compatibility to older APIs which may be lost in SDK tools 13: OK, so I have set my build process to use the SDK tools 13 and my targetSdkVersion to 10 and everything should be fine, right? Unfortunately not! The reason is, that my app is compatible downwards to Android 1.5 (API level 3) as this still makes up about 5% of my users. Unfortunately after setting the build target to 13 some parts of my code did not compile any more as they referenced deprecated methods which were supported until SDK tools 10 but no longer starting with SDK tools 11 (e.g. Service.setForeground).

The basics how to handle backwards compatibility are described here -- but this article does not describe how to call deprecated methods which are no longer available as they will cause a compiler error. I solved this by creating a new library project used by my app. For this library project I have set the build target back to 10 which will cause it to be compiled using the SDK tools 10 which still know about the android deprecated API I am using. My app then calls helper methods from this compatibility library and so I can compile my app with a newer target SDK.


Related Topics

Here the list of the other related discussions I have found:

  • AdMob can't display ads because of configChanges
  • AdMob SDK 4.3.1 - I can't display banner

Admob ads implementation gives errors

The total number of methods that can be referenced within a single DEX file is limited. That's why you should enable Multidex.
Make the following changes in your module-level build.gradle file:

    android {
....

defaultConfig {
...
multiDexEnabled true
}

dependencies {
...
implementation "androidx.multidex:multidex:2.0.1" // if you're using AndroidX
implementation 'com.android.support:multidex:1.0.3' // or add this one if you don't use AndroidX
}

You might also need to change your application class file:

  1. If you don't use your own Application class, add the following line to your AndroidManifest.xml file:
    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<application
...
android:name="androidx.multidex.MultiDexApplication" >
...
</application>
</manifest>

  1. If you override the Application class, make sure your class extends MultiDexApplication:
    public class MyApplication extends MultiDexApplication {
// your app code
}

More details about Multidex you can find here.

Android AdMob implementation play-services problem

Problem is happening because you are using Support Libraries such as support:appcompat, support:support-v4 along with the latest version of the play-services-ads (18.2.0) which already migrated to AndroidX.

So, you are getting this error because your app is using Support Library but play-services-ads is already using AndroidX.

In order to fix:

1) Migrate your app to AndroidX (recommended). More info HERE

2) Use an old version of play-services-ads (such as 'com.google.android.gms:play-services-ads:16.0.0' for example.)

This second option may fix your issue because play-services-ads:16.0.0 was still using Support Library as well.

I got error when add AdMob ads in my project

In your project gradle, use this version of firebase-core. This should solve the issue!

implementation 'com.google.firebase:firebase-core:16.0.8'


Related Topics



Leave a reply



Submit