Java Question [SOLVED] Loading a .so file

Johan Schoeman

Expert
Licensed User
Longtime User
I am doing a shortcut wrap for someone to see if the demo activity is working on a PDA401 device. I have worked with .so files before so my SLC structure looks as follows (same as with other projects that I have done):

401DEMO
additional
lib
armeabi
libzyapi_common.so​
libs
android-support-v4.jar​
src
android
........​
com
........​
pda401wrapper
pda401Wrapper.java
libzyapi_common.so is loaded in:
B4X:
package android.zyapi;


public class CommonApi {
    private static CommonApi mMe = null;
   
    public CommonApi() {
    }
   
    // gpio
    public native int setGpioMode(int pin, int mode); 
    public native int setGpioDir(int pin, int dir);
    public native int setGpioPullEnable(int pin, int enable);
    public native int setGpioPullSelect(int pin, int select);
    public native int setGpioOut(int pin, int out);
    public native int getGpioIn(int pin);
    //serialport 
    public native int openCom(String port, int baudrate,int bits,char event,int stop);
    public native int openComEx(String port, int baudrate,int bits,char event,int stop, int flags);
    public native int writeCom(int fd, byte[] buf, int sizes);
    public native int readCom(int fd, byte[] buf, int sizes);
    public native int readComEx(int fd, byte[] buf, int sizes, int sec, int usec);
    public native void closeCom(int fd);
   
    static { 
        System.loadLibrary("zyapi_common");
    }
   
}

It compiles without any problem but when I "kick start" the external Demo Activity I get the following error:
B4X:
java.lang.UnsatisfiedLinkError: JNI_ERR returned from JNI_OnLoad in "/data/app/JHS.PDA401-2/lib/arm/libzyapi_common.so"
    at java.lang.Runtime.loadLibrary(Runtime.java:371)
    at java.lang.System.loadLibrary(System.java:988)
    at android.zyapi.CommonApi.<clinit>(CommonApi.java:26)
    at com.qs.service.ScanService.initGPIO(ScanService.java:172)
    at com.qs.service.ScanService.onCreate(ScanService.java:41)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3001)
    at android.app.ActivityThread.access$1800(ActivityThread.java:178)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1553)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5643)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

So, it looks like it is looking for the .so file in the "arm" folder while no such folder exists in the SDK that I have downloaded. Only folder armeabi which contains libzyapi_common.so exits in the SDK that I have downloaded.

Can anyone help to please solve this mystery?
 

MarcoRome

Expert
Licensed User
Longtime User
I am doing a shortcut wrap for someone to see if the demo activity is working on a PDA401 device. I have worked with .so files before so my SLC structure looks as follows (same as with other projects that I have done):

401DEMO
additional
lib
armeabi
libzyapi_common.so​
libs
android-support-v4.jar​
src
android
........​
com
........​
pda401wrapper
pda401Wrapper.java
libzyapi_common.so is loaded in:
B4X:
package android.zyapi;


public class CommonApi {
    private static CommonApi mMe = null;
  
    public CommonApi() {
    }
  
    // gpio
    public native int setGpioMode(int pin, int mode);
    public native int setGpioDir(int pin, int dir);
    public native int setGpioPullEnable(int pin, int enable);
    public native int setGpioPullSelect(int pin, int select);
    public native int setGpioOut(int pin, int out);
    public native int getGpioIn(int pin);
    //serialport
    public native int openCom(String port, int baudrate,int bits,char event,int stop);
    public native int openComEx(String port, int baudrate,int bits,char event,int stop, int flags);
    public native int writeCom(int fd, byte[] buf, int sizes);
    public native int readCom(int fd, byte[] buf, int sizes);
    public native int readComEx(int fd, byte[] buf, int sizes, int sec, int usec);
    public native void closeCom(int fd);
  
    static {
        System.loadLibrary("zyapi_common");
    }
  
}

It compiles without any problem but when I "kick start" the external Demo Activity I get the following error:
B4X:
java.lang.UnsatisfiedLinkError: JNI_ERR returned from JNI_OnLoad in "/data/app/JHS.PDA401-2/lib/arm/libzyapi_common.so"
    at java.lang.Runtime.loadLibrary(Runtime.java:371)
    at java.lang.System.loadLibrary(System.java:988)
    at android.zyapi.CommonApi.<clinit>(CommonApi.java:26)
    at com.qs.service.ScanService.initGPIO(ScanService.java:172)
    at com.qs.service.ScanService.onCreate(ScanService.java:41)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3001)
    at android.app.ActivityThread.access$1800(ActivityThread.java:178)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1553)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5643)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

So, it looks like it is looking for the .so file in the "arm" folder while no such folder exists in the SDK that I have downloaded. Only folder armeabi which contains libzyapi_common.so exits in the SDK that I have downloaded.

Can anyone help to please solve this mystery?

Do you tried with:

B4X:
static {
        System.loadLibrary("libzyapi_common");
    }
 

JordiCP

Expert
Licensed User
Longtime User
Try to use
B4X:
System.load( full_path_to_lib)
to load libs from wherever you want if you know where they should be. I have some experiments where I added it to my assets folder, copied it to File.Dirinternal and then loaded it from there

System.loadlibrary seems to use the 'predefined' path, or a list of paths to check, to where libraries should be, but can be implementation dependent, or I (don't know) if it can also can be forced from code.
 

Johan Schoeman

Expert
Licensed User
Longtime User
1. Do you get the same error on a standard Android device?
2. Have you tried to add an arm folder with the same so file?
Erel, I only have a standard Android device to test it on (I have also done so with a PDA3505 and it worked on a standard android device - in this case it is a PDA401 but does not work). Have already tried to add a arm folder with the same so file in it but the same error occurs.

I will try @JordiCP suggestion a bit later today. I seem to recall that the armeabi folder was a sub folder of the libs folder (not of the additional folder).
 

Johan Schoeman

Expert
Licensed User
Longtime User
Try to use
B4X:
System.load( full_path_to_lib)
to load libs from wherever you want if you know where they should be. I have some experiments where I added it to my assets folder, copied it to File.Dirinternal and then loaded it from there

System.loadlibrary seems to use the 'predefined' path, or a list of paths to check, to where libraries should be, but can be implementation dependent, or I (don't know) if it can also can be forced from code.
Tonight the first line of the error looks different.... even when reverting back to the original setup. So where is "/data/app-lib/JHS.PDA401-19/" hiding?
B4X:
java.lang.UnsatisfiedLinkError: JNI_ERR returned from JNI_OnLoad in "/data/app-lib/JHS.PDA401-19/libzyapi_common.so"

I have no tried every possible permutation and combination that I can think of including adding these folders (all containing the .so file) inside the additional/lib/ folder.
upload_2017-7-3_20-25-25.png
 

JordiCP

Expert
Licensed User
Longtime User
No idea of what can it be o_O

Perhaps testing the original example in your device (and the generated apk structure) can be a good idea
 

MarcoRome

Expert
Licensed User
Longtime User
upload_2017-7-3_22-9-42.png



In Apk to zip

upload_2017-7-3_22-11-36.png

upload_2017-7-3_22-12-34.png


upload_2017-7-3_22-13-24.png
 

Attachments

  • upload_2017-7-3_22-9-9.png
    upload_2017-7-3_22-9-9.png
    52.8 KB · Views: 392
  • upload_2017-7-3_22-12-8.png
    upload_2017-7-3_22-12-8.png
    3.3 KB · Views: 376

JordiCP

Expert
Licensed User
Longtime User
I have tested the 401demo APK in my device and gives the same error.

Screenshot_2017-07-03-23-45-47-175_com.miui.bugreport.png

perhaps it is hardware dependent, I don't know
 

Johan Schoeman

Expert
Licensed User
Longtime User

Klaas

Member
Licensed User
It is working on the target device....! Thanks for everyone's input!
Hi Johan,
I see your message post from last year.
My problem is the same as your one as in picture below.
How did you fix this problem?
Tried everything you tried as in your post.
 

Attachments

  • error1.gif
    error1.gif
    92.8 KB · Views: 344

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan,
I see your message post from last year.
My problem is the same as your one as in picture below.
How did you fix this problem?
Tried everything you tried as in your post.
I have not done anything special to fix it - it does not run on a standard android device but on the actual PDA401 (different hardware) it worked.
 
Top