user1026605
user1026605

Reputation: 1430

Android: NoClassDefFoundError android.os.AsyncTask

Since a couple of weeks, I'm seeing more and more crashes of my app with the following exception

Fatal Exception: java.lang.NoClassDefFoundError android.os.AsyncTask

This code has run for month without any issue, and it seems now to fail on some devices (75% android 2.3.x and 25% android 4.0.3) It fails when I create a new instance of a class which extends AsyncTask.

I create this class from the UI thread.

How can that class be not found as it's defined within the SDK ?

Upvotes: 15

Views: 5698

Answers (5)

subas
subas

Reputation: 56

I experienced same error on android 2.3.3, but same app was stable on 4.0+. It's a Freemium and the error occurs only when in FREE mode, which runs Google Admob adverts. So the error has to be connected with this but I do no have the detail. Here is how I solved the problem:

Execute a statement that would cause the AsyncTask class to be loaded before loading the ads.

steps 1: Create a dummy AsyncTask extension class

public class DummyAsyncTask extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        return null;
    }
}

step 2: just in your main activity:

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        new DummyAsyncTask();
        .
        .some code
        .
        load your ads here
    }
}

After step 2 above, all other code section that instantiates AsyncTask extended class run normally.

Upvotes: 0

William
William

Reputation: 20196

Yes, looks like it is a problem with one of the versions of Google play Services. See https://code.google.com/p/android/issues/detail?id=81083

A work around is to add:

try {
      Class.forName("android.os.AsyncTask");
}
catch(Throwable ignore) {
      // ignored
}

into your Application#onCreate()

this appears to ensure that the root classloader loads AsyncTask so that it is then available from within Play Services.

Upvotes: 12

user1026605
user1026605

Reputation: 1430

It looks like yet another Google Play Services bug...

https://groups.google.com/forum/#!topic/google-admob-ads-sdk/_x12qmjWI7M

Edit: confirmed by Google staff => https://groups.google.com/d/msg/google-admob-ads-sdk/_x12qmjWI7M/9ZQs-v0ZZTMJ

Upvotes: 7

Gillis Haasnoot
Gillis Haasnoot

Reputation: 2278

Same issue here. I see them for 95% of the cases on android 4.0.3 devices. remaining 5% for 2.3 devices

Errors are randomly occurring from different parts of the code. Some examples:

   java.lang.NoClassDefFoundError: android/os/AsyncTask
   at android.webkit.WebView.setupPackageListener(WebView.java:1305)
   at android.webkit.WebView.<init>(WebView.java:1176)
   at android.webkit.WebView.<init>(WebView.java:1136)

and

   java.lang.NoClassDefFoundError: android/os/AsyncTask
   at android.webkit.WebView.setupPackageListener(WebView.java:1354)
   at android.webkit.WebView.access$10900(WebView.java:363)
   at android.webkit.WebView$PrivateHandler.handleMessage(WebView.java:10411)

and

   java.lang.NoClassDefFoundError: android.os.AsyncTask
   at android.webkit.WebView.setupPackageListener(WebView.java:1385)
   at android.webkit.WebView.<init>(WebView.java:1192)
   at android.webkit.WebView.<init>(WebView.java:1150)
   at android.webkit.WebView.<init>(WebView.java:1135)
   at android.webkit.WebView.<init>(WebView.java:1106)
   at android.webkit.WebView.<init>(WebView.java:1093)
   at com.google.android.gms.ads.internal.util.g.f(SourceFile:400)
   at com.google.android.gms.ads.internal.util.g.a(SourceFile:385)

it is completely unclear why these errors are happening. usually i dont see anything in the stacktrace pointing to my code.

Upvotes: 1

Jados Games
Jados Games

Reputation: 1

I have the same error:

BuscaDatosJugador().execute(participante.getIconImageUrl(),String.valueOf(altoenvio), String.valueOf(contador));

My solution:

final Runnable r = new Runnable()
{
    public void run() 
    {
        try {
             --- my code ---
        }
    };

    r.run();
}

Upvotes: 0

Related Questions