15

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 ?

user1026605
  • 1,633
  • 4
  • 22
  • 58
  • 3
    would you be kind enough to share your code? – Sarthak Mittal Nov 25 '14 at 07:59
  • This isn't related to the code. I got those random crashes from different part of my code which haven't been updated for month. This isn't a code issue, maybe a project / build issue, but I don't see how a NoClassDefFoundError on AsyncTask can be possible... – user1026605 Nov 25 '14 at 08:01
  • if you are using eclipse, Right click your project androidtools>addsupport lib if it didn't work try fix project properties. This is my guess. please provide logcat for further investigation. – Amith Nov 25 '14 at 08:03
  • I don't have any logcat because 99.99% of my users don't have this issue. The app build fine and work fine on most devices, but I just started to see those errors for the past weeks so I'm wondering where they can come from. The AsyncTask is part of the Android SDK, so I don't understand why adding the support library will fix anything... – user1026605 Nov 25 '14 at 08:20
  • It may be an OOM issue. Try adding `android:largeHeap="true"` and turn hardware acceleration off. – Ali Behzadian Nejad Nov 25 '14 at 08:25
  • AsyncTask was introduced in api level 3. Any chance your apps minimum sdk level is below that? – Amith Nov 25 '14 at 08:31
  • My minSDK target is 9. As xplain in my post I only see those errors on devices running Android 2.3.x and 4.0.3 – user1026605 Nov 25 '14 at 08:35
  • @Ali Behzadian Nejad Turning largeHeap isn't a good solution to fix OOM issues. The app will be more likely to be killed by Android if it uses this flag in case the device needs to free some ressources. – user1026605 Nov 25 '14 at 08:37
  • Is it possible that in a very few cases, your asynctask is not created in the main thread ? – ToYonos Nov 25 '14 at 11:31
  • No they're created from the main thread (Activity, service onCreate function). Also it would crash on every device it it were created from a background thread – user1026605 Nov 25 '14 at 15:55
  • 2
    I have started to get these too. So fr all from 2.3.x and different models/brands. I wonder if it would be related to some sort of Google Play services update? – nLL Nov 26 '14 at 15:17
  • 1
    I am also facing same issue. http://stackoverflow.com/questions/27167525/noclassdeffounderror-android-2-3-x – Shakti Malik Nov 27 '14 at 09:52
  • 1
    @Shakti Malik sorry I just posted on your issue as well ;) It definitely looks like a Google Play Services issue – user1026605 Nov 27 '14 at 12:00
  • I get this too in Android lower than 5.0. But in 5.0 works perfectly. – SleepNot Feb 13 '15 at 08:33
  • It was a Google Play Services bug which is now fixed. There's a simple workaround. Called the following code at the entry point of your application (Application.onCreate()) try { Class.forName("android.os.AsyncTask"); } catch(Throwable ignore) { // ignored } – user1026605 Feb 13 '15 at 09:46

5 Answers5

12

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.

William
  • 20,150
  • 8
  • 49
  • 91
7

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

user1026605
  • 1,633
  • 4
  • 22
  • 58
1

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.

Gillis Haasnoot
  • 2,229
  • 1
  • 19
  • 23
0

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();
}
  • 1
    This is a Google Play Services bug. I don't want to update my code as a workaround. I want to keep using asynctasks, which allows me to easily update the UI – user1026605 Nov 29 '14 at 08:53
0

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.

subas
  • 56
  • 1
  • 3