Pankaj
Pankaj

Reputation: 81

How to use libusb.so library functions in C or JAVA coding after library is successfully loaded?

I am trying to use libusb.so How can I make use of libusb library in my android project.

I have compiled libusb library for my android project using libusb source.Now I want to use the functions of newly generated library libusb.so in my code. I've used following code..to just check the functions are loading or not.. What should I do to list device information in my app using libusb library.

code in jni directory..

libTest.c

#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <sys/types.h>
JNIEXPORT jstring JNICALL Java_com_example_loadlibrary_MainActivity_libTask(JNIEnv* env, jobject obj)
{
if( libusb_init(NULL) )
    return (*env)->NewStringUTF(env, "Library Loaded Successfuly!");
else
    return (*env)->NewStringUTF(env, "Can not load library........");
}

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

include $(CLEAR_VARS)

LOCAL_MODULE := libusb-1.0

LOCAL_SRC_FILES := libusb-1.0.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE    := libTest
LOCAL_SRC_FILES := libTest.c

include $(BUILD_SHARED_LIBRARY)

libusb-1.0.so

and src contains this java file:

MainActivity.java

package com.example.loadlibrary;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView  tv = new TextView(this);
    tv.setText(libTask());
    setContentView(tv);


}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
private native String libTask();
static{
    System.loadLibrary("usb-1.0");
    System.loadLibrary("libTest");
 }

}

but It shows error in ndk compilation:

**error: undefined reference to 'libusb_init' collect2: ld returned 1 exit status make: [obj/local/armeabi/libTest.so] Error 1*

it shows that function is not defined in my code. So how I will be able to get all the functions get working....

and atlast if I do

replace: if( libusb_init(NULL) ) with: if(1)

all files are generated but shows this error:

recipe for target Loadlibrary.exe' failed makefile /Loadlibrary/Debug line 31 C/C++ Problem undefined reference to_WinMain@16' Loadlibrary line 39, external location: \usr\src\debug\cygwin-1.7.17-1\winsup\cygwin\lib\libcmain.c C/C++ Problem make: * [Loadlibrary6.exe] Error 1 Loadlibrary C/C++ Problem

Upvotes: 1

Views: 4495

Answers (2)

Ermir
Ermir

Reputation: 247

What I used to build with NDK and libusb support is the following: Add into you Android.mk

 LOCAL_LDLIBS :=-L/path/to/your/compiled/libusb.so -lusb 
Give it a try. Hope this helps.

Upvotes: 1

Shiv
Shiv

Reputation: 4567

First load your libraries and then use function inside it

      static{
      System.loadLibrary("usb-1.0");
      System.loadLibrary("libTest");
      }
      public final static native String libTask(String st); //libTask is returning a string also...

.so files are in "jni" folder so first check that after build are they created in "libs/armeabi" folder also??

Upvotes: 1

Related Questions