Jump to content
KimHJ

Android aidl error when using it.

Recommended Posts

I create a JavaInterface file using java2op, but I have problems when I use it. I have attached the JavaInterface.pas

I get this error when it execute printText.

First chance exception at $8689BB93. Exception class EJNIFatal with message 'Java type recieptservice/com/recieptservice/PrinterInterface could not be found'. Process ComcaH10.apk (6409)

 

What is missing?

 

Tanks for any help.

 

uses
JavaInterfaces;

procedure TMainForm.PrintThis;
 var
 Myprinter: JPrinterInterface;
 begin

      MyPrinter := TJPrinterInterface.Create;

      MyPrinter.printText(StringToJString('This Is A Test'));
      MyPrinter.endWork;

 end;

 

JavaInterfaces.pas

Share this post


Link to post
1 minute ago, KimHJ said:

Exception class EJNIFatal with message 'Java type recieptservice/com/recieptservice/PrinterInterface could not be found'

That usually means you have not added the required .jar file (the one you ran Java2OP on) to the project. In Project Manager:

  1. Expand the Android 32-bit target
  2. Right-click on Libraries
  3. Click Add..
  4. Select the .jar file
  5. Click OK

Note that the .jar will be compiled in regardless of whether you compile for 32-bit or 64-bit.

Share this post


Link to post

You are right, I didn't know I had to include the jar file.

 

I have a aidl and a jar, I made the JavaInterface.pas from the aidl, do I need to add that as well?

 

Thanks.

Edited by KimHJ

Share this post


Link to post

I added the jar file, I did not see anything about adding the aidl in Embarcadero's website about jar files.

Now I get an exception class 6 in Andoidapi.JNIBridge in class procedure TBridgeHelper.Alloc

Could this be because I used the aidl file to create the JavaInterfaces.pas?

Share this post


Link to post
1 hour ago, KimHJ said:

I did not see anything about adding the aidl in Embarcadero's website about jar files.

That's because you don't need to.

1 hour ago, KimHJ said:

Could this be because I used the aidl file to create the JavaInterfaces.pas?

Possibly - the Java signatures in that file look a bit odd. Is the .jar file available publicly?

Share this post


Link to post

Now I get exception 6 in the create line.

Do I call the wrong class?

procedure TMainForm.PrintThis(MyPrint:String);
 var
 Myprinter: JPrinterInterface;
 begin

      MyPrinter := TJPrinterInterface.Create;
      MyPrinter.beginWork;
      MyPrinter.printText(StringToJString('This Is A Test'));
      MyPrinter.endWork;

 end;

 

JavaInterfaces.pas

Share this post


Link to post
2 minutes ago, KimHJ said:

Now I get exception 6 in the create line.

Do I call the wrong class?

Possibly, but it's hard to tell without access to the .jar. You have not answered this:

On 11/16/2024 at 3:33 PM, Dave Nottage said:

Is the .jar file available publicly?

If it is public, where can it be obtained from?

Share this post


Link to post
6 minutes ago, KimHJ said:

I have attached it here.

Thanks. It appears you should use TJPrinterInterface_Default, rather than TJPrinterInterface. 

Share this post


Link to post

Thanks, It worked I don't get any errors.

Now I just have to find out why it's not printing.

 

When I look at the Android example code it looks like they jus send the command printText

 MainActivity.this.findViewById(R.id.test2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    SrPrinter.getInstance(getApplicationContext()).printText(((EditText) findViewById(R.id.test1)).getText().toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

Here are the the first part of the SrPrinter.class

 

package com.sr;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.os.RemoteException;
import java.util.Deque;
import java.util.concurrent.LinkedBlockingDeque;
import recieptservice.com.recieptservice.PSAMCallback;
import recieptservice.com.recieptservice.PrinterInterface;
import recieptservice.com.recieptservice.PrinterInterface.Stub;

public class SrPrinter {
    static SrPrinter srPrinter;
    static Deque<Runnable> deque;
    static volatile PrinterInterface printerInterface = null;
    static boolean reconnect = false;
    static ServiceConnection serviceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            SrPrinter.printerInterface = Stub.asInterface(iBinder);
            synchronized(SrPrinter.deque) {
                while(SrPrinter.printerInterface != null && !SrPrinter.deque.isEmpty()) {
                    ((Runnable)SrPrinter.deque.poll()).run();
                }

            }
        }

        public void onServiceDisconnected(ComponentName componentName) {
            SrPrinter.printerInterface = null;
            SrPrinter.reconnect = true;
        }
    };

    public SrPrinter() {
    }

    public static synchronized SrPrinter getInstance(Context context) {
        Intent intent;
        if (srPrinter != null) {
            if (reconnect) {
                intent = new Intent();
                intent.setClassName("recieptservice.com.recieptservice", "recieptservice.com.recieptservice.service.PrinterService");
                context.startService(intent);
                context.bindService(intent, serviceConnection, 1);
                reconnect = false;
            }

            return srPrinter;
        } else {
            srPrinter = new SrPrinter();
            deque = new LinkedBlockingDeque();
            intent = new Intent();
            intent.setClassName("recieptservice.com.recieptservice", "recieptservice.com.recieptservice.service.PrinterService");
            context.startService(intent);
            context.bindService(intent, serviceConnection, 1);
            return srPrinter;
        }
    }

 

Share this post


Link to post
9 minutes ago, KimHJ said:

intent.setClassName("recieptservice.com.recieptservice", "recieptservice.com.recieptservice.service.PrinterService");

I do not see the recieptservice.com.recieptservice.service.PrinterService class anywhere in the .jar file you attached. Did you compile the .jar yourself? 

12 minutes ago, KimHJ said:

When I look at the Android example code..

Where is this example code?

Share this post


Link to post
3 hours ago, KimHJ said:

The SDK is 18mb here is my dropbox link.

The file printer.jar in the SDK appears to be a cut-down version (not sure why they provide it in the first place - it might be in error) of the one you actually need to use, which is inside printer-release.aar. If you have Delphi 12.2, you can add this file to the Libraries node under Android 32-bit target in Project Manager instead of printer.jar. 

 

This library includes the required methods of SrPrinter (e.g. getInstance), so you should be able to use this function to get an instance of it:

uses
  Androidapi.Helpers;

function SrPrinter: JSrPrinter;
begin
  Result := TJSrPrinter.JavaClass.getInstance(TAndroidHelper.Context);
end;

..and call the functions like they do in the Java examples, e.g.:

SrPrinter.printQRCode(StringToJString('123456'), 4, 3);

However you will need to first:

  1. Rename printer-release.aar to printer-release.zip
  2. Extract the classes.jar file from the .zip file
  3. Run Java2OP on the extracted classes.jar to recreate your JavaInterfaces.pas import.

 

Edited by Dave Nottage
  • Thanks 1

Share this post


Link to post

Thanks a lot for your help, I would never had been able to find out this by my self. I was almost going back to Android Studio and make the app there.

 

Share this post


Link to post

Sorry, I still have problems when compiling the new JavaInterfaces.pas I get those errors all the way down from JSrPrinter_10

[JavaSignature('com/sr/SrPrinter$10')]
  JSrPrinter_10 = interface(JRunnable)
    ['{A13C6C9C-B945-4438-A33F-1B4453FD0555}']
    function _Getval$line: Integer; cdecl;			//function needs result type. With a red line under the $ sign
    procedure run; cdecl;
    property val$line: Integer read _Getval$line;	// property 'val' does not exsist in base class. With a red line under val($line), read and $ sign in Getval$text
  end;
  TJSrPrinter_10 = class(TJavaGenericImport<JSrPrinter_10Class, JSrPrinter_10>) end;

Does it looks like Java2Op created some wrong classes?

 Is it something i can correct manually?

 

Again, thanks for any help.

 

JavaInterfaces.pas

Share this post


Link to post
11 minutes ago, KimHJ said:

Does it looks like Java2Op created some wrong classes?

 Is it something i can correct manually?

Yes - you can safely remove any declarations that have a signature that ends with $ and a number (and remove the corresponding xxxClass declarations), as these are anonymous inner classes.

 

  • Thanks 1

Share this post


Link to post
6 minutes ago, KimHJ said:

Great, that did it.

Good to hear, but does that mean it now compiles, or that your app is working also? 🙂

 

Share this post


Link to post

Yes, it compiles and it prints.

First It didn't print, but then I added the command Nextline(1) first and PrintText after then it printed.

Now I just need to create the rest of the app.

I'm working on two different apps at the same time as you can see on my posts, not getting enough sleep lately.

Again, thanks for all your help.

 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×