Use libusb1.0.so functions inside your own library

I am trying to integrate the libusb library. I followed all the steps mentioned above, but still could not achieve.

What I've done:

  • Follow the procedure in this link
  • This helped me generate libusb1.0.so files for different ABIs.
  • I have included these .SOs in my Android project under / src / main / jniLbs / AIBs / libusb 1.0.so
  • Native-lib.cpp file created (see code below)
  • Modified CMakeLists.txt (Editing purpose for using libusb functions inside my own lib by importing it "# include" libusb.h "" )

My native lib.cpp:

#include <jni.h> #include <string> #include "libusb.h" //**Getting error can not find libusb extern "C" JNIEXPORT jstring JNICALL Java_com_example_admin_usb_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); } 

My CMakeLists.txt file:

 # For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.4.1) # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/native-lib.cpp ) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log ) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. # target_link_libraries( # Specifies the target library. # native-lib # # # Links the target library to the log library # # included in the NDK. # ${log-lib} ) add_library( # Sets the name of the library. usb1.0 # Sets the library as a shared library. SHARED IMPORTED ) set_target_properties( # Specifies the target library. usb1.0 # Specifies the parameter you want to define. PROPERTIES IMPORTED_LOCATION # Provides the path to the library you want to import. usb1.0/src/${ANDROID_ABI}/libusb1.0.so ) target_link_libraries( native-lib ${log-lib} usb1.0 ) 

What am I trying to achieve?

I want to use the libusb functions inside my native lib by importing "#include" libusb.h ". Therefore, I can use its function to detect usb and perform some operations.

Problem: Could not find libusb.h

The reason for updating the CMakeLists.txt file is because I want to transfer both the (native-lib and external library libusb.so) libraries to the APK and use libusb as an external library and its function inside my native cpp code ( But I don’t sure this is the right way to do this ).

I did not find the correct tutorial on integrating libusb into Android and fixed the documentation.

I think I do not have the CMakeLists.txt file.

Please correct me if I do something wrong.

0
source share

Source: https://habr.com/ru/post/1259261/


All Articles