Question

After searching the web about a problem I have I found that answer.

in brief, I need to spawn a new process from an Android application, and run a simple C program. I've created simple C program, like the next one:

int main()
{
 printf("This is the message before sleep() function\n");
 while(1){
    Sleep(1000); 
 }
 printf("This is the message after 1 second");
 return 0;
}

Iv'e complied the C program with Cygwin with the next command (gcc myProgram.c -o myProgram).

Iv'e put that file in the assest folder and I copied it at the begining of the program to the internal device memory to the folder "data/data/packageName/files/myProgram".

Now I want to execute the program, and when I will check, adb shell -> ps I want to see two process with the same name, but I can't find it.

I am trying to run the program like this:

Runtime.getRuntime().exec("chmod 755 data/data/packageName/files/myProgram");

This is not working, I can't find two process, and I don't know if this is the right way.

What I'm doing wrong?

Thanks.

Was it helpful?

Solution

You need to deploy the binary to the file system, to a location which is not marked as non-executable (that's why /sdcard/ and other common places don't help). You need to choose the version of your executable that matches the platform (armeabi, or x86, or mips). You should make sure that the file you deployed has the eXecutable permission.

The easiest way to ensure all this is to copy the arm build of your binary to ${project_root}/libs/armeabi/lib_myProgram_.so, and same for other relevant ABIs. Now the APK builder will pack the binary(s), and the installer will unpack them (with eXecutable permissions) to /data/data/your.package.name.with.dots/lib/lib_myProgram_.so.

All this done, you simply call from Java (the following line can be called from Activity or Service, which have access to context):

Runtime.getRuntime().exec(getContext().getApplicationInfo().nativeLibraryDir + "lib_myProgram_.so");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top