JNI: Uninstall Application Feedback Case Study

JNI: Uninstall Application Feedback Case Study

Foreword

As Android developers, we often believe that understanding JNI (Java Native Interface) is crucial, but many online tutorials are either too basic or too complex. They either explain how to configure the NDK environment or build a simple “helloWorld” demo, or dive into the depths of C language programming, which is often too advanced for Android developers. In this article, we will explore how to use JNI to call C language code from Java, and how to use C language to call Java methods. We will also discuss how to use C language to uninstall an application and provide feedback to the user.

What Can You Learn from this Article

  1. How to call C language code from Java to validate user names and passwords.
  2. How to call Java methods from C language.
  3. How to use C language to uninstall an application and provide feedback to the user.

Disclaimer

This article is an original work by the author and should not be reproduced without permission.

A What Can You Learn from this Article

1. Java Call C Language Code to Validate User Names and Passwords

To validate user names and passwords, we need to create a new method in the JNI class that will be called from Java. We will use the native keyword to indicate that this method is implemented in C language.

public native int checkUser(String name, String pass);

We will then implement this method in C language using the javah tool to generate the corresponding header file.

#include <jni.h>

JNIEXPORT jint JNICALL
Java_jnidemo_hlq_com_jnidemo_JNI_checkUser(JNIEnv *env, jobject instance, jstring name_, jstring pass_) {
    const char *name = (*env)->GetStringUTFChars(name_, 0);
    const char *pass = (*env)->GetStringUTFChars(pass_, 0);

    // TODO(Env) -> ReleaseStringUTFChars(name_, name);
    // TODO(Env) -> ReleaseStringUTFChars(pass_, pass);

    return 1; // Return 1 if the user name and password are valid
}

2. C Language Call Java Methods

To call Java methods from C language, we need to create a new method in the JNI class that will be called from C language. We will use the native keyword to indicate that this method is implemented in Java.

public native String testHello();

We will then implement this method in C language using the javah tool to generate the corresponding header file.

#include <jni.h>

JNIEXPORT jstring JNICALL
Java_jnidemo_hlq_com_jnidemo_JNI_testHello(JNIEnv *env, jobject instance) {
    return (*env)->NewStringUTF(env, "huanglinqing");
}

3. Uninstall Application Feedback Case Study

To uninstall an application and provide feedback to the user, we need to create a new method in the JNI class that will be called from C language. We will use the native keyword to indicate that this method is implemented in Java.

public native void uninstall(String packageName, int versionCode);

We will then implement this method in C language using the fork system call to create a new process that will uninstall the application.

#include <jni.h>
#include <unistd.h>

JNIEXPORT void JNICALL
Java_jnidemo_hlq_com_jnidemo_JNI_uninstall(JNIEnv *env, jobject instance, jstring packageName, jint versionCode) {
    int code = fork();

    if (code > 0) {
        // Parent process
        return;
    } else {
        // Child process
        char *dataDir = "/data/data/";
        char *packageNameC = (*env)->GetStringUTFChars(packageName, 0);
        char *packageNameC2 = new char[strlen(packageNameC) + strlen(dataDir) + 1];
        strcpy(packageNameC2, dataDir);
        strcpy(&packageNameC2[strlen(dataDir)], packageNameC);

        int flag = 1;
        while (flag) {
            sleep(1);
            FILE *file = fopen(packageNameC2, "rt");
            if (file == NULL) {
                flag = 0;
                if (versionCode < 17) {
                    execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://baidu.com", NULL);
                } else {
                    execlp("am", "am", "start", "--user", "0", "-a", "android.intent.action.VIEW", "-d", "http://baidu.com", (char *) NULL);
                }
            } else {
                fclose(file);
                LOGD("---", "I'm still");
            }
        }

        delete[] packageNameC2;
        (*env)->ReleaseStringUTFChars(packageName, packageNameC);
    }
}

This is just a basic example of how to use JNI to call C language code from Java, and how to use C language to call Java methods. You can modify and extend this code to suit your needs.