{"id":15252,"date":"2023-12-01T11:06:50","date_gmt":"2023-12-01T03:06:50","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=15252"},"modified":"2023-12-01T11:06:50","modified_gmt":"2023-12-01T03:06:50","slug":"lua%e5%bc%80%e5%8f%91%e5%ae%89%e5%8d%93app%e5%85%a5%e9%97%a8%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/15252\/","title":{"rendered":"lua\u5f00\u53d1\u5b89\u5353app\u5165\u95e8\u4ecb\u7ecd"},"content":{"rendered":"

Lua\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u811a\u672c\u8bed\u8a00\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u5e94\u7528\u9886\u57df\uff0c\u5305\u62ec\u6e38\u620f\u5f00\u53d1\u3001\u79fb\u52a8\u5e94\u7528\u5f00\u53d1\u7b49\u3002\u5728\u5b89\u5353app<\/a>Android\u5e94\u7528\u5f00\u53d1\u4e2d\uff0cLua\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u79cd\u53ef\u9009\u7684\u8bed\u8a00\u6765\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\uff0c\u901a\u8fc7\u4e0eJava\u6df7\u5408\u7f16\u7a0b\u7684\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u5728Android\u5e73\u53f0\u4e0a\u7f16\u5199\u51fa\u9ad8\u6548\u3001\u4f18\u79c0\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n

\u4e0b\u9762\u662f\u4e00\u4e2a Android \u5e94\u7528\u5f00\u53d1\u7684\u7b80\u5355\u6d41\u7a0b\uff1a<\/p>\n

1. \u4f7f\u7528 Android Studio \u521b\u5efa\u4e00\u4e2a Android \u9879\u76ee<\/p>\n

2. \u4f7f\u7528 Java \u7f16\u5199 Android \u5e94\u7528\u7684\u6838\u5fc3\u4ee3\u7801\u548c\u903b\u8f91\uff0c\u5982 UI \u754c\u9762\u548c\u5e94\u7528\u903b\u8f91\u7b49<\/p>\n

3. \u4f7f\u7528 Lua \u7f16\u5199\u4e0e Java \u96c6\u6210\u7684\u811a\u672c\uff0c\u63a7\u5236 Android \u5e94\u7528\u7a0b\u5e8f\u7684\u884c\u4e3a\u548c\u4ea4\u4e92<\/p>\n

4. \u5c06 Lua \u4ee3\u7801\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u52a0\u8f7d\u548c\u6267\u884c<\/p>\n

\u4ee5\u4e0b\u662f\u66f4\u8be6\u7ec6\u7684\u6b65\u9aa4\uff1a<\/p>\n

1. \u4e0b\u8f7d\u5e76\u5b89\u88c5 Android Studio<\/p>\n

2. \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Android \u9879\u76ee\uff0c\u547d\u540d\u4e3a MyAndroidApp<\/p>\n

3. \u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a \/assets\/luaScripts<\/p>\n

<\/figure>\n<\/p>\n

\u7684\u76ee\u5f55\uff0c\u7528\u4e8e\u5b58\u653e Lua \u811a\u672c\u6587\u4ef6<\/p>\n

4. \u4e3a Android \u9879\u76ee\u96c6\u6210 Lua<\/p>\n

1\uff09\u5728 build.gradle \u6587\u4ef6\u4e2d\u6dfb\u52a0\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n

“`gradle<\/p>\n

repositories {<\/p>\n

maven {<\/p>\n

url ‘https:\/\/github.com\/ltpr\/liblua-android’<\/p>\n

}<\/p>\n

}<\/p>\n

dependencies {<\/p>\n

implementation ‘com.github.ltpr:liblua:5.1.5.53a’<\/p>\n

}<\/p>\n

“`<\/p>\n

2\uff09\u521b\u5efa LuaRuntime \u7c7b\uff0c\u7528\u4e8e\u52a0\u8f7d\u548c\u6267\u884c Lua \u811a\u672c\u6587\u4ef6\uff1a<\/p>\n

\u5b89\u5353app\u5236\u4f5c<\/a> “`java<\/p>\n

public class LuaRuntime {<\/p>\n

private static final String SCRIPTS_DIRECTORY = “luaScripts\/”;<\/p>\n

static {<\/p>\n

System.loadLibrary(“lua”);<\/p>\n

}<\/p>\n

public static void executeScriptFromAssets(Context context, String fileName) throws IOException {<\/p>\n

AssetManager assetManager = context.getAssets();<\/p>\n

InputStream inputStream = assetManager.open(SCRIPTS_DIRECTORY + fileName);<\/p>\n

byte[] bytes = new byte[inputStream.available()];<\/p>\n

inputStream.read(bytes);<\/p>\n

String script = new String(bytes);<\/p>\n

executeScript(script);<\/p>\n

}<\/p>\n

public static native void executeScript(String script);<\/p>\n

}<\/p>\n

“`<\/p>\n

5. \u7f16\u5199 Lua \u811a\u672c<\/p>\n

1\uff09\u5728 luaScripts \u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a myscript.lua \u7684\u6587\u4ef6<\/p>\n

2\uff09\u5728 myscript.lua \u6587\u4ef6\u4e2d\u7f16\u5199 Lua \u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

“`lua<\/p>\n

function calculate()<\/p>\n

return 2 + 3<\/p>\n

end<\/p>\n

“`<\/p>\n

6. \u8c03\u7528 Lua \u811a\u672c<\/p>\n

1\uff09\u5728 MainActivity.java \u4e2d\u8c03\u7528 Lua \u811a\u672c\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

“`java<\/p>\n

public class MainActivity extends AppCompatActivity {<\/p>\n

@Override<\/p>\n

protected void onCreate(Bundle savedInstanceState) {<\/p>\n

super.onCreate(savedInstanceState);<\/p>\n

setContentView(R.layout.activity_main);<\/p>\n

try {<\/p>\n

LuaRuntime.executeScriptFromAssets(this, “myscript.lua”);<\/p>\n

} catch (IOException e) {<\/p>\n

e.printStackTrace();<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

7. \u8fd0\u884c Android \u5e94\u7528\u7a0b\u5e8f<\/p>\n

\u5728\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0cAndroid \u7cfb\u7edf\u5c06\u8bfb\u53d6 myscript.lua \u811a\u672c\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7 LuaRuntime \u7c7b\u52a0\u8f7d\u548c\u6267\u884c\u6587\u4ef6\u3002\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u5c06\u8f93\u51fa\u7ed3\u679c “5”\u3002<\/p>\n

\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u8bf4\u660e\u4e86 Android \u5e94\u7528\u7a0b\u5e8f\u5982\u4f55\u901a\u8fc7 Lua \u811a\u672c\u63a7\u5236\u548c\u6267\u884c\u3002Lua \u53ef\u4ee5\u5728 Android \u4e2d\u5b9e\u73b0\u8bb8\u591a\u590d\u6742\u7684\u64cd\u4f5c\u548c\u4efb\u52a1\uff0c\u4f8b\u5982\u6e38\u620f AI\uff0c\u52a8\u6001\u7528\u6237\u754c\u9762\u7b49\u7b49\u3002\u754c\u9762\u7b49\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Lua\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u811a\u672c\u8bed\u8a00\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u5e94\u7528\u9886\u57df\uff0c\u5305\u62ec\u6e38\u620f\u5f00\u53d1\u3001\u79fb\u52a8\u5e94\u7528\u5f00\u53d1\u7b49\u3002\u5728Android\u5e94\u7528\u5f00\u53d1\u4e2d\uff0cLua\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u79cd\u53ef\u9009\u7684\u8bed\u8a00\u6765\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\uff0c\u901a\u8fc7\u4e0eJava\u6df7\u5408\u7f16\u7a0b\u7684\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u5728Andr<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[14,22404,171,283,22405],"class_list":["post-15252","post","type-post","status-publish","format-standard","hentry","category-zuoapp","tag-14","tag-app","tag-171","tag-283"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/15252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=15252"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/15252\/revisions"}],"predecessor-version":[{"id":15278,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/15252\/revisions\/15278"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=15252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=15252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=15252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}