How to integrate Lua into your own Android game

Source: Internet
Author: User
Tags android games

How to makeLuaIntegrate to your ownAndroid gamesIs the content to be introduced in this article.GamesIntroducing 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.

No more detailsAndroidDevelopment Environment configuration 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:

 
 
  1. Native Development Kit r6http://developer.android.com/sdk/ndk/index.html   
  2. Cygwin 1.7 http://www.cygwin.com/   
  3. Lua 5.1.4 sourcehttp://www.lua.org/   
  4. 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:

 
 
  1. LOCAL_PATH := $(call my-dir)   
  2. include $(CLEAR_VARS)   
  3. LOCAL_ARM_MODE  := arm 
  4. LOCAL_MODULE    := luajava-1.1  
  5. LOCAL_SRC_FILES := lapi.c \      
  6.     lauxlib.c \      
  7.     lbaselib.c \  
  8.     lcode.c \      
  9.     ldblib.c \      
  10.     ldebug.c \      
  11.     ldo.c \      
  12.     ldump.c \      
  13.     lfunc.c \      
  14.     lgc.c \      
  15.     linit.c \      
  16.     liolib.c \      
  17.     llex.c \      
  18.     lmathlib.c \      
  19.     lmem.c \      
  20.     loadlib.c \      
  21.     lobject.c \      
  22.     lopcodes.c \      
  23.     loslib.c \      
  24.     lparser.c \      
  25.     lstate.c \      
  26.     lstring.c \      
  27.     lstrlib.c \      
  28.     ltable.c \      
  29.     ltablib.c \      
  30.     ltm.c \      
  31.     lundump.c \      
  32.     lvm.c \      
  33.     lzio.c \      
  34.     print.c \      
  35. 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.

 
 
  1. package com.android.hello;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.widget.TextView;  
  5. import org.keplerproject.luajava.*;  
  6. public class HelloAndroidActivity extends Activity {      
  7. /** Called when the activity is first created. */      
  8.  @Override       
  9.  public void onCreate(Bundle savedInstanceState) {          
  10.    LuaState L = LuaStateFactory.newLuaState();          
  11.    L.openLibs();          
  12.    L.LdoString("text = 'Hello Android, I am Lua.'");          
  13.    L.getGlobal("text");          
  14.    String text = L.toString(-1);          
  15.   super.onCreate(savedInstanceState);          
  16.   TextView tv = new TextView(this);          
  17.   tv.setText(text);          
  18.   setContentView(tv);      
  19.  }  

The following figure shows the running result:

HereAndroidUnderLuaThe environment has been configured, and how to use it depends on the design of programmers.

Summary: How to makeLuaIntegrate to your ownAndroid gamesThe content has been introduced, and it will be helpful for you to learn through this article!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.