emoji表情引發的JNI崩潰

今天突然接到客服那邊的反饋說,有玩家反饋進遊戲後不久就崩潰了,我先是懷疑網絡問題,因爲一連接聊天成功後就掛了。之後用logcat抓日誌,發現掛在jni那裏了

JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xf0 string: '' in call to NewStringUTF from void org.cocos2dx.lib.Cocos2dxRenderer.nativeRender()

調用JNI的NewStringUTF方法就掛了,然後讓後臺把聊天日誌全部拉出來,另存爲html放到mac機上查看。發現一個特殊的表情,如下圖所示:

我先讓後臺的同事,把所有聊天信息清理乾淨,這時候設備重新登錄進去沒有問題了。所以確定問題就是這個NewStringUTF方法引起的(但部分設備上有問題,部分設備沒問題。看了一下好像是Android5.0及以後的系統就有此問題),問了其它同事,發現他們之前遇到過並且處理了。

有二種方案:一種是升級NDK,另外一種是C++傳給Java時使用byte[],Java裏再把byte[]轉成String,避免NewStringUTF導致的崩潰。

我用的是cocos2d-x 2.x版本,找到CCImage.cpp文件,修改getBitmapFromJava方法

bool getBitmapFromJava(const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, float fontSize)
{
    JniMethodInfo methodInfo;
    if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmap", 
        "([BLjava/lang/String;IIII)V"))
    {
        CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
        return false;
    }

    /**create bitmap
     * this method call Cococs2dx.createBitmap()(java code) to create the bitmap, the java code
     * will call Java_org_cocos2dx_lib_Cocos2dxBitmap_nativeInitBitmapDC() to init the width, height
     * and data.
     * use this appoach to decrease the jni call number
    */

    int strLen = strlen(text);
    jbyteArray byteArray = methodInfo.env->NewByteArray(strLen);
    methodInfo.env->SetByteArrayRegion(byteArray, 0, strLen, reinterpret_cast<const jbyte*>(text));


//        jstring jstrText = methodInfo.env->NewStringUTF(text);
    jstring jstrFont = methodInfo.env->NewStringUTF(pFontName);

    methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, byteArray,
        jstrFont, (int)fontSize, eAlignMask, nWidth, nHeight);

//        methodInfo.env->DeleteLocalRef(jstrText);
    methodInfo.env->DeleteLocalRef(byteArray);

    methodInfo.env->DeleteLocalRef(jstrFont);
    methodInfo.env->DeleteLocalRef(methodInfo.classID);

    return true;
}

註釋部分爲原來的代碼,將string替換爲byte[]再傳給Java即可,其它地方如果也遇到JNI崩潰的問題,也按上面進行修改即可。

符一個字符串與jbyteArray的互轉函數

jbyteArray as_byte_array(unsigned char* buf, int len) {
    jbyteArray array = env->NewByteArray(len);
    env->SetByteArrayRegion(array, 0, len, reinterpret_cast<jbyte*>(buf));
    return array;
}
 
unsigned char* as_unsigned_char_array(jbyteArray array) {
    int len = env->GetArrayLength(array);
    unsigned char* buf = new unsigned char[len];
    env->GetByteArrayRegion(array, 0, len, reinterpret_cast<jbyte*>(buf));
    return buf;
}

mysql 5.5之前僅支持3個字節,如果遊戲中有留言等功能要存進數據庫的記錄,那麼你就需要過濾這些字符了,不然就會插入數據報錯。

更多閱讀鏈接:

JNI UTF-8 encoding bug with some characters

Android ICS 4.0 NDK NewStringUTF is crashing down the App

A correct way to convert byte[] in java to unsigned char* in C++, and vice versa?

emoji處理方式大起底

cocos2d-x android遊戲使用自己的字體

Android 上的 製表符(tab) —— 一個神奇的字符 (cocos2dx crash)

Android 上的 製表符(tab) —— 一個神奇的字符 (二)

Java Native Interface

C and C++ JNI - University of Cambridge

Java Native Interface

探索在Android中使用Emoji Font的方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章