Android Question Fatal Exception: java.lang.NoClassDefFoundError android.os.AsyncTask and a fix

skaliwag

Member
Licensed User
Longtime User
This error can appear in apps that have been happily running fine, and seems to be due to an update to Google Play Services.
It is most common in Android versions 2.3.x but also seems to occur on HTC devices with version 4.x.x
Usually it is seen when using Admobs, but in my case it was triggered by the excellent
Facebook SDK Wrapper + Native Facebook LoginButton by Periklis, which uses AsyncTask.
The error seems to occur the first time an AsyncTask is created.
There are lots of discussions of this on the Android forums.

Fortunately the fix is quite simple.

Using the Reflector library simply add this to Activity_Create
B4A will warn you about an empty Catch
B4X:
Dim Obj2 As Reflector
Try
    Obj2.Target = Obj2.CreateObject("android.os.AsyncTask")
Catch
End Try[/SIZE]

Alternatively, If using Version 4.30 or above of B4A, you could use the JavaObject library instead
B4X:
Sub Process_Globals
  Private NativeMe As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      NativeMe.InitializeContext
   End If
   NativeMe.RunMethod("jAsyncFix", Null)
End Sub

#If JAVA
public void jAsyncFix()
{
   try
   {
  Class.forName("android.os.AsyncTask");
   }
   catch(Throwable ignore)
   {
   }
}
#End If
[/SIZE]
 
Top