0
votes

In my current project I'm trying to use simple file which that written with C language, after install NDK, CMake, LLDB from android studio, my current path for ndk is :

ndk.dir=/Users/mahdi/Desktop/Home/Packages/AndroidSdk/ndk-bundle

and

gradle.properties content

android.useDeprecatedNdk=true

build.gradle:

defaultConfig {
    applicationId "ir.pishguy.myapp"
    minSdkVersion 17
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
    vectorDrawables.useSupportLibrary = true

    ndk{
        moduleName "web_service_encryption"
        ldLibs "log", "z", "m"
        abiFilters "armeabi", "armeabi-v7a", "x86"
    }
}

Now when I try to make and install app I get this error:

Error:(115) *** Android NDK: Aborting . Stop. Error:Execution failed for task ':app:compileDebugNdk'. com.android.ide.common.process.ProcessException: Error while executing process /Users/mahdi/Desktop/Home/Packages/AndroidSdk/ndk-bundle/ndk-build with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/Users/mahdi/Desktop/Home/Projects/Android/myapp/app/build/intermediates/ndk/debug/Android.mk APP_PLATFORM=android-25 NDK_OUT=/Users/mahdi/Desktop/Home/Projects/Android/myapp/app/build/intermediates/ndk/debug/obj NDK_LIBS_OUT=/Users/mahdi/Desktop/Home/Projects/Android/myapp/app/build/intermediates/ndk/debug/lib APP_ABI=armeabi-v7a,armeabi,x86}

And c file in jni folder:

#include<string.h>
#include<jni.h>
#include<android/log.h>

//  LOGI("hello") ?? LOGI("money %d",15)
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native", __VA_ARGS__))

const char key[] = "~yfj(e^m1)o@2pc!"; //16???
int len = 0;


unsigned char getByteNumber(unsigned char first, unsigned char end) {
    int firstPosition = 0, endPosition = 0;
    int position = 0;
    for (; position < 16; position++) {
        if (key[position] == first) {
            firstPosition = position;
        }
        if (key[position] == end) {
            endPosition = position;
        }
    }
    return (firstPosition << 4) | (endPosition);
}


void encrypt(unsigned char p[], unsigned char res[]) {
    int i = 0;
    for (; i < len; i++) {
        res[2 * i] = key[p[i] / 16];
        res[2 * i + 1] = key[p[i] % 16];
    }
}


void decrypt(unsigned char p[], char res[]) {
    int i;
    for (i = 0; i < len; i++) {
        res[i] = getByteNumber(p[i * 2], p[i * 2 + 1]);
    }
}

jstring Java_ir_pishguy_myapp_EncryptUtil_encrypt(JNIEnv *env, jclass this,
        jbyteArray src) {
    unsigned char *buff = (char*) (*env)->GetByteArrayElements(env, src, NULL);
    len = (*env)->GetArrayLength(env, src);
    unsigned char res[len * 2];
    encrypt(buff, res);
    res[len * 2] = '\0';
    (*env)->ReleaseByteArrayElements(env, src, buff, 0);
    jstring resStr = (*env)->NewStringUTF(env, res);
    return resStr;
}

jstring Java_ir_pishguy_myapp_EncryptUtil_decrypt(JNIEnv *env, jclass this,
        jbyteArray src) {
    unsigned char *buff = (char*) (*env)->GetByteArrayElements(env, src, NULL);
    len = (*env)->GetArrayLength(env, src);
    len = len / 2;
    signed char res[len];
    decrypt(buff, res);
    res[len] = '\0';
    (*env)->ReleaseByteArrayElements(env, src, buff, 0);
    jstring resStr = (*env)->NewStringUTF(env, res);
    return resStr;
}
1
Any particular reason for not using externalNativeBuild? - Michael

1 Answers

0
votes

APP_PLATFORM=android-25

It looks like the useDeprecatedNdk path is just wrong. 25 is your targetSdkVersion, not your minSdkVersion (NDK target needs to match your minimum target). Switch away from the deprecated thing and use externalNativeBuild.

The build issue you're seeing is because there's no such thing as android-25 in the NDK (we only have platforms where there are actually new native APIs to save on size).

Even if you fix that (by targeting 24 instead), you're going to have runtime issues on anything older than 24. Your NDK target needs to be 17 to match your minSdkVersion.