Tag Archives: jni

[Solved] ERROR: Unknown host CPU architecture: arm64

ERROR: Unknown host CPU architecture: arm64

When compiling the android ndk project built on Android.mk, I found that the following error occurs on the m1 macbook pro

ERROR: Unknown host CPU architecture: arm64

Need to modify the ndk-build file in the ndk root directory (presumably because the m1 belongs to the arm architecture)

#!/bin/sh
DIR="$(cd "$(dirname "$0")" && pwd)"
$DIR/build/ndk-build "$@"

Change to

#!/bin/sh
DIR="$(cd "$(dirname "$0")" && pwd)"
arch -x86_64 /bin/bash $DIR/build/ndk-build "$@"

[Solved] JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring

JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring

A/k.myapplicatio: java_vm_ext.cc:545] JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring
    java_vm_ext.cc:545]     in call to GetStringUTFChars
    java_vm_ext.cc:545]     from void com.sdk.myapplication.MainActivity.triggerGetStringUTFCharsNPE(java.lang.String, java.lang.String)

The reason for the above message, from a Jni method, refers to the case where the JVM calls the method and the GetStringUTFChars entry jstring is empty, it must not refer specifically to the method body where the GetStringUTFChars call has a NULl pointer, but includes other C/C++ method calls in the method that have a NULL pointer. NULL pointer. So we have to look at the crash stack carefully to quickly locate the place where the NULL pointer is generated to avoid invalid output.

the above example is extracted according to our own business code
the example code is as follows
java

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        triggerGetStringUTFCharsNPE("hello", null);//introduce a null
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();


    public native void triggerGetStringUTFCharsNPE(String p1, String p2);
}

jni

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

static const char* TAG = "Demo";


void nestFunction(JNIEnv *env, jstring p2) {
    //Here p1 is simply passed in via a parameter, but the actual business logic may be obtained by other methods/ways
    const char* pStr = env->GetStringUTFChars(p2, nullptr);
    __android_log_print(ANDROID_LOG_DEBUG, TAG, "pStr:%s", pStr);
    env->ReleaseStringUTFChars(p2, pStr);
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_sdk_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}extern "C"
JNIEXPORT void JNICALL
Java_com_sdk_myapplication_MainActivity_triggerGetStringUTFCharsNPE(JNIEnv *env, jobject thiz,
                                                                    jstring p1, jstring p2) {
    const char* pStr1 = env->GetStringUTFChars(p1, nullptr);
    __android_log_print(ANDROID_LOG_DEBUG, TAG, "pStr1:%s", pStr1);
    env->ReleaseStringUTFChars(p1, pStr1);
    //故意让nestFunction产生 JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring
    nestFunction(env, p2);
}

[Solved] AndroidStudio libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined referen

1. Error reporting:

The error of Android C + + OpenSSL link is as follows:

I:/webrtc/android/openssl-1.1.1k/output-armeabi-v7a/lib/libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined reference to 'sigfillset'

I:/webrtc/android/openssl-1.1.1k/output-armeabi-v7a/lib/libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined reference to 'sigdelset'

I:/webrtc/android/openssl-1.1.1k/output-armeabi-v7a/lib/libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined reference to 'sigdelset'

I:/webrtc/android/openssl-1.1.1k/output-armeabi-v7a/lib/libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined reference to 'sigdelset'

I:/webrtc/android/openssl-1.1.1k/output-armeabi-v7a/lib/libcrypto.a(armcap.o):armcap.c:function OPENSSL_cpuid_setup: error: undefined reference to 'sigdelset'

I:/webrtc/android/openssl-1.1.1k/output-armeabi-v7a/lib/libcrypto.a(ui_openssl.o):ui_openssl.c:function open_console: error: undefined reference to 'tcgetattr'

I:/webrtc/android/openssl-1.1.1k/output-armeabi-v7a/lib/libcrypto.a(ui_openssl.o):ui_openssl.c:function read_string_inner: error: undefined reference to 'signal'

I:/webrtc/android/openssl-1.1.1k/output-armeabi-v7a/lib/libcrypto.a(ui_openssl.o):ui_openssl.c:function read_string_inner: error: undefined reference to 'tcsetattr'

I:/webrtc/android/openssl-1.1.1k/output-armeabi-v7a/lib/libcrypto.a(ui_openssl.o):ui_openssl.c:function read_string_inner: error: undefined reference to 'tcsetattr'

clang++: error: linker command failed with exit code 1 (use -v to see invocation)

2. Reason:

Sigdelset, sigfillset and signal cannot be found in the SDK of Android

3. Solution:

Modify the minSdkVersion of build.grandle to a version number after 21:

apply plugin: 'com.android.application'
def LIBWEBRTC_HOME_PATH = "I:/webrtc/android/webrtc_m84_20201001/webrtc_android/src/"
//def LIBWEBRTC_HOME_PATH = "I:/webrtc/android/androidnativeapi/app/webrtc/"
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "org.webrtc.examples.androidnativeapi"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                arguments "-DLIBWEBRTC_HOME_PATH=" + LIBWEBRTC_HOME_PATH,
                        "-DANDROID_STL=c++_static"
            }
        }
        ndk {
            abiFilters  "armeabi-v7a"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    sourceSets {
        main {
            // 1. configure the root directory libs to load third-party so libraries, (it is best not to create jniLibs, in the many open source libraries may cause conflicts, not yet found)
            // 2. automatically copy the so libraries in the libs directory to the specified directory when running
            // 3. If you don't need to recompile the so you created, you can copy the so generated by (app/build/intermediates/transforms) to this directory
            jniLibs.srcDirs = ['libs']
            // If it is a single folder, you can directly configure it as follows
            // jniLibs.srcDir 'libs'
        }
    }
    buildToolsVersion '28.0.2'//ADD
}
repositories {
    flatDir{
        dirs'libs'
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.aar"])

    //implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    //implementation(name: 'libwebrtc', ext: 'aar')
    //implementation 'org.webrtc:google-webrtc:1.0.+'
}

JNI calls the add function of list in Java to flash back

JNI calls the add function of List in Java to flit back
New changes to the problem

The problem
Flash back code is as follows:
in recent applications, the need to transfer data in C to Java a structure, in this structure, there are GNSSObservation object type of List, GNSSObservation for custom structures, therefore in the jni invocation List are required for data transmission of the add function to add data into the List, but the program, when the function is called multiple times, flash back, specific code is as follows:

env – & gt; CallObjectMethod(GnssObservationList,GnssObservationListAddFunctionID,GnssObservation); Instead of running every time, the program runs for several minutes in a row, and the function is called about 40 times per second. The reason for the backout is never found, and the data that needs to be passed to Java is passed as a double array. The reason for the code flash back has not been solved, I hope you can give some suggestions on the cause of flash back.
The new change