Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Can you convert/translate Corona app to Android Native?
Started by amirfarazmand Aug 29 2018 08:22 PM

15 replies to this topic
android native
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

amirfarazmand

[GLOBAL: userInfoPane.html]
amirfarazmand
  • Contributor

  • 111 posts
  • Corona SDK

Is it possible to write up code in Corona and convert it to Android native? Is it proprietary?



[TOPIC: post.html]
#2

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Contributor

  • 824 posts
  • Corona SDK

0_o. I am a bit confused so I'll answer it twice:

 

1. No, you can't write Lua corona code and auto-translate that to native java android code.

2. Yes, you can write Lua corona code and build a native android app/game.



[TOPIC: post.html]
#3

amirfarazmand

[GLOBAL: userInfoPane.html]
amirfarazmand
  • Contributor

  • 111 posts
  • Corona SDK

I was looking for first answer. I assume when you write Lua it is translated to Java before running anyway. And the graphics are also converted to XML files. Isnt this correct?



[TOPIC: post.html]
#4

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Contributor

  • 824 posts
  • Corona SDK

No. It isn't translated to java in the way you think. In most cases, Corona is creating OpenGL objects that run inside their engine from the Lua. I am fairly sure they create these on the fly. Otherwise, they would have to maintain conversion code for all the platform they support including the simulator and the ios emulator. When you build they take your lua code, the appropriate corona engine and the other bits (native plugins, native UI elements) and package it for the appropriate platform. No code conversion is taking place.



[TOPIC: post.html]
#5

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,838 posts
  • Corona Staff

Corona is not a translator. It's a combination of things.

 

First, Lua is a language that you write, but binary C++ code parses the Lua and executes it. It handles memory allocation, doing math, etc. Many items call things in the standard C libraries. For instance, string.format() is using the standard C library function sprintf() to do its work. Display objects are also C++ based code that turns your Lua statements like display.newImageRect() into various OpenGL calls to create an image on screen and manipulate it.

 

Next there are operating system level items, like opening the camera to take a photo where we've written that code in Java for Android, Objective C for iOS and then part of that code allows Lua to be bridged to the Java/Objective C code.

When you build the app, we output an Android binary package that includes any images and audio files, the necessary Android package XML files (i.e. AndroidManifest.xml) and packages it into a .apk and code signs the file.

 

The Simulator does all this work for you, so you only have to worry about your Lua side code, providing artwork and learning our API calls. Hit a button, get a valid APK file.

 

If you need more, we do offer "native" builds, where you use a special project that we've set up that has a folder for you to put your Corona Lua code, images and audio files in. But you use Android Studio to build the project instead of the Corona simulator. This allows you to write your own Java code and bridge it to where your Lua code can call your Java routines. You can also make your own Java-based plugins that your Lua code can call as well.

 

Rob



[TOPIC: post.html]
#6

amirfarazmand

[GLOBAL: userInfoPane.html]
amirfarazmand
  • Contributor

  • 111 posts
  • Corona SDK

Thanks Rob and agramonte.

 

I tried the Andorid native but even at start I get this error when trying to open through Android 

Failed to generate resource.car  

 

Is there a good video instruction how to start the native side?



[TOPIC: post.html]
#7

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Contributor

  • 824 posts
  • Corona SDK

My opinion only: All of it is not really Corona specific. You can search on youtube for how to build native android apps, get familiar with gradle, how jnilibs work in an android project, how to run extra scripts while building and the structure of a typical android project. Once you learn all that the structure and how the native build works will be easy to understand. All of the corona magic is inside a set of .so files the rest of the native build is standard android build stuff, a little code glue, and some scripts.

 

Getting away from all that is why I ended up in Corona and doing mostly Corona Simulator builds.


  • roaminggamer likes this

[TOPIC: post.html]
#8

amirfarazmand

[GLOBAL: userInfoPane.html]
amirfarazmand
  • Contributor

  • 111 posts
  • Corona SDK

I know enough about android native. However, every android studio upgrade creates whole lot of issues that you rarely see in corona. I was hoping translation does all the muscle works.
So to develop native, you do as usual in corona and only access the classes? I imagine you can’t access objects at the same time in android and corona, is it correct?

[TOPIC: post.html]
#9

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Contributor

  • 824 posts
  • Corona SDK

You can send messages back and forth between Corona and the android side and then using those messages to appropriately modify objects on either side. But no you can't, for example, change the color of an object in lua from the android side directly.



[TOPIC: post.html]
#10

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,838 posts
  • Corona Staff

This tutorial is really, really old. It's not based on our current Corona Native Android Studio builds, but the old Enterprise based ANT command line builds. This is one of the reasons this tutorial is not available on our site, but someone cloned it, so you still can access it:

 

http://www.sdknews.com/cross-platform/corona/tutorial-corona-enterprise-quickstart-android

 

Most of the tutorial isn't going to be of any use to you at all, but if you scroll down to the "Do something interesting" section, that's where you can find the actual Lua, Java and bridge code between that shows how to talk to native API's from Lua. It's a very simple example. You would of course still have to copy the latest App template package from the Corona/Native folder to your working project, update the AndroidManifest.XML and such in Android Studio. 

 

The App project should work in up-to-date Android studios.

 

Rob



[TOPIC: post.html]
#11

amirfarazmand

[GLOBAL: userInfoPane.html]
amirfarazmand
  • Contributor

  • 111 posts
  • Corona SDK

I tried it but I get this error on Windows 10.

Process 'command 'C:\Program Files (x86)\Corona Labs\Corona\Native\Corona\win\bin/CoronaBuilder.exe'' finished with non-zero exit value 1
 
resulting in error:
ERROR: Failed to generate resource.car
 
I imagine there is access issue for the CoronaBuilder.exe. 
no clue how you can get over this.


[TOPIC: post.html]
#12

greg brady

[GLOBAL: userInfoPane.html]
greg brady
  • Contributor

  • 277 posts
  • Corona SDK

so technically, a corona app could be built in c++?

 

Greg



[TOPIC: post.html]
#13

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,838 posts
  • Corona Staff

Greg, not really. If you want to call api calls like display.newImageRect(), you're going to need to do that in Lua with at a minimum of a main.lua and config.lua. Anything that's going to be part of the OpenGL hierarchy will have to go through the Corona C to Lua bridge and be done in the Lua side of things. 

 

Corona Native lets you use Xcode or Android Studio to build apps without the simulator. You still have your Corona project which is going to be all of your Lua code. From there, if you want to do offline builds, you can with your 100% Corona/Lua code. Xcode/Android studio will take care of the packaging and code signing, linking in the Corona libraries etc.  While there are benefits, in particular, if you don't have a good network connect, to doing native builds, the Simulator using our build dialogs really makes things simple and generally faster to do builds.

 

CoronaBuilder is basically a command line tool that does the building for you. Basically, you can provide all of the build dialog parameters on the command line. Again, most people don't want to mess with life on the command line, but some people need to batch build multiple Corona apps and command line builds can save them time.

 

Now all that said, the other thing that Corona Native lets you do is twofold:  1. It lets you use some C++, Objective C, Swift or Java code with your Corona app. Let's say you need to access some feature that Corona doesn't support (like turning on the camera flash/LED). You can use Xcode/Android Studio, have your main Corona app and write some native code and create some functions that bridge them to Lua and you can then call those native functions from Lua.  2. Likewise, you can use similar technology to create a plugin (which has the native functionality and bridge code, but in a more portable method). If your plugin is useful enough, you can submit it to the Corona Marketplace for others to use. You can also purchase self-hosted plugins which lets you build plugins for yourself and use the Simulator for building.

 

Rob



[TOPIC: post.html]
#14

greg brady

[GLOBAL: userInfoPane.html]
greg brady
  • Contributor

  • 277 posts
  • Corona SDK

Hi Rob,

 

To understand this, it is a Lua to C bridge, that converts the lua to C code?  How about the conversion from C to java (Android) or C to objective C(Apple)?  That is a second step in creating the apk? 

 

I'm just curious, Greg



[TOPIC: post.html]
#15

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,838 posts
  • Corona Staff

I guess I wasn't clear enough. Your Lua code is never converted to C code. We embed a Lua interpreter in your package and your Lua code is processed by that interpreter. 

 

You can think of Lua as being two parts:  1. The Lua interpreter.  This is the part that lets you write for loops, if-then-else statements, assign values to variables, write your own functions, create tables of data etc. and 2. The C to Lua bridge. This provides your native side with a set of functions that lets you push values from your C, Java, ObjC code to the Lua side and fetch those values from Lua so they are available to the native functions. This bridge also lets your native side code generate events that can be caught and processed on the Lua side.

 

Lets take a simple example. Lua knows nothing about OpenGL and Texture memory, so a function like display.newRect() was written in C++, talks to a C based OpenGL library and creates a basic rectangular texture.  When you write:

 

local myRect = display.newRect( 100, 100, 25, 50 )

 

The values for x, y, width and height are pushed on to the Lua stack and it calls the C++ function for creating a new rectangle (something one of our engineers wrote in C++). That C++ function will pop the four values off of the Lua stack, call the OpenGL function to create the texture and put the rectangle on the screen and then it pushes the address to the display object back to Lua and it gets stored in your myRect variable. Obviously, there is more going on than that to create a display object (setting up remove methods, finalize methods, fill color methods, etc.)   

 

So nothing is being converted, just being run and calling functions. Objective C is just C with Apple's library of functions and some additional Object-Oriented syntax going on (It's a hybrid of C and Smalltalk.) On the Android side, Lua knows how to bridge with Java in addition to C/C++.  So display.newRect() is still done with C++ for Android, but calling native.newTextField() would be calling a Java function through the Lua bridge.

 

Hope this helps.

Rob



[TOPIC: post.html]
#16

greg brady

[GLOBAL: userInfoPane.html]
greg brady
  • Contributor

  • 277 posts
  • Corona SDK

Thanks Rob,  that's just incredible, it sounds like a very intricate but efficient design. I appreciate your explanation,  it fills in alot of unknowns about how Corona SDK works.

Greg




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: android native