java嵌入lua有多個方案可選擇,比如純java的luaj 、純C jni的 luajava還有個lua4j。在android中嵌入lua,我偏向使用luajava(當然面對除arm外的平臺如x86、mips或需單獨打包).

 

要使用luajava除了要下載luajava還要下載lua源碼,需要注意的是luajava並不支援lua2.0以上的版本,我測試支援的版本是5.1.4,而且luajava中缺少頭檔luajava.h(本文末提供該檔下載)。下載完後把lua和luajava的luajava.c和luajava.h解壓到同一個檔夾,注意要在llex.c原始檔案的trydecpoint函數的



ls->decpoint = (cv ? cv->decimal_point[0] : '.');

 

改為
ls->decpoint = '.';

 

不然編譯不過。





實際使用中嵌入lua可能我們並不樂意讓人輕易知道我們使用了lua。這就要編譯so時不輸出lua自己的api,和luajava的JAVA_org_keplerproject_luajava_LuaState_XXX函數。

 

關閉lua的api輸出,可以在luaconf.h頭檔中找到LUA_API宏
#define LUA_API extern

 

將它改為

 

#define LUA_API extern __attribute__((visibility("hidden")))
而要關閉luajava中輸出函數,則需要在luajava中JNIEXPORT的函數和在JNI_OnLoad中注冊所有JAVA_org_keplerproject_luajava_LuaState_XXX函數(這種函數命名實在是醜陋!)。以下是我整理出來的函數(其中注釋掉的函數是luajava中的作者並沒有實現的).
static JNINativeMethod METHOD_LUASTATE[]={
{"_open", "()Lorg/keplerproject/luajava/CPtr;", (void *)JAVA_org_keplerproject_luajava_LuaState__1open},
{"_close", "(Lorg/keplerproject/luajava/CPtr;)V", (void *)JAVA_org_keplerproject_luajava_LuaState__1close},
{"_newthread", "(Lorg/keplerproject/luajava/CPtr;)Lorg/keplerproject/luajava/CPtr;", (void *)JAVA_org_keplerproject_luajava_LuaState__1newthread},
{"_getTop","(Lorg/keplerproject/luajava/CPtr;)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1getTop},
{"_setTop","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1setTop},

 

{"_pushValue","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1pushValue},
{"_remove","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1remove},
{"_insert","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1insert},
{"_replace","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1replace},
{"_checkStack","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1checkStack},

 

{"_xmove","(Lorg/keplerproject/luajava/CPtr;Lorg/keplerproject/luajava/CPtr;I)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1xmove},
{"_isNumber","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1isNumber},
{"_isString","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1isString},
{"_isCFunction","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1isCFunction},
{"_isUserdata","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1isUserdata},
{"_type","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1type},

 

{"_typeName","(Lorg/keplerproject/luajava/CPtr;I)Ljava/lang/String;",(void*)JAVA_org_keplerproject_luajava_LuaState__1typeName},
{"_equal","(Lorg/keplerproject/luajava/CPtr;II)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1equal},
{"_rawequal","(Lorg/keplerproject/luajava/CPtr;II)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1rawequal},
{"_lessthan","(Lorg/keplerproject/luajava/CPtr;II)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1lessthan},
{"_toNumber","(Lorg/keplerproject/luajava/CPtr;I)D",(void*)JAVA_org_keplerproject_luajava_LuaState__1toNumber},
{"_toInteger","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1toInteger},

 

{"_toBoolean","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1toBoolean},
{"_toString","(Lorg/keplerproject/luajava/CPtr;I)Ljava/lang/String;",(void*)JAVA_org_keplerproject_luajava_LuaState__1toString},
{"_objlen","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)JAVA_org_keplerproject_luajava_LuaState__1objlen},
{"_toThread","(Lorg/keplerproject/luajava/CPtr;I)Lorg/keplerproject/luajava/CPtr;",(void*)JAVA_org_keplerproject_luajava_LuaState__1toThread},
{"_pushNil","(Lorg/keplerproject/luajava/CPtr;)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1pushNil},
{"_pushNumber","(Lorg/keplerproject/luajava/CPtr;D)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1pushNumber},

 

// {"_pushInteger","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1pushInteger},
{"_pushString","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/String;)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1pushString__Lorg_keplerproject_luajava_CPtr_2Ljava_lang_String_2},
{"_pushString","(Lorg/keplerproject/luajava/CPtr;[BI)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1pushString__Lorg_keplerproject_luajava_CPtr_2_3BI},
{"_pushBoolean","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1pushBoolean},
{"_getTable","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1getTable},
{"_getField","(Lorg/keplerproject/luajava/CPtr;ILjava/lang/String;)V",(void*)JAVA_org_keplerproject_luajava_LuaState__1getField},

 

 
 {"_rawGet","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)Java_org_keplerproject_luajava_LuaState__1rawGet},
 {"_rawGetI","(Lorg/keplerproject/luajava/CPtr;II)V",(void*)Java_org_keplerproject_luajava_LuaState__1rawGetI},
 {"_createTable","(Lorg/keplerproject/luajava/CPtr;II)V",(void*)Java_org_keplerproject_luajava_LuaState__1createTable},
 {"_getMetaTable","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1getMetaTable},
 {"_getFEnv","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)Java_org_keplerproject_luajava_LuaState__1getFEnv},
 {"_setTable","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)Java_org_keplerproject_luajava_LuaState__1setTable},

 {"_setField","(Lorg/keplerproject/luajava/CPtr;ILjava/lang/String;)V",(void*)Java_org_keplerproject_luajava_LuaState__1setField},
 {"_rawSet","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)Java_org_keplerproject_luajava_LuaState__1rawSet},
 {"_rawSetI","(Lorg/keplerproject/luajava/CPtr;II)V",(void*)Java_org_keplerproject_luajava_LuaState__1rawSetI},
 {"_setMetaTable","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1setMetaTable},
 {"_setFEnv","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1setFEnv},
 {"_call","(Lorg/keplerproject/luajava/CPtr;II)V",(void*)Java_org_keplerproject_luajava_LuaState__1call},

 {"_pcall","(Lorg/keplerproject/luajava/CPtr;III)I",(void*)Java_org_keplerproject_luajava_LuaState__1pcall},
 {"_yield","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1yield},
 {"_resume","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1resume},
 {"_status","(Lorg/keplerproject/luajava/CPtr;)I",(void*)Java_org_keplerproject_luajava_LuaState__1status},
 {"_gc","(Lorg/keplerproject/luajava/CPtr;II)I",(void*)Java_org_keplerproject_luajava_LuaState__1gc},
 {"_error","(Lorg/keplerproject/luajava/CPtr;)I",(void*)Java_org_keplerproject_luajava_LuaState__1error},

 {"_next","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1next},
 {"_concat","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)Java_org_keplerproject_luajava_LuaState__1concat},
 {"_pop","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)Java_org_keplerproject_luajava_LuaState__1pop},
 {"_newTable","(Lorg/keplerproject/luajava/CPtr;)V",(void*)Java_org_keplerproject_luajava_LuaState__1newTable},
 {"_strlen","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1strlen},
 {"_isFunction","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1isFunction},

 {"_isTable","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1isTable},
 {"_isNil","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1isNil},
 {"_isBoolean","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1isBoolean},
// {"_isThread","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1isThread},
 {"_isNone","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1isNone},
 {"_isNoneOrNil","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1isNoneOrNil},

 {"_setGlobal","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/String;)V",(void*)Java_org_keplerproject_luajava_LuaState__1setGlobal},
 {"_getGlobal","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/String;)V",(void*)Java_org_keplerproject_luajava_LuaState__1getGlobal},
 {"_getGcCount","(Lorg/keplerproject/luajava/CPtr;)I",(void*)Java_org_keplerproject_luajava_LuaState__1getGcCount},
 {"_LdoFile","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/String;)I",(void*)Java_org_keplerproject_luajava_LuaState__1LdoFile},
 {"_LdoString","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/String;)I",(void*)Java_org_keplerproject_luajava_LuaState__1LdoString},
 {"_LgetMetaField","(Lorg/keplerproject/luajava/CPtr;ILjava/lang/String;)I",(void*)Java_org_keplerproject_luajava_LuaState__1LgetMetaField},

 {"_LcallMeta","(Lorg/keplerproject/luajava/CPtr;ILjava/lang/String;)I",(void*)Java_org_keplerproject_luajava_LuaState__1LcallMeta},
 {"_Ltyperror","(Lorg/keplerproject/luajava/CPtr;ILjava/lang/String;)I",(void*)Java_org_keplerproject_luajava_LuaState__1Ltyperror},
 {"_LargError","(Lorg/keplerproject/luajava/CPtr;ILjava/lang/String;)I",(void*)Java_org_keplerproject_luajava_LuaState__1LargError},
 {"_LcheckString","(Lorg/keplerproject/luajava/CPtr;I)Ljava/lang/String;",(void*)Java_org_keplerproject_luajava_LuaState__1LcheckString},
 {"_LoptString","(Lorg/keplerproject/luajava/CPtr;ILjava/lang/String;)Ljava/lang/String;",(void*)Java_org_keplerproject_luajava_LuaState__1LoptString},
 {"_LcheckNumber","(Lorg/keplerproject/luajava/CPtr;I)D",(void*)Java_org_keplerproject_luajava_LuaState__1LcheckNumber},

 {"_LoptNumber","(Lorg/keplerproject/luajava/CPtr;ID)D",(void*)Java_org_keplerproject_luajava_LuaState__1LoptNumber},
 {"_LcheckInteger","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1LcheckInteger},
 {"_LoptInteger","(Lorg/keplerproject/luajava/CPtr;II)I",(void*)Java_org_keplerproject_luajava_LuaState__1LoptInteger},
 {"_LcheckStack","(Lorg/keplerproject/luajava/CPtr;ILjava/lang/String;)V",(void*)Java_org_keplerproject_luajava_LuaState__1LcheckStack},
 {"_LcheckType","(Lorg/keplerproject/luajava/CPtr;II)V",(void*)Java_org_keplerproject_luajava_LuaState__1LcheckType},
 {"_LcheckAny","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)Java_org_keplerproject_luajava_LuaState__1LcheckAny},

 {"_LnewMetatable","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/String;)I",(void*)Java_org_keplerproject_luajava_LuaState__1LnewMetatable},
 {"_LgetMetatable","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/String;)V",(void*)Java_org_keplerproject_luajava_LuaState__1LgetMetatable},
 {"_Lwhere","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)Java_org_keplerproject_luajava_LuaState__1Lwhere},
 {"_Lref","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1Lref},
 {"_LunRef","(Lorg/keplerproject/luajava/CPtr;II)V",(void*)Java_org_keplerproject_luajava_LuaState__1LunRef},
 {"_LgetN","(Lorg/keplerproject/luajava/CPtr;I)I",(void*)Java_org_keplerproject_luajava_LuaState__1LgetN},

 {"_LsetN","(Lorg/keplerproject/luajava/CPtr;II)V",(void*)Java_org_keplerproject_luajava_LuaState__1LsetN},
 {"_LloadFile","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/String;)I",(void*)Java_org_keplerproject_luajava_LuaState__1LloadFile},
 {"_LloadBuffer","(Lorg/keplerproject/luajava/CPtr;[BJLjava/lang/String;)I",(void*)Java_org_keplerproject_luajava_LuaState__1LloadBuffer},
 {"_LloadString","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/String;)I",(void*)Java_org_keplerproject_luajava_LuaState__1LloadString},
 {"_Lgsub","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",(void*)Java_org_keplerproject_luajava_LuaState__1Lgsub},
 {"_LfindTable","(Lorg/keplerproject/luajava/CPtr;ILjava/lang/String;I)Ljava/lang/String;",(void*)Java_org_keplerproject_luajava_LuaState__1LfindTable},

 {"_openBase","(Lorg/keplerproject/luajava/CPtr;)V",(void*)Java_org_keplerproject_luajava_LuaState__1openBase},
 {"_openTable","(Lorg/keplerproject/luajava/CPtr;)V",(void*)Java_org_keplerproject_luajava_LuaState__1openTable},
 {"_openIo","(Lorg/keplerproject/luajava/CPtr;)V",(void*)Java_org_keplerproject_luajava_LuaState__1openIo},
 {"_openOs","(Lorg/keplerproject/luajava/CPtr;)V",(void*)Java_org_keplerproject_luajava_LuaState__1openOs},
 {"_openString","(Lorg/keplerproject/luajava/CPtr;)V",(void*)Java_org_keplerproject_luajava_LuaState__1openString},
 {"_openMath","(Lorg/keplerproject/luajava/CPtr;)V",(void*)Java_org_keplerproject_luajava_LuaState__1openMath},

 {"_openDebug","(Lorg/keplerproject/luajava/CPtr;)V",(void*)Java_org_keplerproject_luajava_LuaState__1openDebug},
 {"_openPackage","(Lorg/keplerproject/luajava/CPtr;)V",(void*)Java_org_keplerproject_luajava_LuaState__1openPackage},
 {"_openLibs","(Lorg/keplerproject/luajava/CPtr;)V",(void*)Java_org_keplerproject_luajava_LuaState__1openLibs},
 {"luajava_open","(Lorg/keplerproject/luajava/CPtr;I)V",(void*)Java_org_keplerproject_luajava_LuaState_luajava_1open},
 {"_getObjectFromUserdata","(Lorg/keplerproject/luajava/CPtr;I)Ljava/lang/Object;",(void*)Java_org_keplerproject_luajava_LuaState__1getObjectFromUserdata},
 {"_isObject","(Lorg/keplerproject/luajava/CPtr;I)Z",(void*)Java_org_keplerproject_luajava_LuaState__1isObject},

 {"_pushJavaObject","(Lorg/keplerproject/luajava/CPtr;Ljava/lang/Object;)V",(void*)Java_org_keplerproject_luajava_LuaState__1pushJavaObject},
 {"_pushJavaFunction","(Lorg/keplerproject/luajava/CPtr;Lorg/keplerproject/luajava/JavaFunction;)V",(void*)Java_org_keplerproject_luajava_LuaState__1pushJavaFunction},
 {"_isJavaFunction","(Lorg/keplerproject/luajava/CPtr;I)Z",(void*)Java_org_keplerproject_luajava_LuaState__1isJavaFunction}
};

 

OK!最後將org.keplerproject.luajava包名和類名也一同改改(當然如果 改 了也要注意修改so中的函數簽名),再给so取個其他的什麼名字^_^。希望本文對您有所幫助。

 
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()