TJJ
TJJ

Reputation: 167

Android development: updating code to more recent NDK?

I have a problem with some code that has been written for Android NDK 10d. It's not my code, but I'd like to fork and recompile this project. But I run into problems with the current NDK (19c). I have seen the code snippet in other projects as well, but I couldn't find a proper way to update this section. Maybe someone here has an idea or could explain what the problem is exactly?

Code:

JNIEXPORT jint JNICALL Java_jackpal_androidterm_TermExec_createSubprocessInternal(JNIEnv *env, jclass clazz,
    jstring cmd, jobjectArray args, jobjectArray envVars, jint masterFd)
{
    const jchar* str = cmd ? env->GetStringCritical(cmd, 0) : 0;
    String8 cmd_8;
    if (str) {
        cmd_8.set(str, env->GetStringLength(cmd));
        env->ReleaseStringCritical(cmd, str);
    }

...

    int ptm = create_subprocess(env, cmd_8.string(), argv, envp, masterFd);

return ptm;
}

I get the following error (referring to cmd_8.set(str, env->GetStringLength(cmd));):

process.cpp:210:19: error: cannot initialize a parameter of type 'const char16_t *' with an lvalue of type 'const jchar *' (aka 'const unsigned short *')
        cmd_8.set(str, env->GetStringLength(cmd));
                  ^~~

So I guess parameter types changed with newer versions of NDK? Do I need a type conversion somewhere? I guess there are just updated functions/calls to this in newer versions, but I couldn't find any documentation (also didn't know what to look for).

Any ideas?

Upvotes: 0

Views: 123

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57163

The compiler in r19 is less forgiving than the one from r10. The parameter types did not change, but the recent clang requires that you add explicit cast:

 cmd_8.set((const char16_t*)str, env->GetStringLength(cmd));

This said, your project may have a worse problem, as explained @Richard Critten. If it relies upon libutils and other Android private libraries, it will not work on Android Marshmallow and higher.

Upvotes: 1

Related Questions