Wrapping a C command line executable in a standard Android application - android

I have a C command line executable that I have successfully compiled for Android. I can copy the executable and launch it with Android Terminal Emulator on my Android ICS phone by doing the following:
execute "export TERMINFO=/etc/terminfo"
execute "mount -o remount rw /sdcard"
launch the executable from the sdcard
Step one is necessary because the command line application makes use of the ncurses library and if i do not set TERMINFO then I get an error when I try launch the application. If I leave off the second step then I get an "Access denied" when I try launch the command line application from the sdcard. So, provided I manually do these steps I can launch the command line executable.
Now what I am wanting to do is to wrap this command line executable inside a standard Android application. The source for Android Terminal Emulator is open source and so I can use that to open an EmulatorView inside my Android application. My question though is how do I go about including the native executable inside the apk, deploying it to the device in a location where my application will have rights to execute it in the EmulatorView. I am a bit concerned about whether I will be able to get over the rights issue so that the native command line executable can be launched.
I do know about the Android NDK, but would prefer not to have to re-write a working command line application so that it can be included as a library. I am specifically looking at keeping the C executable source as it is and executing it from the wrapper application. Does anyone know whether this is possible and if so how I would go about doing it?

If you copied the native file under your data folder where you should have appropriate permission, and then use Runtime.exec to execute your file.
You can create a bin folder (or any name) and copy your file under
Process exeProcess =
Runtime.getRuntime().exec("/data/data/YOUR_PACKAGE/bin/EXECUTABLE_FILE");
You can use the object exeProcess to read the data from your executable file.

Related

How to access android root directory

I am developing an application using ionic framework.
The app creates files (*.json) and stores them in /data/user/0/ when i verify whether they exist or not, the result was true which means the files exist in the mentioned directory and I can access and modify their content without problem, but when I check the directory with a file manager or from the computer, no result, the directory is empty.
Could someone tell me what should I do?
use adb to copy the file. Even if it's in root dir, u should have access to it via adb.
Do adb pull data/user/0/filename.json path_on_ur_comp.json.
this will copy the file to the directory you define in the 2nd parameter.
// EDIT:
adb is part of the Android SDK, stands for Android Debug Bridge.
You can use this for MANY MANY different reason but of course, the "main" reason is to debug Android devices. You can use it to transfer files in your case.
In Windows, it's located here:
C:\Users\USERNAME\AppData\Local\Android\sdk\platform-tools\adb
In Mac, it's lcoated here:
/Users/USERNAME/Library/Android/sdk/platform-tools/adb
Depending on which OS you use, open that either with Terminal (Mac) or Command Prompt (Windows).
Once you do that, run the following command:
For Mac:
adb pull data/user/0/filename.json /Users/USERNAME/Desktop/somefile.json
For Windows:
adb pull data/user/0/filename.json c:\Users\USERNAME\Desktop\somefile.json
This will copy the file and put it on your desktop

How to use the terminal in Android Studio?

I'm trying to integrate Google Sign-in into my app, and I'm following this tutorial. However, I'm trying to add the configuration file to my project, and the tutorial says that I should use this command:
$ move path-to-download/google-services.json app/
When I open the terminal in Android Studio and write the command it says:
'$' is not recognized as an internal or external command
I've also noticed that it's similar to Command prompt (I'm using Windows). Can I just copy the .json file and paste it in my app folder?
I believe $ (dollar sign) is indicating a start of new line in Unix based OS terminal. In Windows OS, the command should be
MOVE [Source] [Target]
For more information, check this link.
Or you can just copy and paste it in your app folder. That will be suffice as well as.
'$' is the user prompt, You should omit the '$' and the command should work

I need root permissions to execute a native application under Android?

I have compiled a native application, a terminal only application basically, with the android NDK, my main problem right is that I can't change the permissions on my executable ( a dynamically linked one ) like so chmod +x executable to test and use the application.
I need to root my device just to do that ?
I tried with both adb shell and a random terminal application directly from my phone.
No, you don't need to root a device to use executable binaries. You cannot put it on /sdcard but on most devices there is a directory /data/tmp or /data/local/tmp where you can push files with adb and execute with adb shell.
The robust option is to package an executable in an APK and get it on device by installing the APK, see Is it possible to run a native arm binary on a non-rooted android phone? or How to package native commandline application in apk?.
Note that you cannot change the LD_LIBRARY_PATH, so be careful if your executable depends on some shared libs that are not part of /system/lib.
These guys say root is needed:
How to compile C into an executable binary file and run it in Android from Android Shell?
From my understanding, the regular sdcard is mounted with no execution permission, so you need to write to something like /data/local/, which indeed requires root access.
If you don't package your native code as an Android app, you'll need to run it from shell.
Starting with Android KitKat/Lollipop, executables can only be run from restricted locations. eg an executable installed in /data/data//... will not be allowed to run in any ways, be it with or without root.
Before KitKat, one can copy the executable to its own data directory, make it executable and run it. Not anymore in more recent version of KitKat.
So you will definitively need root to run linux exe on recent versions of Android.

Creating war file in Windows 7

I am trying to set up a GCM Demo Application. http://developer.android.com/guide/google/gcm/demo.html
I am stuck trying to create a war file:
In a shell window, go to the gcm-demo-server directory.
Generate the server's WAR file by running ant war:
I am using Windows 7 how exactly can I create this war file?
Create a batch program called build.bat, containing the following content:
call ant war
pause
This will launch a shell window within which you'll see the ANT output.
Notes:
This assumes that ANT has been installed correctly, with the "ant" executable available on the user's PATH

Want to run script file using system call in android NDK

I want to run script file on android Shell using Native C program.
I tried using system function but it's not working.
result = system("sh ./test.sh");
LOGD("result is %d", result);
system command returns 0 means its not executed script file successfully.
test.sh contains
echo "test...."
Android NDK application could not print test.... when this system call runs.
Even any script can not be started using system call. i checked more than 10 different scripts.
test.sh have 777 permissions
Any help would be appreciated.
where is 'sh'? and what is your '.' current directory when the application runs? try:
result = system("/system/bin/sh /full/path/to/test.sh");
Here i got the answer of this Question on Different post.
Run Shell Script file On Android Embedded Device using System function in Android NDK
thanks to all for Help

Categories

Resources