Android Question java.lang.ClassNotFoundException: de.xxxxxxxxxxxxx.clsinappbilling$_applizenz

Filippo

Expert
Licensed User
Longtime User
Hi,

I have a very strange error.

If I change the name of a classe, from clsinappbilling.bas to clsinappbilling3.bas, then the sub "ReadMapAsB4XObject" (the procedure is in a general module "mAllg") cannot read a file anymore, although the classe has not changed.
Where can the problem be?

The name "applizenz" is a type in the classe.
B4X:
    Type Applizenz(PurchaseTime As Long, PurchaseToken As String, Sku As String)

Modul mAllg:
B4X:
Public Sub ReadMapAsB4XObject(sDir As String, sFilename As String) As Map
    Dim raf As RandomAccessFile
    raf.Initialize2(sDir, sFilename, False, True)
    Dim mp As Map = raf.ReadB4XObject(raf.CurrentPosition)
    raf.Close
    Return mp
End Sub

In the classe the file is read with this procedure:
B4X:
Private Sub GetOwnedProductsOffline
    If File.Exists(File.DirInternal,"lizenz.mp") Then
        'Der Lizenz-File wird als B4XObject gespeichert
        ReadLizenzFile
    End If
End Sub

Private Sub ReadLizenzFile
    Dim lizenz As Applizenz
    Dim mp As Map
    mp.Initialize

   mp = mAllg.ReadMapAsB4XObject(File.DirInternal, "lizenz.mp")
End If

Error:
*** Service (starter) Create ***
Error occurred on line: 890 (mAllg)
java.lang.RuntimeException: java.lang.ClassNotFoundException: fg.cronomillemiglia.clsinappbilling$_applizenz
at anywheresoftware.b4a.randomaccessfile.B4XSerializator.readType(B4XSerializator.java:314)
at anywheresoftware.b4a.randomaccessfile.B4XSerializator.readObject(B4XSerializator.java:374)
at anywheresoftware.b4a.randomaccessfile.B4XSerializator.readMap(B4XSerializator.java:248)
at anywheresoftware.b4a.randomaccessfile.B4XSerializator.readObject(B4XSerializator.java:364)
at anywheresoftware.b4a.randomaccessfile.B4XSerializator.ReadObject(B4XSerializator.java:129)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.ReadB4XObject(RandomAccessFile.java:340)
at fg.cronomillemiglia.mallg._readmapasb4xobject(mallg.java:2211)
at fg.cronomillemiglia.clsinappbilling3._readlizenzfile(clsinappbilling3.java:1554)
at fg.cronomillemiglia.clsinappbilling3._getownedproductsoffline(clsinappbilling3.java:1524)
at fg.cronomillemiglia.clsinappbilling3._initialize(clsinappbilling3.java:440)
at fg.cronomillemiglia.starter._service_create(starter.java:1504)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at fg.cronomillemiglia.starter.onCreate(starter.java:56)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3342)
at android.app.ActivityThread.-wrap4(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1680)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: fg.cronomillemiglia.clsinappbilling$_applizenz
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:453)
at java.lang.Class.forName(Class.java:378)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readTypeClass(RandomAccessFile.java:583)
at anywheresoftware.b4a.randomaccessfile.B4XSerializator.readType(B4XSerializator.java:291)
... 27 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "fg.cronomillemiglia.clsinappbilling$_applizenz" on path: DexPathList[[zip file "/data/app/fg.cronomillemiglia-sVbtZAWHHM9Wa59Pn9In2Q==/base.apk"],nativeLibraryDirectories=[/data/app/fg.cronomillemiglia-sVbtZAWHHM9Wa59Pn9In2Q==/lib/arm64, /system/lib64, /system/vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 32 more
** Service (starter) Start **
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Filippo

Expert
Licensed User
Longtime User
Have you tried cleaning the project? It looks like something doesn't know about the name change.
Yes, several times already.
The compiler simply does not find the type "applizenz" anymore, although it is always in the same classe.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Ah! You are reading an object from a file that is already saved. The type of the saved object in the file is using the old class name as that is what it was saved as. You need to rewrite the file with the new class name. The class name is part of the type name.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
Ah! You are reading an object from a file that is already saved. The type of the saved object in the file is using the old class name as that is what it was saved as. You need to rewrite the file with the new class name. The class name is part of the type name.
Ok. That's what I thought, but I couldn't imagine the class name being stored in the file.

Isn't that an error?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Isn't that an error?
No. That's the way it works. You can have the same type name in different classes The actual type name includes its class name as that's how it works in Java

EDIT: Wrong again! I'm being too hasty tonight :( I think I am right but try rewriting the file with the new class object and see if it fixes it.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
EDIT: Wrong again! I'm being too hasty tonight :(
:D

but try rewriting the file with the new class object and see if it fixes it.
Unfortunately, this does not work for the user.
I have to open the file first and check if a license is available.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
I have changed the classe name because of versioning.
Since the name change causes problems, so I created a b4xlib library from the classe.
Here you can change the version without problems.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip: in the future you should use B4XSerializator directly:
B4X:
Public Sub ReadMapAsB4XObject(sDir As String, sFilename As String) As Map
    Dim ser 
    Return ser.ConvertObjectToBytes(File.ReadBytes(sDir, sFilename)
End Sub
The file format is a slightly different than RAF.WriteB4XObject / ReadB4XObject so you cannot change now.

The type name with the enclosing class is saved. Types declared in Main module or B4XMainPage can be used in apps with different package names.
 
Upvote 0

Ronomega73

Member
Licensed User
Longtime User
Hello! I'm not sure if this is still unsolved but I have just experienced the same problem while trying to fix another unrelated one. The Class Not Found error showed when I tried to transfer the type declaration from one module to the main activity module. When I moved back the type declaration to where it originally was the error went away. It seems that when you have already created an object of the same type and then later on move that type declaration somewhere else, using/referencing the said object will generate the Class Not Found error.
 
Upvote 0
Top