How to run a C program in adb shell? - android

So adb shell gives one a linux terminal into an android phone, when it is connected to a computer. One can run all the standard linux commands like ls and cat inside the android phone, and explore the android filesystem using the adb shell.
Now I wonder, is it possible to use a C/C++ compiler such as clang or gcc to compile and execute a C/C++ program in the android phone, as on linux? I am not talking about android app development here, I mean a linux command line C/C++ program for android. Also, can I use the C standard library and linux system calls such as fork(), exec(), kill(), and working with file descriptors, in the android adb shell, just as I would do it on linux?

Yes. You can . For that you need copy your compiler exe's to android file system. From there you can easily call. Same time make sure that all pre-builed libs in android PATH folder.

Related

Running elf executable on Android

I was wondering if the only way to run an executable in android is by installing an apk. Would it be possible to run an elf executable? Just as done on linux.
Just drop and run it might be a problem due to restrictions that android uses. Note that those restrictions are updated every Android version.
If you target your executable to run on a rooted device, you can write an app that dumps the executable in a way that bypasses the restrictions and runs it.
If you target you executable to run on a custom ROM or Recovery, you can place the executable in a way that pass the restrictions and run it (without the need of a wrapper app).
if the executable is built for the target architecture, then
If the executable is statically linked: Yes
If the executable is built with Android toolchain/NDK: Yes
If you have the libraries against which the executable is linked: Yes
fi
If you have USB debugging enabled, just use adb push to copy the executable to device, not to a location mounted with noexec, and go to the shell with adb shell, and execute it. You might need to chmod it before executing.

Qt execute external executable program?

I am writing a Qt program to connect with Android devices.
I know that I should execute the adb first, and the connection is based on TCP, so I need to run
./adb forward tcp:xxport tcp:xxport
How to run this line in Qt ? I have copy the adb to my project file.
I develop this on Linux, and would run on Windows XP , so prefer cross-platform plan (only works on Windows XP is fine also).
QProcess p;
p.start(adbBinaryPath(), {"forward", "tcp:xxport", "tcp:xxport"});
To run adb you can use QProcess. Note that you need compiled adb binary for each windows and linux platforms.

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.

Android apps: making shell command-line tools that executes stuff in the app

I know how to start activities through a shell, run shell commands through Java, etc. But what about creating a command-line tool in Java? I want the user to be able to use the GUI as well as a CLI (i.e. adb shell or a Terminal emulator).
Is it possible to write the code in Java? Or do I need to delve into the NDK? Being able to write it in Java would be a lot easier due to all the available libraries etc.

installing and running a script file on android emulator

To make use of ASL library for my screen shot app on android,i need to install a script file (run.ps1) and run it on emulator ... So can any one tell me how can i do it...should i do it using ADB or using Eclipse...?
A .ps1 file appears to be a PowerShell script. PowerShell is a Windows application. Android is not Windows. Hence, if it is indeed a PowerShell script, you cannot run that script on Android.

Categories

Resources