Corona Enterprise — Android Project Structure

This guide explains how a Corona Enterprise for Android project fits together.

Files/Directories

The following elements apply to a Corona Enterprise project:

Corona Project Files

  • Corona/ — A normal Corona project consists of your Lua files such as main.lua, along with additional resource files like images, audio, video, etc. By default, build.gradle (Module: App) is set up to assume that these files reside inside a folder called Corona that sits alongside the android folder.

Plugins

For plugins, there are additional files and directories of interest:

  • android/plugin/build.gradle — This is the build script that creates the plugin. The output is a .jar file. The only files included in the .jar are those located in plugin/src/main/, whether they are code or resources.
  • android/plugin/src/main/AndroidManifest.xml — This is where you set up the plugin's package name.
  • android/plugin/libs/ — This is where third-party (Corona) .jar plugins that your plugin relies upon should go.
  • android/plugin/src/main/jniLibs/ — This is where third-party (Corona) .so plugins that your plugin relies upon should go.
  • android/plugin/src/main/java/plugin/library/LuaLoader.java — This is the code for the Lua library plugin.library on the Android side.

Walkthrough

This brief walkthrough outlines the flow of the CoronaEnterprise/ProjectTemplates/App/ project for Android:

  1. android/app/src/main/java/com/mycompany/app/CoronaApplication.java

    At launch time, CoronaApplication.java is instantiated. It adds a private class to be notified of various app-level events like app suspend and resume.

    Right before main.lua is invoked, the onCreate() method is invoked. At this point, OpenGL is set up and all Corona frameworks are available.

  2. Corona/main.lua

    In the Lua code, plugin.library is loaded via require(). The Corona engine will then look for a corresponding Java class called plugin.library.LuaLoader and invoke it. The name of this class is dynamic and is constructed using the original library name passed to require() as the package name for a LuaLoader class. The LuaLoader class is expected to implement the JNLua interface com.naef.jnlua.JavaFunction.

  3. android/plugin/src/main/java/plugin/library/LuaLoader.java

    When the fully-qualified class path for LuaLoader is resolved, the class is instantiated with the default constructor. The invoke() method is then called by Lua via JNLua and does all the heavy lifting, for example creating the Lua library table, registering the Lua methods like show(), and then leaving the table at the top of the Lua stack.

    The LuaLoader class is instantiated once for the lifetime of the process, much like a .dll or .so file is typically loaded once for the lifetime of the process.

    The invoke() method is called once for each Lua state that does a require(). This corresponds to being called once each time the CoronaActivity is instantiated.