Android native can pass a value (string) in Debug mode, but does not return a value in Release mode

hide the sensitive value in the native layer to prevent decompilation from being searched for by keywords. I use this method.

//charstringsrcstring
-sharpinclude <stdio.h>

int main() {
    char src[] = "ab";
    int len = 0;
    while (src[len] != "\0")
        lenPP;
    printf("\n(char[]){");
    for (int i = 0; i <= len - 1; iPP) {
        printf("\"%c\"", src[i]);
        if (i < len - 1)
            printf(",");
    }
    printf(",\"\\0\"}\n");
    return 0;
}

then encapsulate the char array of split into the CPP part of the code of JNI in the following manner

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_example_MyClass_a(JNIEnv *env, jobject, jint i) {
    //...
    int char_select = (int) i;
    char *retStr = NULL;
    //
    char *str1 = (char[]) {"a","b","\0"};
    //...
    //switch
    switch (char_select) {
        case 0:
            retStr = str1;
            break;
        //...
        
    }
    //
    return env->NewStringUTF(retStr);
}

use (reflect some methods, but use the native layer to store related name fields)

public class MyClass extends Application{
    //so
    static {
        System.loadLibrary("native");
    }
    
    static native String a(int i);

    private static void x(Context context) {
        try {
            //native
            Class<?> clz1 = Class.forName(a(1));
            Method mtd1 = clz.getMethod(a(2));
            Object obj1 = mtd1.invoke(context);
            Method mtd2 = clz1.getMethod(a(3));
            String str = (String) mtd2.invoke(context);
            Field fld1 = clz1.getField(a(4));
            //...
        //    
        } catch (Exception e) {
            e.printStackTrace();
        }
    //onCreate
    @Override
    public void onCreate() {
        super.onCreate();
        x(this);
        //...
    }
}

then finds a strange problem. This code works well in Debug mode, but does not work in Release mode. The value returned is null or garbled. what is the reason for this?

search Baidu Bing Google DuckDuckGo to no avail, I do not know much about the writing of JNI, please give me advice.

Mar.18,2021
Menu