Android Question GetStaticField Error

wineos

Member
Licensed User
Hi,

Is there anyone who can tell me how to fix the error?

B4X:
Logger connected to:  asus Nexus 7
--------- beginning of main
Copying updated assets files (2)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 108 (Main)
java.lang.IllegalArgumentException: java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c074079:ASN.1 encoding routines:ASN1_get_object:HEADER_TOO_LONG
    at com.android.vending.licensing.LicenseChecker.generatePublicKey(LicenseChecker.java:121)
    at com.android.vending.licensing.LicenseChecker.<init>(LicenseChecker.java:92)
    at anywheresoftware.b4a.objects.LicenseCheckerWrapper.Initialize(LicenseCheckerWrapper.java:42)
    at net.wineos.android.counsellor.main._activity_create(main.java:428)
    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:144)
    at net.wineos.android.counsellor.main.afterFirstLayout(main.java:105)
    at net.wineos.android.counsellor.main.access$000(main.java:17)
    at net.wineos.android.counsellor.main$WaitForLayout.run(main.java:83)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c074079:ASN.1 encoding routines:ASN1_get_object:HEADER_TOO_LONG
    at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:250)
    at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:47)
    at java.security.KeyFactory.generatePublic(KeyFactory.java:172)
    at com.android.vending.licensing.LicenseChecker.generatePublicKey(LicenseChecker.java:112)
    ... 19 more
Caused by: java.lang.RuntimeException: error:0c074079:ASN.1 encoding routines:ASN1_get_object:HEADER_TOO_LONG
    at com.android.org.conscrypt.NativeCrypto.d2i_PUBKEY(Native Method)
    at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:248)
    ... 22 more
** Activity (main) Resume **
Here is my code:

B4X:
Private Sub GetDeviceId() As String

    Dim r As Reflector
    Dim Api As Int
  
    Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
  
    If Api < 9 Then
        'Old device
        If File.Exists(File.DirInternal, "__id") Then
            Return File.ReadString(File.DirInternal, "__id")
        Else
            Dim id As Int
            id = Rnd(0x10000000, 0x7FFFFFFF)
            File.WriteString(File.DirInternal, "__id", id)
            Return id
        End If
    Else
        'New device
        Return r.GetStaticField("android.os.Build", "SERIAL")
    End If
  
End Sub
 

stevel05

Expert
Licensed User
Not sure what you are trying to do, but on first look you haven't set the target for the reflector object.
 

wineos

Member
Licensed User
Thank you stevel05. I post the full code here.

B4X:
Sub Activity_Create(FirstTime As Boolean)

    Dim lc As LicenseChecker
    
    Activity.LoadLayout("main")
        
    lc.Initialize("lc", GetDeviceId, publicKey, encryptKey.GetBytes("UTF8"))
    lc.SetVariableAndValue("mindful_counsellor", successKey)
    lc.CheckAccess
    
End Sub


Private Sub GetDeviceId() As String

    Dim r As Reflector
    Dim Api As Int
  
    Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
  
    If Api < 9 Then
        'Old device
        If File.Exists(File.DirInternal, "__id") Then
            Return File.ReadString(File.DirInternal, "__id")
        Else
            Dim id As Int
            id = Rnd(0x10000000, 0x7FFFFFFF)
            File.WriteString(File.DirInternal, "__id", id)
            Return id
        End If
    Else
        'New device
        Return r.GetStaticField("android.os.Build", "SERIAL")
    End If
  
End Sub

Sub lc_Allow()
    
End Sub

Sub lc_DontAllow()
    
    ToastMessageShow("未經許可使用,應用程式終止!", True)
    'Activity.Finish
    
End Sub

Sub lc_Error(ErrorCode As String)

    ToastMessageShow(ErrorCode & " : " & "程式錯誤,應用程式終止!", True)
    'Activity.Finish
    
End Sub
 

stevel05

Expert
Licensed User
B4X:
 Dim r As Reflector
    Dim Api As Int
    r.Target = ?          '<- you are missing this assignment
    Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
 

stevel05

Expert
Licensed User
you need to tell the reflector where to get the static field from.
 

stevel05

Expert
Licensed User
Sorry my mistake, code looks OK and may not be related.
 

stevel05

Expert
Licensed User
Try
B4X:
Api = r.GetStaticField("android.os.Build.VERSION", "SDK_INT")
 

stevel05

Expert
Licensed User
I just had to set up my test environment, clean install of windows.

The get deviceID sub works OK for me as you had it but I cant test the licence checker, but it appears that is where the issue is.
 

stevel05

Expert
Licensed User
No sorry, I've never used it. I'm sure someone will have an idea.
 

udg

Expert
Licensed User
Could be because "SERIAL" is deprecated since Api 26?
 
Top