Congratulations to our 2022 Qt Champions!

Requesting Root on Qt Android

  • Hi all.
    I have a rooted Android device, and am trying to run my Qt app with root permissions.
    I've looked around, and it seems that the recommended way to do this is by running the following line of Java:

    Process process = Runtime.getRuntime().exec("su");

    From what I understand, it creates a new Java thread as root, and the parent thread inherits this permission.

    I have attempted to convert this call into a QAndroidJniObject, using the following C++ code:

    QAndroidJniObject process = QAndroidJniObject::callStaticObjectMethod("java/lang/Runtime", "getRuntime().exec", "(Ljava/lang/Process)Ljava/lang/String;", "su");

    Unfortuantely, it just does nothing. No prompt for root access, not even an error message!
    I'm guessing this is because I've treated "getRuntime().exec" as if it were a class, whereas from the look of things, getRuntime is a function and exec is called on the return value of that.
    Is there a way to deal with this in Qt? If not, does anyone know a way of requesting root using native code?


  • Lifetime Qt Champion

    @espocjo As far as I know Runtime.getRuntime().exec executes the command in a new process, not in a thread in your process (how should this even work?). Since su is executed in another process I don't think your process will become root. Usually a program is started as root (like: sudo ./app).

  • You're not wrong.
    After working out the intricacies of Java and discovering ANDROID_PACKAGE_SOURCE_DIR, I finally managed to get that command to work.

    Of course, it did nothing. I have no idea what it does; I was just copying what I'd read on Stack Exchange.
    (Re: multiple threads in a single process, Windows can have multi-threaded .exes. Evidently, it seems like Android doesn't! Please excuse me, I just spent half a day trying to get "hello world" printed over logcat.)

  • @jsulm
    Do you know of any command that an app could use to elevate itself? Or am I going about it the wrong way?

  • Lifetime Qt Champion

    @espocjo One process executes one executable, it doesn't matter how many threads. You cannot execute one executable in one thread and another executable in another thread in the same process.
    Can't you start your app with sudo? Or use setuid:

  • @jsulm

    Apparently setuid was disabled in Android 4.3 for security reasons.
    (And apparently Java's "Process" is not a new thread).
    I don't know of any way to launch an app with su command. If there is one, it doesn't seem simple.

Log in to reply