73

Background

I have an app with Admob SDK used in it, to show Interstitial ads (full screen ads).

Reccently Google has updated the SDK, along with many other things (build tools, gradle plugin, IDE, etc...), including admob (firebase ads).

compile 'com.google.firebase:firebase-ads:11.4.2'

The problem

After updating, I noticed that whenever the app tries to show an Interstitial ad, it crashes.

The crash log is as such:

10-28 14:01:01.394 4523-4523/... I/Ads: Ad opening.
10-28 14:01:01.400 1606-2154/? E/ActivityManager: Activity Manager Crash. UID:10080 PID:4523 TRANS:63
                                                  java.lang.IllegalStateException: Only fullscreen activities can request orientation
                                                      at com.android.server.am.ActivityRecord.setRequestedOrientation(ActivityRecord.java:2189)
                                                      at com.android.server.am.ActivityManagerService.setRequestedOrientation(ActivityManagerService.java:4975)
                                                      at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:1101)
                                                      at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2927)
                                                      at android.os.Binder.execTransact(Binder.java:697)


10-28 14:01:01.403 4523-4523/... D/AndroidRuntime: Shutting down VM
10-28 14:01:01.406 4523-4523/... E/AndroidRuntime: FATAL EXCEPTION: main

java.lang.RuntimeException: Unable to start activity ComponentInfo{.../com.google.android.gms.ads.AdActivity}: java.lang.IllegalStateException: Only fullscreen activities can request orientation

Thing is that it worked fine before. Now I'm not sure what causes it to occur.

Searching the web, I didn't see anything that talks about "Only fullscreen activities can request orientation" .

What I had in the manifest, is something a bit old, that might not be needed anymore:

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent"/>

I say it's probably not needed, because i can't see it anymore in the tutorials, even though I remember I did in the past:

What I've tried

  1. I tried to remove the manifest entry. Still same issue.

  2. I tried to set the theme to be "@android:style/Theme.Translucent.NoTitleBar.Fullscreen" by force, but it also didn't help.

  3. I even thought that it might be because I currently have Android 8.1, but when I tried using the app from the play store, I noticed it worked just fine, without crashes.

  4. I tried to go back to as far as version 11.2.2 of admob and use gradle v 2.3.3 , but it also didn't help.

  5. When trying on an emulator with Android 7.0 , it worked fine with latest versions of everything, and removal of the whole manifest activity tag.

  6. I tried to use this in gradle file, instead of what I used :

    compile 'com.google.android.gms:play-services-ads:11.4.2'

Still crashed on my device (Nexus 5x with Android 8.1) yet didn't crash on emulator.

  1. Finally, I tried to run the app on emulator with Android 8.1, and it crashed just as on the real device.

  2. Reported about this to Google Admob team, as well as Android issue tracker. I hope they will answer me about this.

The questions

What could be the cause to this issue?

Is it because of Android 8.1 ? If so, how come the older version of the app worked fine? I tried to revert there a lot of versions to old ones, yet it still crashed.

And, most importantly, how can I solve it?

Floern
  • 33,559
  • 24
  • 104
  • 119
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • I think they are supposed to release play services library targeting the 8.1 release and it will include the fix. (they have only released a support library, when they usually release both support and play services libraries when there is an sdk update) – meh Oct 30 '17 at 14:29
  • Half a year passed, and the problem is still actual :| Tried latest version `firebase-ads:11.8.0` – Vasily Kabunov Feb 05 '18 at 11:13

7 Answers7

63

EDIT

Now a newer admob version is available. I confirm that using the latest releases (15.0.1 or newer) the issue doesn't appear anymore, so you can use the latest version of Admob

com.google.firebase:firebase-ads:17.1.2

setting the target version to the latest, if you have used the workaround previously suggested.

----------------------------------------------------------

Set the target to Android API Level to 26 to fix the issue, waiting for Admob update

In build.gradle

targetSdkVersion 26

if targetSdkVersion is >=27 ( > android.os.Build.VERSION_CODES.Othat is 26) you get this error, they have changed ActivityRecord in latest Android version adding this:

void setRequestedOrientation(int requestedOrientation) {
        if (ActivityInfo.isFixedOrientation(requestedOrientation) && !fullscreen
                && appInfo.targetSdkVersion > O) {
            throw new IllegalStateException("Only fullscreen activities can request orientation");
        ....
        }

The change has been introduced in this commit

Prevent non-fullscreen activities from influencing orientation This changelist enforces that activities targeting O and beyond can only specify an orientation if they are fullscreen.

and probably in admob lib have messed some check

Silverstorm
  • 15,398
  • 2
  • 38
  • 52
  • 16
    Why would they add this limitation, of being able to change the orientation just for full screen activity? Doesn't make sense. An app should be deciding for itself if it will be landscape or portrait, no matter if it's full screen or not. – android developer Oct 29 '17 at 08:07
  • 2
    OK, wrote a request to remove this restriction here: https://issuetracker.google.com/issues/68454482 – android developer Oct 29 '17 at 15:11
  • Seems this solved it, but when does this issue occur? When I ever set the targetSdk to the new one, how can I avoid it? – android developer Oct 29 '17 at 17:32
  • @androiddeveloper you should be able to avoid it if don't specify any orientation in manifest for the related non-fullscreen activity – Silverstorm Oct 29 '17 at 19:02
  • What if the activity is supposed to be in landscape, yet not full screen... – android developer Oct 29 '17 at 19:47
  • I don't understand what you mean. You mean you've tried this ? – android developer Oct 30 '17 at 08:17
  • Sorry for the typo. I haven't tried, however shouldn't be important in what mode is if you don't specify orientation in the manifest, since if the Activity isn't fixed the `if(...)` condition of the quoted code block is `false` and the Exception not thrown unless you have set the orientation mode of related Activity in the manifest and consequently orientation `isFixed(...)` is `true`. Otherwise if the orientation is specified in the manifest only fullscreen are allowed to use `setRequestedOrientation` regardless manifest fixed orientation. – Silverstorm Oct 30 '17 at 14:37
  • This makes zero sense. In Android O (= API 26) they ask if the targetSdkVersion is greater than O and let the app crash if it is. – devconsole Dec 20 '17 at 12:14
  • @devconsole The if is > O, not >=O so 26 isn't included in the check that starts from versions >=27 – Silverstorm Dec 22 '17 at 16:51
  • 5
    I use 15.0.1 but problem still persists on some Android 8 devices not all, maybe because they use the old google play service version – Wayne Sep 11 '18 at 03:15
  • @Wayne Did you find any solution for those devices ? I'm facing the same problem on some devices… – AnthoPak Oct 03 '18 at 12:26
  • @AnthoPak not yet :( – Wayne Oct 08 '18 at 08:23
44

EDIT

the newer (above 16) admob version fixed the issue

Old Answer

try to add this in your manifest

<activity
android:name="com.google.android.gms.ads.AdActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
tools:replace="android:theme"
 />

Or, if you are using appcompat:

<style name="TranslucentTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    etc

And in the manifest, make sure the activity is declared with

android:theme="@style/TranslucentTheme"
Omar Mahmoud
  • 2,902
  • 1
  • 14
  • 22
  • 1
    Better to use `Theme.Translucent.NoTitleBar.Fullscreen`, because original theme of `AdActivity` is `Theme.Translucent` – Khang .NT Feb 13 '18 at 05:59
  • 1
    Why don't they fix this as the highest priority? It's ridiculous for thousands of developers to all have to do this exact fix after many hours of research and unhappy users due to crashes.... – doctorram Mar 24 '18 at 14:53
  • Another variant of this answer that might work better: https://issuetracker.google.com/issues/68427483#comment31 – Gabor Apr 17 '18 at 02:36
  • @Khang.NT - the problem with this solution is that this particular theme (Theme.Translucent.NoTitleBar.Fullscreen) is causing this crash... for some reasons. So no translucency for us I think :( – zompi Jun 18 '18 at 10:58
  • But what if I don't want my app to be full screen? – Mars Nov 14 '18 at 21:57
  • Thanks. This works: google services 4.0.1 / firebase-ads 17.1.1 / API 28 Pie – TomV Nov 20 '18 at 14:27
3

please do not use any properties in Android Manifest files AdActivity.

please remove this line from AdActivity in Manifest file

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

Just simply add this following line to Android Manifest file.

    <activity android:name="com.google.android.gms.ads.AdActivity" />

If you want to set your desired activity as portrait mode please add this following line to your activities onCreate method:

if (android.os.Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
   activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

For firebase ADS 17.0.0 and upper versions use this line to manifest file.

<application
...........>

    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="@string/app_id" />

</application>

You can get app id from Admob -> Apps -> your app -> App Settings -> App Id. Please copy and paste this App_Id into your project.

Ahamadullah Saikat
  • 4,437
  • 42
  • 39
2

This issue will Automatically be fixed when ad mob will update their ads sdk for API 27 their ads dependency do not support API 27 so due to this issue occurring.

 compile 'com.google.android.gms:play-services-ads:11.8.0'

Only full screen activities can request orientation, this is due to ads meta data in your mainfest file,admob should update their sdk for 27 version. enter image description here

if you have set target sdk version 27 then you would get this error, actually android updating sdk very fast, and some update admob left behind to do ,for integration with android sdk, so admob have pending this update, they should do this very soon ,Its not on developer side issue, if you wants to escape from this crash then set your sdk to 26.

Najaf Ali
  • 1,433
  • 16
  • 26
  • What exactly is the command that can trigger this crash in code? And why this weird restriction ? – android developer Dec 28 '17 at 16:19
  • just due to admob is not update when it will update their dependencies of showing ads with android 8.0 its just due to admob side late updation . it may take in weeks hopefully. then it will be correct. – Najaf Ali Jan 03 '18 at 08:31
  • Where did you get this screenshot from? Does it say there that the fix will be applied, and on which version of Admob? – android developer Jan 03 '18 at 08:33
  • I get this screen shot from my respected sir on Developer Console and sent me to correct this and then i fixed it by the time setting target 26, as dependencies was not updated on admob side when it will be fixed then i shall update my app to 27 – Najaf Ali Jan 05 '18 at 05:06
  • Do you know if and when they will fix this? – android developer Jan 05 '18 at 09:08
  • 3
    How on earth has this NOT been fixed yet Google team? It's a very simple fix - for something that has been causing major disruption to everyone using their SDK for well over a month now. I thought with the update on 18th December this was fixed. No crashes in AVDs anymore, in Android Studio but from looking at the latest crash reports - this has still not been fixed in the library. Very frustrating to not be able to support the latest OS. – David West Jan 08 '18 at 13:31
  • yes you are right, am notifying this thing they are changing their policies , sdk updation very fast they should care about the stability of the sdk version also.am till building apk at target sdk 26, due to this issue. – Najaf Ali Jan 12 '18 at 12:00
  • 2
    Great. We've been told to upgrade the SDK as soon as possible. We did it, we got punished. – Kimi Chiu Jan 14 '18 at 06:00
1

It seems that upgrading to:

com.google.firebase:firebase-ads:15.0.1

solved this issue for me. I've just tested it on Nexus 5X with 8.1.0 and Interstitial Admob ads work now.

More complete solution:

app's build.gradle:

...

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    defaultConfig {
        ...

        targetSdkVersion 27

        ..
    }
}

dependencies {
    ...

    implementation 'com.google.firebase:firebase-core:15.0.2'
    implementation 'com.google.firebase:firebase-ads:15.0.1'

    ...
}
apply plugin: 'com.google.gms.google-services'

top level build.gradle:

buildscript {
    ...

    dependencies {
        ...

        classpath 'com.google.gms:google-services:3.3.0'

        ...
    }
}

...
Ziem
  • 6,579
  • 8
  • 53
  • 86
1

I got the same crash with Vungle SDK (which was included through Appodeal SDK), so this does not precisely answer the question related to Admob SDK, but in case anyone's getting this with Vungle or Appodeal SDK:

The versions were as follows: Appodeal SDK - 2.4.2-220518, Vungle SDK - 6.2.5. After upgrading to the at that time newest Appodeal SDK (version 2.4.9-051218), newer Vungle SDK was included (version 6.3.12) and the crash no longer occured.

Adomas
  • 466
  • 1
  • 8
  • 21
  • Are Vungle and Appodeal replacement to Admob? Why do you mention them? And, speaking about them, are they good alternatives? Do they have an app to show you how well they perform (like this of Admob: https://play.google.com/store/apps/details?id=com.google.android.apps.ads.publisher ) ? – android developer Jan 06 '19 at 18:35
  • @androiddeveloper, I mentioned Vungle and Appodeal because I had the same exception that is in this issue (was trying to figure out what's causing it), just it is related to a different ad network. Shared my findings in case someone else is having an issue like me. I think it depends on an individual situation if they're good alternatives to Admob, I'm not in a position to advise on this. But they can be used together with Admob. – Adomas Jan 06 '19 at 22:48
-1

In Android Oreo You can't change the orientation by code so you have check whenever change the orientation


  if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {

                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }