Search code examples
androidc++cmakecallbackjava-native-interface

App crashing while calling callback() to java. Getting runtime error due to detatchThread


I am trying to basically create a callback from native-lib.cpp class callback() method to call some method in java. But for that, I need env variable also. I am trying to create same in native-lib.cpp but getting this error. I think some issue because of thread being detatched. Can someone please clarify this. I am very new to callbacks creation concept. Can someone point out exactly the issue. Some issue with detachThread seems to me. Can someone clarify on the issue. Thanks in advance.

This is the native-lib.cpp:

#include <jni.h>
#include <string>
#include "Numbers.h"

#include <android/log.h>

#define  LOG_TAG    "logs"

#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)


typedef struct number_ctx{
    JavaVM  *javaVM;
    jclass   mathsClass;
    jobject  mathsObj;
    jclass   mainActivityClass;
    jobject  mainActivityObj;
    pthread_mutex_t  lock;
} NumbersCtx;
NumbersCtx g_ctx;


JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
    JNIEnv* env;
    memset(&g_ctx, 0, sizeof(g_ctx));

    g_ctx.javaVM = vm;

    if (vm ->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
        return JNI_ERR; // JNI version not supported.
    }

    jclass  clz = env->FindClass(
            "com/example/maths/Maths1");

    g_ctx.mathsClass = static_cast<jclass>(env->NewGlobalRef(clz));

    clz = env->FindClass(
            "com/example/maths/MainActivity");

    g_ctx.mainActivityClass = static_cast<jclass>(env->NewGlobalRef(clz));

    g_ctx.mathsObj = NULL;
    g_ctx.mainActivityObj = NULL;

    env->DeleteLocalRef(clz);

    return  JNI_VERSION_1_6;
}

void callback() {

    JNIEnv* env;

    LOGD("Starting finally ");

    if (g_ctx.javaVM ->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
        LOGD("JNI Error is %d" , JNI_ERR); // JNI version not supported.
    }

    LOGD("Env variable received successfully");

    int status = g_ctx.javaVM->AttachCurrentThread(&env, NULL);

    if(status != JNI_OK) {
        LOGD("Failed to attach the thread");
    } else {

        LOGD("Thread attached successfully");

        jmethodID testMessage = env->GetMethodID(g_ctx.mathsClass, "printMessage",
                                                 "()V");
        // Call the method on the object
        env->CallVoidMethod(g_ctx.mathsObj, testMessage);

        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
        }

        LOGD("Void Method called Successfully");

        g_ctx.javaVM->DetachCurrentThread();

        LOGD("Thread detached successfully");
    }

    LOGD("This callback is called finally");
}

jfieldID getPtrFieldId(JNIEnv * env, jobject obj)
{
    static jfieldID ptrFieldId = 0;

    if (!ptrFieldId)
    {
        jclass c = env->GetObjectClass(obj);
        ptrFieldId = env->GetFieldID(c, "numberptr", "J");
        env->DeleteLocalRef(c);
    }

    return ptrFieldId;
}


extern "C"
JNIEXPORT void JNICALL
Java_com_example_maths_Maths1_createNumberInstance(JNIEnv *env, jobject thiz, jint a, jint b) {
    // TODO: implement createNumberInstance()

    g_ctx.mathsObj = env->NewGlobalRef(thiz);
    Numbers* num = new Numbers(a, b);
    callback();
    env->SetLongField(thiz, getPtrFieldId(env, thiz), (jlong) num);

}

This is error which I am getting:

2020-06-02 19:25:39.101 15965-15965/com.example.maths A/m.example.math: runtime.cc:2067] Thread[1,tid=15965,Native,Thread*=0xec991800,peer=0x71b391f0,"main"] attempting to detach while still running code
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] Runtime aborting...
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] Dumping all threads without mutator lock held
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] All threads:
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] DALVIK THREADS (17):
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] "main" prio=10 tid=1 Runnable
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | group="" sCount=0 dsCount=0 flags=0 obj=0x71b391f0 self=0xec991800
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | sysTid=15965 nice=-10 cgrp=default sched=0/0 handle=0xece36dc8
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | state=R schedstat=( 466154407 353144186 350 ) utm=12 stm=34 core=0 HZ=100
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | stack=0xff27a000-0xff27c000 stackSize=8192KB
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | held mutexes= "abort lock" "mutator lock"(shared held)
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #00 pc 00493b43  /apex/com.android.runtime/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, int, BacktraceMap*, char const*, art::ArtMethod*, void*, bool)+115)
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #01 pc 005b1fd2  /apex/com.android.runtime/lib/libart.so (art::Thread::DumpStack(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, bool, BacktraceMap*, bool) const+994)
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #02 pc 005ace61  /apex/com.android.runtime/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, bool, BacktraceMap*, bool) const+65)
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #03 pc 005d2cd1  /apex/com.android.runtime/lib/libart.so (art::DumpCheckpoint::Run(art::Thread*)+929)
2020-06-02 19:25:39.497 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #04 pc 005cac06  /apex/com.android.runtime/lib/libart.so (art::ThreadList::RunCheckpoint(art::Closure*, art::Closure*)+486)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #05 pc 005c9e6c  /apex/com.android.runtime/lib/libart.so (art::ThreadList::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, bool)+2268)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #06 pc 005796f0  /apex/com.android.runtime/lib/libart.so (art::AbortState::DumpAllThreads(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, art::Thread*) const+448)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #07 pc 00564d30  /apex/com.android.runtime/lib/libart.so (art::Runtime::Abort(char const*)+1536)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #08 pc 000249b3  /apex/com.android.runtime/lib/libartbase.so (_ZNSt3__110__function6__funcIPFvPKcENS_9allocatorIS5_EES4_EclEOS3_+35)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #09 pc 0000bac7  /system/lib/libbase.so (android::base::LogMessage::~LogMessage()+727)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #10 pc 005645de  /apex/com.android.runtime/lib/libart.so (art::Runtime::DetachCurrentThread()+414)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #11 pc 003ecc83  /apex/com.android.runtime/lib/libart.so (art::JII::DetachCurrentThread(_JavaVM*)+67)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #12 pc 003e3445  /apex/com.android.runtime/lib/libart.so (art::(anonymous namespace)::CheckJII::DetachCurrentThread(_JavaVM*)+117)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #13 pc 00009d6a  /data/app/com.example.maths-alZFaEzd-BAd84VC19Fvrg==/lib/x86/libnative-lib.so (_JavaVM::DetachCurrentThread()+42)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #14 pc 00009a8a  /data/app/com.example.maths-alZFaEzd-BAd84VC19Fvrg==/lib/x86/libnative-lib.so (callback()+554)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #15 pc 0000a24c  /data/app/com.example.maths-alZFaEzd-BAd84VC19Fvrg==/lib/x86/libnative-lib.so (Java_com_example_maths_Maths1_createNumberInstance+172)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #16 pc 00144f67  /apex/com.android.runtime/lib/libart.so (art_quick_generic_jni_trampoline+71)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #17 pc 0013e7d2  /apex/com.android.runtime/lib/libart.so (art_quick_invoke_stub+338)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #18 pc 00149a69  /apex/com.android.runtime/lib/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+281)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #19 pc 00332502  /apex/com.android.runtime/lib/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::ShadowFrame*, unsigned short, art::JValue*)+386)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #20 pc 0032c19c  /apex/com.android.runtime/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+988)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #21 pc 006818dd  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+989)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #22 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #23 pc 0011dc02  [anon:dalvik-classes.dex extracted in memory from /data/app/com.example.maths-alZFaEzd-BAd84VC19Fvrg==/base.apk] (com.example.maths.Maths1.<init>+26)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #24 pc 0068461c  /apex/com.android.runtime/lib/libart.so (MterpInvokeDirect+1324)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #25 pc 00138921  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_direct+33)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #26 pc 0011db02  [anon:dalvik-classes.dex extracted in memory from /data/app/com.example.maths-alZFaEzd-BAd84VC19Fvrg==/base.apk] (com.example.maths.MainActivity.onCreate+26)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #27 pc 00681b4c  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #28 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #29 pc 0019175e  /system/framework/framework.jar (android.app.Activity.performCreate+30)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #30 pc 00681b4c  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #31 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #32 pc 00191726  /system/framework/framework.jar (android.app.Activity.performCreate+2)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #33 pc 00681b4c  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #34 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #35 pc 001eefda  /system/framework/framework.jar (android.app.Instrumentation.callActivityOnCreate+6)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #36 pc 00681b4c  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #37 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #38 pc 00180bb8  /system/framework/framework.jar (android.app.ActivityThread.performLaunchActivity+752)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #39 pc 0068461c  /apex/com.android.runtime/lib/libart.so (MterpInvokeDirect+1324)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #40 pc 00138921  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_direct+33)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #41 pc 00180836  /system/framework/framework.jar (android.app.ActivityThread.handleLaunchActivity+94)
2020-06-02 19:25:39.498 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #42 pc 00681b4c  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #43 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #44 pc 0025e28e  /system/framework/framework.jar (android.app.servertransaction.LaunchActivityItem.execute+126)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #45 pc 00681b4c  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #46 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #47 pc 002607da  /system/framework/framework.jar (android.app.servertransaction.TransactionExecutor.executeCallbacks+154)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #48 pc 00681b4c  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #49 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #50 pc 00260716  /system/framework/framework.jar (android.app.servertransaction.TransactionExecutor.execute+146)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #51 pc 00681b4c  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #52 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #53 pc 0017fb6e  /system/framework/framework.jar (android.app.ActivityThread$H.handleMessage+78)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #54 pc 00681b4c  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #55 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #56 pc 002f5846  /system/framework/framework.jar (android.os.Handler.dispatchMessage+38)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #57 pc 00681b4c  /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #58 pc 00138821  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #59 pc 00319ee6  /system/framework/framework.jar (android.os.Looper.loop+466)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #60 pc 00684fdc  /apex/com.android.runtime/lib/libart.so (MterpInvokeStatic+1260)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #61 pc 001389a1  /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_static+33)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #62 pc 00189466  /system/framework/framework.jar (android.app.ActivityThread.main+194)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #63 pc 002f8e0a  /apex/com.android.runtime/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEbb.llvm.1175793267244191248+298)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #64 pc 002ffcc5  /apex/com.android.runtime/lib/libart.so (art::interpreter::EnterInterpreterFromEntryPoint(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*)+181)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #65 pc 0066fc49  /apex/com.android.runtime/lib/libart.so (artQuickToInterpreterBridge+1209)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #66 pc 0014503d  /apex/com.android.runtime/lib/libart.so (art_quick_to_interpreter_bridge+77)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #67 pc 0013e9a2  /apex/com.android.runtime/lib/libart.so (art_quick_invoke_static_stub+418)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #68 pc 00149a7a  /apex/com.android.runtime/lib/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+298)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #69 pc 0055a563  /apex/com.android.runtime/lib/libart.so (art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, art::(anonymous namespace)::ArgArray*, art::JValue*, char const*)+99)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #70 pc 0055c37f  /apex/com.android.runtime/lib/libart.so (art::InvokeMethod(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jobject*, _jobject*, unsigned int)+1327)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #71 pc 004c91a3  /apex/com.android.runtime/lib/libart.so (art::Method_invoke(_JNIEnv*, _jobject*, _jobject*, _jobjectArray*)+83)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   at com.example.maths.Maths1.createNumberInstance(Native method)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   at com.example.maths.Maths1.<init>(Maths1.java:16)
2020-06-02 19:25:39.499 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   at com.example.maths.MainActivity.onCreate(MainActivity.java:21)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] 
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] "Runtime worker thread 0" prio=10 tid=2 Native (still starting up)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | group="" sCount=1 dsCount=0 flags=1 obj=0x0 self=0xd9500000
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | sysTid=15991 nice=0 cgrp=default sched=0/0 handle=0xec77fd90
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | state=S schedstat=( 1604515 798906 8 ) utm=0 stm=0 core=1 HZ=100
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | stack=0xec771000-0xec773000 stackSize=63KB
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | held mutexes=
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   kernel: (couldn't read /proc/self/task/15991/stack)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #00 pc 00000b77  [vdso] (__kernel_vsyscall+7)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #01 pc 00092328  /apex/com.android.runtime/lib/bionic/libc.so (syscall+40)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #02 pc 00150802  /apex/com.android.runtime/lib/libart.so (art::ConditionVariable::WaitHoldingLocks(art::Thread*)+114)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #03 pc 00150783  /apex/com.android.runtime/lib/libart.so (art::ConditionVariable::Wait(art::Thread*)+35)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #04 pc 005d4b15  /apex/com.android.runtime/lib/libart.so (art::ThreadPool::GetTask(art::Thread*)+261)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #05 pc 005d3c43  /apex/com.android.runtime/lib/libart.so (art::ThreadPoolWorker::Run()+83)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #06 pc 005d370f  /apex/com.android.runtime/lib/libart.so (art::ThreadPoolWorker::Callback(void*)+239)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #07 pc 0011a8e5  /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+53)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #08 pc 000af6a7  /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+71)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   (no managed stack frames)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] 
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] "Jit thread pool worker thread 0" prio=10 tid=3 Native
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | group="" sCount=1 dsCount=0 flags=1 obj=0x159c0030 self=0xec98ee00
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | sysTid=15990 nice=0 cgrp=default sched=0/0 handle=0xd9404d90
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | state=S schedstat=( 4160010 1789692 15 ) utm=0 stm=0 core=1 HZ=100
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | stack=0xd9306000-0xd9308000 stackSize=1023KB
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | held mutexes=
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   kernel: (couldn't read /proc/self/task/15990/stack)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #00 pc 00000b77  [vdso] (__kernel_vsyscall+7)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #01 pc 00092328  /apex/com.android.runtime/lib/bionic/libc.so (syscall+40)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #02 pc 00150802  /apex/com.android.runtime/lib/libart.so (art::ConditionVariable::WaitHoldingLocks(art::Thread*)+114)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #03 pc 00150783  /apex/com.android.runtime/lib/libart.so (art::ConditionVariable::Wait(art::Thread*)+35)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #04 pc 005d4b15  /apex/com.android.runtime/lib/libart.so (art::ThreadPool::GetTask(art::Thread*)+261)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #05 pc 005d3c75  /apex/com.android.runtime/lib/libart.so (art::ThreadPoolWorker::Run()+133)
2020-06-02 19:25:39.500 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #06 pc 005d370f  /apex/com.android.runtime/lib/libart.so (art::ThreadPoolWorker::Callback(void*)+239)
2020-06-02 19:25:39.501 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #07 pc 0011a8e5  /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+53)
2020-06-02 19:25:39.501 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   native: #08 pc 000af6a7  /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+71)
2020-06-02 19:25:39.501 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   (no managed stack frames)
2020-06-02 19:25:39.501 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] 
2020-06-02 19:25:39.501 15965-15965/com.example.maths A/m.example.math: runtime.cc:630] "Runtime worker thread 1" prio=10 tid=4 Native (still starting up)
2020-06-02 19:25:39.501 15965-15965/com.example.maths A/m.example.math: runtime.cc:630]   | group="" sCount=1 dsCount=0 flags=1 obj=0x0 self=0xd9508c00

Solution

  • If GetEnv returns JNI_OK, the current thread is already attached and you should not try to detach it.

    This is why Android is screaming bloody murder at you: you are trying to detach the main thread!

    You should only try to attach (and detach later) if it returned JNI_EDETACHED.