欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android嵌入lua 博客分类: android luaandroidluajavajniluaj 

程序员文章站 2024-03-25 20:30:46
...

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标识的函数去除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取个其他的什么名字^_^。希望本文对您有所帮助。