1

I am trying to use libusb in an Android app, it is embedded in an Android native module and built with cmake. The code only call libusb_init and returns a string to the activity and it systematically fails with LIBUSB_ERROR_OTHER.

The thing is I can get it to work when it is launched from a small command-line program doing only libusb_init that we launch with adb shell. In that case, it returns success.

The device (Samsung Galaxy Tab E) runs Android 7 and is rooted, with Magisk Manager I give my app root permission but it's not like doing "su" in command-line.

I mainly tried with version 20 and 22 (we use mostly version 20 in our other projects).

I already tried many stackoverflow solutions and workarounds but they were either ineffective or not possible to implement (due to rom restrictions for example).

Here is how I call libusb.c :

#include <jni.h>
#include <string>

extern "C" {
#include "libusb.h"
}

using namespace std;

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    string hello = "Hello from C++";
    libusb_context **lusb;
    int rc = libusb_init(lusb);

    return env->NewStringUTF(hello.c_str());
}

And here is how I call this code from Activity :

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        //The purpose of this is to ask for root access, handled with 
        //Magisk Manager in my case
        Runtime.getRuntime().exec("su")

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Result = ${stringFromJNI()}", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    companion object {

        // Used to load the 'native-lib' library on application startup.
        init {
            System.loadLibrary("native-lib")
        }
    }

Small program that I run in command line (it works wether I'm root or not):

#include <iostream>
extern "C" {
    #include "libusb.h"
}
using namespace std;

int main (int argc, char* argv[]) {
    libusb_context *lusb;
    int rc = libusb_init(&lusb);
    if ( rc != LIBUSB_SUCCESS ) {
        cout << "Failed to open api. Error: " << libusb_error_name(rc) << endl;
        return 1;
    }

    cout << "This worked" << endl;
    return 0;
}
tsmirani
  • 11
  • 2

0 Answers0