How to integrateLuaImplementationAndroid gamesIs the content to be introduced in this article, mainly to learnLUAIntegration, how to achieve their ownAndroid games. NumerousGamesIntroducing the scripting language as a solution for fast content release and flexible scaling, we are looking for Android Application IntegrationLuaIn script language solutions, a large amount of fragmented information does not provide developers with a clear direction.
Here, I will write down my plan completed by integrating the data, hoping to help the programmers who need it. You are welcome to provide a faster solution. I will not repeat the configuration of the Android development environment andLuaIntroduction to the scripting language. If you need to know more about it, you can use Google.
My development environment is Window, listing the tools and components we will use:
- Native Development Kit r6http://developer.android.com/sdk/ndk/index.html
- Cygwin 1.7 http://www.cygwin.com/
- Lua 5.1.4 sourcehttp://www.lua.org/
- luajava 1.1 source http://www.keplerproject.org/luajava/manual.html
This section briefly introduces several basic concepts involved in the solution:
Lua is implemented in C, so if we do not intend to rewrite the interpreter in Java, we need a way to enable good communication between C and Java, java programmers must have come up with JNIJava Native Interface ). You may have used JNI in Linux and Windows, but the process is not complex: write the Java file and declare the native method in it → use javah to generate the C header file → write the implementation for the function declared in the C file → compile the C code to generate a dynamic link library, in this way, the native method can be used in Java.
In Android, we also need to go through these steps. However, when compiling dynamic link libraries, we need the NDK tool provided by Google. To use NDK in Windows, you also need to install Cygwin in UNIX simulation environment. We will also use luajava, an open-source project that well encapsulates Lua.
The detailed steps are as follows:
Install Cygwin. The downloaded setup is an installer. It is responsible for finding and installing the required files online or locally. We recommend that you download the files to your local computer for future use, during the installation process, the root node of the All-node tree node must be determined when the installation package is selected. In the Install status, the required package is not installed. If the installation takes a long time, wait patiently until Cygwin is successfully installed.
Install NDK. NDK is a compressed file. After downloading and decompressing it, we recommend that you extract it to a directory that facilitates input ).
Download the above lua and luajava source code and decompress it to any directory.
Create a directory named luajava4android in any directory), create a subdirectory named jni under the Directory and specify the name as jni), and upload the C files in lua and luajava, including header files and source files, to the directory, and create Android. mk file, copy the following content to the file:
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_ARM_MODE := arm
- LOCAL_MODULE := luajava-1.1
- LOCAL_SRC_FILES := lapi.c \
- lauxlib.c \
- lbaselib.c \
- lcode.c \
- ldblib.c \
- ldebug.c \
- ldo.c \
- ldump.c \
- lfunc.c \
- lgc.c \
- linit.c \
- liolib.c \
- llex.c \
- lmathlib.c \
- lmem.c \
- loadlib.c \
- lobject.c \
- lopcodes.c \
- loslib.c \
- lparser.c \
- lstate.c \
- lstring.c \
- lstrlib.c \
- ltable.c \
- ltablib.c \
- ltm.c \
- lundump.c \
- lvm.c \
- lzio.c \
- print.c \
- luajava.c include $(BUILD_SHARED_LIBRARY)
Run Cygwin in the Cygwin installation directory. bat enters the command line interface, enters the luajava4android path just created, and executes ndk-build. After the build is successful, a lib directory is generated under the directory. so files with the suffix are the dynamic link library files we need.
Import the java source code in luajava to your Android project, create the \ libs \ armeabi \ structure path under the project directory, and upload the dynamic link library file to refresh the project, this completes all the steps and is not complex. You can write a simple instance to verify that the Lua environment has been set up.
- package com.android.hello;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- import org.keplerproject.luajava.*;
- public class HelloAndroidActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- LuaState L = LuaStateFactory.newLuaState();
- L.openLibs();L.LdoString("text = 'Hello Android, I am Lua.'");
- L.getGlobal("text");String text = L.toString(-1);
- super.onCreate(savedInstanceState);
- TextView tv = new TextView(this);
- tv.setText(text);
- setContentView(tv);
- }
- }
Summary: how to integrateLuaImplementationAndroid gamesI hope this article will help you!