Calling Lua Script From an Android Application - android

Let me first of all clarify a few things:
I am not trying to run a a Lua script from a command line.
I am not trying to invoke any android functions from Lua
So with that out of the way, here is what I am trying to do.
From an Android Activity invoke directly OR indirectly (JNI/SL4A) a Lua script and get back the results in the activity.
Now looking at documentation for SL4A I see a few drawbacks:
1) I cannot find the documentation saying that it lets one programmatically call Lua.
2) It looks like SL4A might need to install as a separate application (not too seemless).
The only other option I see is to NDK cross compile all of Lua and then try to invoke it in C code in some manner.

You may want to look at my sample project AndroLua. It contains a Lua interpreter embedded directly into an Android application using the Android NDK. Only very small changes were necessary to successfully embed it into the Android application.
In order to actually use Lua from your application, LuaJava is also bundled to allow you to use Lua from Java and the other way round.
Look at the application to see an example how I override the print function to allow an output to a TextView instead of a console.
Update: loading modules
I assume the module you want to load is implemented in Lua. The standard Lua techniques for module loading work as usual - you just have to modify the package.path to your application data directory (or wherever you want to store your scripts/modules).
Imagine that you have a module called hello.lua in the application data directory:
$ adb shell
# cd /data/data/sk.kottman.androlua
# cat hello.lua
module(..., package.seeall)
function greet(name)
print('Hello ' .. name)
end
#
Then try running this code in the interpreter:
-- add the data directory to the module search path
package.path = '/data/data/sk.kottman.androlua/?.lua;'..package.path
-- load the module
require 'hello'
-- run a function, should show "Hello Lua!"
hello.greet('Lua!')

Related

Is there a way to get data from sensors from command line?

In Android, I know it's possible to use Runtime.getRuntime().exec(...) to execute native command line on android system like echo or ls.
I wonder if is there is any way to get data from any sensor module (like photo or gps) not from Android API (through Java or Kotlin), but by executing a command line with Runtime.getRuntime().exec(...). Is there a way to do it?
Technically all that the Android framework ( + HAL ) does is communicate via system calls with the kernel.
It would certainly be possible for you to write a binary ( C/C++) that does that communication for you, bypassing the framework.
And then you could call that binary with Runtime.getRuntime().exec(...) ( assuming rooted and have access ).
There aren't many tools to access the sensors like that ( expect maybe some vendors might have for testing). The only thing that comes to mind that you could use to get some information is by calling dumpsys in shell. This will give you lots of info about the current state of the system, and for example some location data as explaind in this answer
You can pack binary executables into your apk and launch them via Runtime.getRuntime().exec(...). Depending on the data you want to read it may be possible to implement an C/C++ program which reads /proc or /dev. If you completely statically link this executable (use i.e. musl libc) you can call it from your android app to read the data you need.

Interact with C++ program from android using parcelable class

Basically, I have a C++ program that finds the sum of two numbers given. I need to provide the two numbers to the C++ program as input using my android app and then display the result in my android app. I guess I need to use parcelable class. Can someone please tell me the steps to be followed?
Edit: I forgot to mention that the C++ program that I intend to communicate with is an executable program (sum.exe)
To run a C++ executable on Android, you can use something like Runtime.exec("sum 1 2"). There are a lot of tutorials, e.g. https://www.mkyong.com/java/how-to-execute-shell-command-from-java/. The output (stdout and stderr) can be parsed, too. A more sophisticated way is to use ProcessBuilder, but the idea is the same.
If you want your executable to keep running in background, and send the numbers to crunch once in a while, you can either use input pipe, or some IPC protocol. Shared memory works well, see e.g. How to use Shared Memory (IPC) in Android.
You can use JNI code, take a look here:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025
where you can find super simple code with C++ being called via JNI wrapper from Java.

generic path for files

Im not sure what it is called but ill do my best to explain it. I did search here on stack exchange and found this answer for an allias for path but i dont want to set a variable for it and i already know that. create alias for path
What I need is and its been along time since I seen it but its used with a % or $ or something that when the program runs from the directory it knows where the directory for the game files are. It didn't matter what the directory the program is in as long as the directory 'gameFiles' is in that directory it will work.
Here's my path:
"/storage/emulated/0/MyGame/MyHackGame/jni/gameFiles/oscom.txt"
I think its something like:
"%or$/gameFiles/oscom.txt"
The main problem is I have a project on source forge and don't want my developers to have to change these paths like 100 times to run the program and then I'll have to change them back.
Also iam using aide for android to make the program and using the standard c++ libaray do this might be difficult to do. I'm not even sure if I can add libarays with aide and native code or how to do it.
Use symlinks as Eugene has suggested or modify build scripts to generate user specific paths into binaries or spearate files at build time. These are ignored by version tracking, so no more double mills. Another way might be checking env variables at runtime.

How can I use code template in android when creating project from command line?

I have used android code templates in eclipse such as blank activity,login activity etc. I want to use these templates while creating project from command line. How can I do this?
You can write and render these templates (or any other template you want to create) using Ruby's ERB gem.
From http://www.ictforu.com/index.php/Ruby/erb-ruby.html:
ERB is an abbreviation for Embedded Ruby
ERB is a feature of Ruby that enables user to dynamically generate any
kind of text from templates. The templates combine plain text with
Ruby code for variable substitution and flow control, which makes them
easy to write and maintain.
ERB is commonly used in Rails platform to generate the webpages, It is
also used in generating XML files, Source code, etc.
Although it's quite common in Ruby on Rails you can use it wherever you want ;-)
I suggest you to check:
ERB documentation
How do I execute ruby template files (ERB) without a web server from command line?

How to call Python script from Android

Is it possible in Android to call Python script? I have already some scripts in Python 2.7 and I want to call that from Android(that script create file and fills with data). To be more specific I am trying to execute Python script on phone, that script connects to some site, download data and do some intelligence and then create file with new data(json on phone).
With googles SL4A-project, it's possible to have Python-scripts execute on your Android phone.
Parts of the Android API are wrapped for Python (but not all of it)
You can embed Python-scripts in your application (sounds like your approach).

Categories

Resources