Cocos2d-x using compiled lua script doesn't work on Android - android

I've been trying to make the HelloLua example to work in Android. It works if the hello.lua is not in compiled form using luac. But if I compile the hello.lua and upload it in my Android phone, it just gives me a black screen. Can anyone help me out in this?
This is the code in the AppDelegate::applicationDidFinishLaunching()
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCString* pstrFileContent = CCString::createWithContentsOfFile("hello.lua");
if (pstrFileContent)
{
pEngine->executeString(pstrFileContent->getCString());
}
#else
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("hello.lua");
pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
pEngine->executeScriptFile(path_c_str());
#endif
Seems that compiled lua works when I'm running in Windows but not in Android.

The man page for luac says that "Precompiled chunks are not portable across different architectures".

Related

Cross-compiling of C code for Android

I cross-compiled my C application for Android ARM with arm-linux-gnueabi tool under Ubuntu 16.04 LTS. I compiled it with static linking flag. This C application is big and it has complicated makefile. It compiled successfully without any errors. But it behaves differently on Android phone and Ubuntu PC. More precisely i have two problems:
popen() and system() functions don't work on Android. They are carried out but do nothing and don't give any errors. This problem i solved with dirty hack.
fgets() functions works strange on Android.
1. About first problem.
I did small research and found that Android doesn't use ordinary libc library (glibc or another library which implements POSIX standard properly). It uses Bionic library instead of it (sorry, Android is new OS for me). I looked into popen() and system() functions code and noticed that these functions use _PATH_BSHELL macros. _PATH_BSHELL is path to the actual system shell. This path is "/system/bin/sh" on Android and "/bin/sh" on Ubuntu.
When i understood it i tried to hook popen() and system() functions. I copied code of these functions from the Bionic source, than i defined macros #define _MY_PATH_BSHELL "/system/bin/sh" and replaced calls like execve(_MY_PATH_BSHELL, argp, environ); by execve(_MY_PATH_BSHELL, argp, environ); calls. So it started work properly.
2. About second problem.
On Ubuntu this code works properly:
is_received = false;
while(!is_received) {
FILE *cmd = popen(command, "r");
is_received = fgets(buf, sizeof(buf), cmd) == NULL ? false : true;
}
But on Android fgets() always returns NULL and this loop works infinitely long. I tried to use read() function instead of fgets() and it worked.
On Android this code with read() works properly:
is_received = false;
while(!is_received) {
FILE *cmd = hooked_popen(command, "r");
int fd = fileno(cmd);
is_received = read(fd, buf, sizeof(buf)) == 0 ? false : true;
}
My questions.
How to solve my problems with popen() and system() neatly and correctly? I think i have to link statically with Bionic library. Is it right? How can i do it in console without Android Studio? I read that it is necessary to use NDK but it is not clear to me how.
Why fgets() behavior isn't similar on Android and Ubuntu?

Android Studio NDK issue

I have a problem. I have an app which works fine on Android 5 & 6. The app has c++ shared lib refference. The problem is: When I compile and assembling to .apk without selecting my 4.0.3 device as target for execution then after installing its failing in my native code. But when Im trying to address this issue via debug everything works just fine. Is it possible that Android Studio is adding some additional parameters to build configuration without which app wont run.
Thank you.
I've found out that the problem is with my lz4 implementation which several years ago I've taken from windows app. There was a lot if stuff like:
#define U32 uint32_t
void* source = {some void pointer}
void* destination = {some void pointer}
*(U32*)destination = *(U32*)source;
I've found on internet implementation of lz4 for Androin and noticed that the differens is in using memcpy not (U32) = (U32). I have rewritten my code to exclude all places like this and it started to work on my 4.0.3.

Write Objective-C directly in Unity Project?

I'm trying to create an app, where at one point, uses native OS functions depending on the mobile platform. For instance, when a user is using iOS, and they tap the 'Email' button, then a UIAlertView will pop up. And so on and so forth for Android, Blackberry, Windows8/8Phone.
I want to be able to write all the code in MonoDevelop (don't need to access code from XCode or Eclipse projects). I've added the proper code for iOS so far, and laid the framework for the other platforms. Hopefully there is a way for the compiler to know that this code is not C#, and will run natively on the platform indicated... So does anyone know if there is a way to basically do this?:
#if UNITY_IPHONE
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email Us"
message:#"Would you like to send us an email?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
#endif
#if UNITY_ANDROID
#endif
#if UNITY_BLACKBERRY
#endif
#if UNITY_WINRT // windows 8, or windows 8 phone
#endif
Looks like you can develop some of it in MonoDevelop. So for the iOS, you can throw some source files in your Assets/Plugins/iOS folder and they will get included as plugins (See http://docs.unity3d.com/Manual/PluginsForIOS.html).
Unfortunately, for Android, your Java files need to be compiled in to jar files (See http://docs.unity3d.com/Manual/PluginsForAndroid.html). So you won't be able to do it all in MonoDevelop.
Though if you really wanted to get fancy, you could probably put your java files in your plugins folder and when you are done making edits in MonoDevelop, you could execute a custom script (Maybe an ant script) that compiles the files in that directory for you.

cocos2d android compile time linking not working

I am trying to get cocos2d-android (cocos2d-2.0-rc2-x-2.0.1) "Helloworld" sample to run under windows. I am using latest version of cygwin along side with android ndk r6, android sdk API 8. And I tried the manual here
after a lot of challenges I am down to this problem which I think is in linking the classes at compile time. when I try to run the *build_native.sh* script I get an error stating that in CCGL.h, PFNGLDELETEVERTEXARRAYSOESPROC which is defined as extern, does not name a type.
//declare here while define in CCEGLView_android.cpp
extern PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT;
extern PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT;
extern PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT;
'PFNGLDELETEVERTEXARRAYSOESPROC' is declared in CCEGLView.h.
#if CC_TEXTURE_ATLAS_USE_VAO
#include <EGL/egl.h>
PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT = 0;
PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT = 0;
PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = 0;
#endif
I tried to add the header address to android.mk under jni directory, to LOCAL_C_INCLUDES, as suggested here,but there were no rules to make them. so can anybody help me with how to compile/link this, it would be much appreciated.
I had this problem, and using a higher version of NDK worked. Try to get the most recent version, reset the environment variables, and run build_native.sh again.
http://developer.android.com/tools/sdk/ndk/index.html

Pure C++ program compiled for Android

I want to compile this program for Android and see it run on my phone:
#include "Hello World.h"
using namespace codewerks;
//=============================================
// Main Loop
//=============================================
int main(int argc, char* argv[])
{
Print(std::string("Hello World!"));
}
Where do I start? Can I compile this with GCC? The NDK seems focused on Java. Thank you.
This is now possible with the latest NDK. You will need an emulator or phone running Android 2.3 to try it, but the NativeActivity documentation has a complete example.
Unfortunately it is somewhat more complicated than a simple "hello world" example, and "main" is spelled "android_main". You still need to worry about your application life cycle as you do in Java, and the only real way to draw to the screen is to use OpenGL ES. It seems to be designed for writing games for Android.
Build as executable. (BUILD_EXECUTABLE)
Copy the executable to sdcard. (adb push)
Go to android shell. (adb shell)
Change the permission of the executable. (chmod 777)
Run the executable. (./out)
You will see the printed result on the console. (happy?)

Categories

Resources