Experience summary of using lua to call Java code under Android platform [easy to understand]

Hello, everyone. Meet again. I'm Jun Quan.

Dynamic language with its flexibility and configurability. Convenient debugging can bring great convenience to development. Suppose you use it well. It can greatly improve the efficiency of development.

No wonder there is no software development as complex as game development that does not integrate scripting language.

Among them, lua is small and flexible. Easy expansion and easy embedding are used in most game development.

for me. For a person who is fully aware of the power of dynamics, integrating a script language into software development has become a very important and meaningful work.

But on the Android platform. After trying python, I found that although integration is not difficult, it still feels a little big. A dynamic library has more than 3M, and the python library has to be tailored by itself,

Compilation is not very convenient. So recently, we have gradually focused on tools like lua. Compared with lua. Although lua is not as pure as Python, it feels that this disadvantage has become its advantage.

It has to be said that lua is more useful than python on the Android platform. As the saying goes, there is no best. Only the most suitable.

Python pursues pure at the same time, but it also hinders its development.

For every developer who regards efficiency as his life. Convenience and usefulness are the king.

Because this means that the software development can be completed as soon as possible, and the risk of software development can be reduced. We can reduce the pressure of development, stay up less nights and spend more time with our girlfriends (HA, there are girlfriends in the program APE).

Let's go back to the title and see how to integrate lua and java with luajava on Android platform.

luajava has been ported on android platform, and the code can be downloaded from https://github.com/mkottman/AndroLua Download.

As for compiling and adding luajava to your own project, I won't say much. Just refer to AndroLua's code. Next, let's focus on how to use lua to interact with android:

The simplest example:

function launchSetting(context)
    intent = luajava.newInstance("android.content.Intent")
    c = luajava.newInstance("android.content.ComponentName","com.android.settings", "com.android.settings.Settings")
    intent:setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
    intent:setComponent(c)
    context:startActivity(intent)
end

This demo sample can be used to launch settings

Line by line:

    intent = luajava.newInstance("android.content.Intent")

Create an Intent instance: equivalent to Intent = new Intent () in Java

    c = luajava.newInstance("android.content.ComponentName","com.android.settings", "com.android.settings.Settings")
 Create a ComponentName Instance, equivalent to Java code: ComponentName c = new ComponentName("com.android.settings", "com.android.settings.Settings") 
     intent:setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
 
 Start a new Activity example 
     intent:setComponent(c)
 
     context:startActivity(intent)
 
 start-up Activity 

Students who are just beginning to learn lua may be right. " "And" is a little strange. Here is a simple analysis, "." Is used to call the method of the class, and "" is used to call the method of the object. Look at this line of code:

   intent:setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
 

The above code is equivalent to the following code:

  intent.setFlags(intent,intent.FLAG_ACTIVITY_NEW_TASK);
 

The hypothesis is written as:

intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);

An error is returned:

Not a valid OO function call

Due to the first parameter intent FLAG_ ACTIVITY_ NEW_ Task is not an object.

Let's take another example:

require 'import'


button_cb = {}
function button_cb.onClick(ev)
    print('hello,world')
    launchSetting(activity)
end


local id = luajava.bindClass("sk.kottman.androlua.R$id")
local launch = activity:findViewById(id.launchButton)


buttonProxy = luajava.createProxy("android.view.View$OnClickListener", button_cb)
launch:setOnClickListener(buttonProxy)

This example shows luajava For the use of createproxy, the sample registers the Listener of a button Click

 Note the following code:
 
 local id = luajava.bindClass("sk.kottman.androlua.R$id")
 
 local launch = activity:findViewById(id.launchButton) 

Because ID is a static class in R.java, it cannot be written as: sk kottman. androlua. R.id:

 In addition, pay attention to the reference method of the class in the class, such as:

android.view.View$OnClickListener

Publisher: full stack programmer, stack length, please indicate the source for Reprint: https://javaforall.cn/116113.html Original link: https://javaforall.cn

Added by semlabs on Sun, 20 Feb 2022 09:33:39 +0200