I have memory leaks regarding Interstitial ads of AdMob with AdActivity object. Whenever an ad is shown, AdActivity object count in the memory increments by 1. I inspect all changes via MAT after explicit GC calls. I use the latest versions of everything.
At first, I thought that this is related to how I implemented my UI or project, but creating a fresh and empty project shows the same leak.
This leak has also existed in the previous Admob SDK (Google Play Services) and now it exists in version 7.0 too.
I see that people try to solve these kinds of issues by creating a SingleInstance empty activity just to show and set as the context of the interstitial ads. I tried them all and they did not work for my case. Some did help but I even couldn't use it because of the flow of my app. launchMode in Android has limitations and it does not help me on my case.
I already notified the AdMob team but even they fix it, it doesn't seem to happen in a short time as they have just released the version 7.0 SDK.
I do not understand how others do not report leaks like this. It cannot be a special case just for me as it happens on even samples or default templates. If anyone somehow solved this issue (including ugly reflection hacks) please share your experience. I have been working on this for months! Really!
AndroidManifest:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="MyApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
MainActivity:
package com.example.leaktest1;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
private InterstitialAd interstitial=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interstitial= new InterstitialAd(getApplicationContext());
interstitial.setAdUnitId("YOUR-ADD-ID");
AdRequest adRequest2 = new AdRequest.Builder()
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
if(interstitial.isLoaded()){
interstitial.show();
}
}
});
interstitial.loadAd(adRequest2);
}
#Override
protected void onDestroy() {
if(interstitial!=null){
interstitial.setAdListener(null);
interstitial=null;
}
super.onDestroy();
}
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.leaktest1.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Start the app. When the ad is shown, close it with backpress and hit back button again to exit app.
App will still be in memory, but the activity is gone. Now touch the app icon to start the activity again, it will show the ad again, exit like you did before.
Cause GC multiple times and get heap dump. You will see that there are 2 AdActivity objects (and also many other related objects). It will continue to grow according to the number of shown ads.
The following did not work too (it still leaks):
/*
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
}
});*/
interstitial.loadAd(adRequest2);
Runnable r=new Runnable() {
#Override
public void run() {
if(interstitial.isLoaded()){
interstitial.show();
}
}};
new Handler().postDelayed(r,10000);
And putting code inside a button did not work too (it still leaks):
Button b = new Button(this);
b.setText("Touch me");
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(interstitial.isLoaded()){
interstitial.show();
}
}
});
ViewGroup v = (ViewGroup) this.findViewById(android.R.id.content);
v.addView(b);
Leak platform and exceptions:
This leak happens on various devices from Samsung and Asus with various un-modded (original firmware) Androis systems from 2.3 to 4.4. It also happens on any setup of android simulator.
(This leak does not show up on rooted Cyanogenmod (Galaxy S3) Android 4.4.4)
UPDATE
The leak does not vanish if I use Activity context instead of Application context. It also causes Activity leak too.
Related
Lately, my android app has been showing Admob interstitial ads that cannot be dismissed. It seems the back button action to dismiss an ad was discontinued. Most interstitial ads include a "Close" button on the ad itself, but not all of them do. When an interstitial ad is delivered without a close button, the only way to close the ad is to kill the app.
Here is an example of an ad we received without a close button:
I am using
implementation 'com.google.android.gms:play-services-ads:17.0.0'
in my Gradle build.
So the question is: Is there any way to close an interstitial ad aside from pressing the close button on the ad?
If the interstitial should be cropped so that the close button does not appear, it may be due to the max_aspect setting in your AndroidManifest.
For Interstitial the android.resizeableActivity setting has to be true.
In may application I have set the android.resizeableActivity=true in the application tag
but set my android.max_aspect to 1.8 which is needed for my game.
And only in my main acticity tag i set the android:resizeableActivity="false"
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:resizeableActivity="true"
>
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="#string/admob_App_id" />
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="#string/app_id" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data android:name="android.max_aspect" android:value="1.8" />
<activity
android:name="com.entwicklerx.macedefense.MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize"
android:resizeableActivity="false">
I hope this helps someone who has stumbled into the same problem as I recently
It seem like AdMob did some A/B testing on Back button (back button closed the Ads for some users and did not do anything for other users) but now it seems that AdMob chose the later option (back button do nothing)
I noticed this with some Ads, most probably it is an AdMob bug related to certain Ads creative sizes on some phones.
Also I've noticed that it stopped happening using newer SDK versions.
Interstitital ads can be closed by back button.
Remove all minterstitial.loadad code, and also showad code , which you call fram callback you implemented , like onadclose callback.
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-........");
// mInterstitialAd.setAdUnitId("ca-app-pub-......."); /// test ads
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener(){
#Override
public void onAdLoaded() {
super.onAdLoaded();
mInterstitialAd.show();
}
#Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
}
#Override
public void onAdClicked() {
super.onAdClicked();
}
#Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
}
#Override
public void onAdOpened() {
super.onAdOpened();
}
});
}
});
I am trying to implement Smaato Video Ads on my Android App.
I have followed the instructions from the Smaato page and the Smaato example app and yet, I cannot manage to show the Smaato Video Ads.
The code that apparently should show a video ad in fullscreen is:
private static Video smaatoVideoAd; // A Smaato Video global variable
smaatoVideoAd = new Video(this.getApplicationContext());
smaatoVideoAd.getAdSettings().setPublisherId(0); //int of 0 is supposedly a test mode
//smaatoVideoAd.getAdSettings().setPublisherId(PublisherID);
smaatoVideoAd.getAdSettings().setAdspaceId(0); //int of 0 is supposedly a test mode
//smaatoVideoAd.getAdSettings().setAdspaceId(VideoAd_ID);
smaatoVideoAd.setVastAdListener(this);
smaatoVideoAd.setAutoCloseDuration(5);
smaatoVideoAd.disableAutoClose(true);
smaatoVideoAd.setVideoSkipInterval(3);
} // End of onCreate
// Called when the video has been loaded.
#Override
public void onReadyToShow() {
// Call this when you want to show the video ad.
smaatoVideoAd.show();
}
#Override
public void onWillShow() {
// Called when the ad will show.
}
#Override
public void onWillOpenLandingPage() {
// Called when the banner has been clicked.
}
#Override
public void onWillClose() {
// Called when Interstitial ad will be closed.
}
#Override
public void onFailedToLoadAd() {
// called when video failed to load.
}
The Smaato libs for build.gradle has been setup, and that is pretty straightforward and short:
repositories {
...
flatDir {
dirs 'libs'
}
...
}
dependencies {
...
implementation name:'SOMAAndroid-9.1.3-release', ext:'aar'
...
}
And in the AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />
<meta-data android:name=”com.google.android.gms.version” android:value=”#integer/google_play_services_version”/>
<activity android:name="com.smaato.soma.interstitial.InterstitialActivity" android:resizeableActivity="false" android:screenOrientation="landscape" />
<activity android:name="com.smaato.soma.video.VASTAdActivity" android:resizeableActivity="false" android:screenOrientation="landscape" />
<activity android:name="com.smaato.soma.ExpandedBannerActivity" android:resizeableActivity="false" android:screenOrientation="landscape" />
Besides, Smaato declares that: "Since v8.0.0, the Smaato Android SDK contains an automatic initialization routine. Therefore there is no need for publishers to manually call an additional SDK initialization method".
But running this code, gives me the following alert in the Logcat:
SOMA_VIDEO: Video must be loaded before showing it
So, the video add is not loading at all. What am I missing here?
Can you help me? Thanks.
Solved! Apparently, I hadn't the correct sequence order. With this order, the warning in Logcat vanishes:
smaatoVideoAd = new Video(this.getApplicationContext());
smaatoVideoAd.setVastAdListener(this);
smaatoVideoAd.setAutoCloseDuration(3);
smaatoVideoAd.disableAutoClose(false);
smaatoVideoAd.setVideoSkipInterval(1);
smaatoVideoAd.getAdSettings().setPublisherId(0); // Trial Publisher Id: 0
smaatoVideoAd.getAdSettings().setAdspaceId(3090); // Trial Adspace Id for video: 3090
smaatoVideoAd.asyncLoadNewBanner();
Notice that the code for Smaato video ad in trial mode is 3090, not 0. Also, I have put the Smaato Ad in a new activity, so closing the ad will close the child activity and not my main one.
I've seen pictures of this elsewhere, but from some time back where the answer is generally "this is a known issue with Android 2.3" I'm using 4.4, so that's definitely not the answer.
I have about the simplest program ever: "Hello, Android". When I launch the emulator, it load up in portrait mode. Using Fn-Ctrl-F11 (Mac), the emulator rotates to landscape mode. But the application and the phone controls do not redraw - the whole thing just looks sideways.
Here's the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.helloandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.test.helloandroid.Hello"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and the Activity XML file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Hello" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
I'm building with Eclipse, the ADT bundle build v22.3.0-887826, although I can't imagine that matters for something this trivial.
My emulator is for device Galaxy Nexus, Android 4.4 API level 19. I've tried this with Hardware keyboard present marked and unmarked. I found reference to a "keyboard lid support" setting which I haven't seen anywhere - this comment is from 3/12 & so may be outdated.
This is my first Android app, so I'm a complete novice at debugging in this environment. TIA for any suggestions on what I'm missing.
EDIT: Adding code for hello.java
package com.test.helloandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Hello extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello, menu);
return true;
}
}
If you are beginner,you should go through this reference document to know about Activity lifecycle.
Here,I'm including few Log and Toast to make it easier to understand the process happens when you rotate the screen.
Example:
package com.test.helloandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Hello extends Activity {
private static final String TAG = "Hello";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
Log.d(TAG, "onCreate");
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello, menu);
return true;
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}
#Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
}
}
I hope it will be helpful !!
Apparently,everything old is new again: orientation change bug in 4.4
"The more they overthink the plumbing, the easier it is to stop up the drain."
It's good to know that I've not missed something obvious; OTOH this is a pretty obvious FAIL on the part of google's quality assurance... did bill gates sneak in there while no one was looking? Or are they greedy & trying to sell phones for testing by mucking up the emulators? Looks like there's a device in my future.
Edit :
Reference : Answered by CommonsWare,the Framework Engineer of Android.
Impossible to rotate the emulator with android 4.4
I have an app that has two activities.
The first one is presented with a single button that opens the second one.
Here is the Manifiest definition for the first one:
<activity
android:name="com.example.buttonexample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Second activity:
<activity
android:name="com.example.buttonexample.MainActivity2"
android:label="#string/title_activity_main_activity2" android:theme="#android:style/Theme.Translucent">
</activity>
Here is how I launch the second activity (via OnClickListener for a button on the first activity):
public void startSecondActivityClick(View v) {
Intent startActivity2 = new Intent(this, MainActivity2.class);
startActivity(startActivity2);
}
This works fine, however when I background the app by hitting home and the foreground the app. I'm noticing that the first activity is continually creating/destroying itself. I verified this by putting some code in the onDestory method to increment a static int:
private static int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
}
protected void onDestroy() {
super.onDestroy();
Log.i("MainActivity", String.format("Destroyed, %d", count));
}
I've also noticed that removing the translucent theme seems to fix this. My question is is there a way to translucent or something similar but also have it not restart? Also, I'm curious why this happens at all. I'm testing this on 4.0.1 ICS on a galaxy SIII.
Ok after some digging I was able to figure out why this is happening. Someone had turned on one of the developer options, "do not keep activities.". After turning this off this stopped happening. I suspect this wouldn't happen in production too often as most people probably don't have that setting on. You can find this under settings -> "developer options" on most phones.
So i've just started to use the new Sony Xperia Tablet S Small App SDK. I'm no realy noob, developed many personal apps before but this has got me stumped.
I've loaded up the Sample project in Eclipse (all correctly configured), sorted some of the errors out, compiled to my device and when I launch the Small App from the launcher at the bottom of the device, it force closes - it's the same with every sample/app that I tried making with the SDK.
I'm attached below my MainApplication.java and AndroidManifest.xml in the hope that someone may be able to see where the issue lies. Don't understand as it's created as per the book. Any help really is appreciated please.
MainApplication.java:
package com.sony.thirdtest;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.View;
import android.widget.Toast;
import com.small.sonyapptest.R;
import com.sony.smallapp.SmallAppWindow;
import com.sony.smallapp.SmallApplication;
public class MainApplication extends SmallApplication {
private Configuration mConfig;
#Override
public void onCreate() {
super.onCreate();
mConfig = new Configuration(getResources().getConfiguration());
setContentView(R.layout.activity_main);
setTitle(R.string.app_name);
SmallAppWindow.Attributes attr = getWindow().getAttributes();
attr.minWidth = 200;
attr.minHeight = 200;
attr.width = 400;
attr.height = 300;
attr.flags |= SmallAppWindow.Attributes.FLAG_RESIZABLE;
getWindow().setAttributes(attr);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainApplication.this, R.string.hello, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onStop() {
super.onStop();
}
#Override
protected boolean onSmallAppConfigurationChanged(Configuration newConfig) {
int diff = newConfig.diff(mConfig);
mConfig = new Configuration(getResources().getConfiguration());
// Avoid application from restarting when orientation changed
if ((diff & ActivityInfo.CONFIG_ORIENTATION) != 0) {
return true;
}
return super.onSmallAppConfigurationChanged(newConfig);
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.small.sonyapptest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="com.sony.smallapp.permission.SMALLAPP" />
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<uses-library android:name="com.sony.smallapp.framework" />
<service
android:name="com.small.sonyapptest.MainApplication"
android:exported="true" >
<intent-filter>
<action android:name="com.sony.smallapp.intent.action.MAIN" />
<category
android:name="com.sony.smallapp.intent.category.LAUNCHER" />
</intent-filter>
</service>
</application>
</manifest>
It seems that there is nothing wrong with your code. So I think the problem is with how you build the project. More exactly, how the small apps framework is included: it shouldn't be!
If you simply add the jar (from the Sony SDK) via "Java build path" -> "Add external Jar", then the classes of the api jar will be included with the application. The problem is those are only stub classes, so you can get one of two possible exceptions.
A simple and quick way to get around this (and still using the standard android sdk, and not switch to the Sony SDK) is the following:
Create a java project, and call it "SmallAppApi" for example
Inside the java project add the small app jar via "Add external jar"
In the last tab in the "Java build path" screen, called "Order and Export" make sure the small app jar is exported.
In the android project, in the "Java build path" screen, in "Projects" tab simply add the java project SmallAppApi (and remove the small app jar).
With this setup the small app jar will be used only when building. This worked fine for me.
In the service tag change android:name="com.small.sonyapptest.MainApplication" to android:name="MainApplication"
Thanks for the responses, all working now after chaning the service tag to just MainApplication.