I have an android application that consists of a Java based APK, native executable, and native library. The apk talks to the native (root NDK c/c++) executable and library over a socket.
I'm not sure if it matters but the executable and library are compiled via cmake, and copied to be executable and then run as root. I need to get some type of debugging going with breakpoints and such, regardless of if it's directly in android studio or via command line.
You would need to run gdbserver on the device and let it attach to your executable
gdbserver comes prebuilt with ndk, usually under <ndk>/prebuilt/android-arm/gdbserver/
Copy gdbserver binary to your device, for instance to /data/local/tmp and give it executable permissions with chmod
If your executable is already running, find its PID with ps command and attach gdb to it:
gdbserver :5039 --attach <PID>
Note that 5039 is port number that is usually used for debugging with gdb, you can use your own if you like
set up a port forwarding from device to pc with
adb forward tcp:5039 tcp:5039
Run gdb locally, note that you need arm targeted gdb that comes with ndk too, usually at
<ndk>toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gdb
Attach gdb to your process
target remote :5039
And from here you need to use gdb commands that match your debugging expectations (set breakpoints, load symbols, step through etc), for examples use cheatsheet or ask in comments
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.
I am trying to install it on an Android operating system (I have a Samsung Galaxy tablet). I would like to install both R and RStudio.
I've been online to try and find out about this but most of the articles are quite old (going back to 2013) and don't make much sense to me.
I have both R and R studio installed on my personal laptop (with a windows OS) and found these both very straightforward to set up, but having some trouble figuring out how to do this on a tablet.
If anyone has done it recently and been successful please let me know.
Install GNURoot from Google Play
Install Gnuroot Wheezy from Google Play. See http://www.linux-magazine.com/
Online/Blogs/Productivity-Sauce/GNURoot-Linux-on-Android-No-Root-Required
for more help.
Update the package repositories (twice!):
apt-get update
apt-get update
Block updates to system Perl:
apt-mark hold perl-base
Install R:
apt-get install r-base
Install all available cran packages from the Debian repositories:
apt-get install r-cran*
Start R from the command line:
R
source
http://www.r-ohjelmointi.org/?p=1434
Rstudio-server didn't work in a non-rooted device, installed in ubuntu in termux. Related links: rstudio in android - rooted, non-root rstudio discussion.
I found two workarounds in a non-rooted device. Working on browser or VNC Viewer.
1. The browser (jupyter) approach
(tested in yoga book with Android 7.1 and Huawei phone with Android 8 - size with ggplot 1.66 Gb)
install termux (google play) (As termux didn't run jupyter-IRkernel, I had to install ubuntu)
install ubuntu in termux (MFDGaming or Neo-Oli).
run this in ubuntu:
apt update && apt upgrade
apt-get install apt-transport-https software-properties-common
# add R repository
add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu disco-cran35/'
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
apt update && apt upgrade
apt install nodejs
apt install npm # absence of this causes non-editable cells in jupyter
apt install gfortran
apt install liblapack-dev
apt install libopenblas-dev
# install R
apt install r-base-dev # or r-base or r-base-core
# install python package manager
apt-get install python3-pip
# install jupyter
pip3 install jupyterlab
# open R
R
Inside R:
# Package necessary for jupyter
install.packages("IRkernel")
IRkernel::installspec()
q()
In ubuntu, run the jupyter lab
jupyter lab --allow-root
# as you will see you have to paste something like this in your internet browser
http://127.0.0.1:8889/?token=1a0f9b3d472d155bb4d46df119b937646d6192f569c9d635
You would need to disable the automatic hibernation of termux in battery settings.
Note: In termux app (google play) is possible to install R, see Conor link; the important setupclang-gfort-8 step can be accomplished after installing make and gcc-8 and using the new package names in the output of the (outdated) Conor protocol. The problem appears if you want GUI. An option is the Rcmdr package, but as termux has not tcl-tk, as you can see inside R using capabilities(), I ended installing ubuntu in termux
2. The VNC approach
As in the browser approach install termux and ubuntu
In ubuntu install aterm
After installing R in ubuntu (see above), install R Commander GUI package:
#in R:
install.packages("Rcmdr")
In ubuntu install vnc4server
In Android install VNC Viewer (google play).
Launch the VNC server in ubuntu:
tigervncserver -xstartup aterm
In VNC Viewer connect to 127.0.0.1:5901 which corresponds to the :1 display (showing the aterm in white), open R in aterm, then library(Rcmdr).
After ploting, see ggplot example, I had to write (blindly) dev.off() because there is no other way to return to the script window.
require(ggplot2)
ggplot(diamonds) + geom_point(aes(x=carat, y=price, color=cut)) + geom_smooth(aes(x=carat, y=price, color=cut))
Notes:I also installed X11 in ubuntu, but didn't seem to improve the GUI experience of Rcmdr. In a yoga book with android the #Ajay answer didn't work.
Not exactly R Studio but still pretty cool... follow this Installing R on Android via Termux guide from Conor Anderson's blog.
Step 1: Install Termux from F-Droid (or Google Play Store)
Install from F-Droid
Step 2: Add the its-pointless package repository
Enter the commands below one at a time:
pkg install curl gnupg
mkdir -p "$PREFIX/etc/apt/sources.list.d/"
echo "deb https://its-pointless.github.io/files/24 termux extras" > "$PREFIX/etc/apt/sources.list.d/pointless.list"
curl "https://its-pointless.github.io/pointless.gpg" | apt-key add
Step 3: Install required .deb packages
Install the following packages. It might be wise to install each individually to monitor the warnings, I think some of these packages may be deprecated at this point in time.
pkg install r-base \
make \
clang \
gcc-9 \
libgfortran3 \
openssl \
libcurl \
libicu \
libxml2
Step 4: Configure your compiler
setupclang-gfort-9
Step 5: Launch R by typing an upper case R
R
Step 6: Further customize with Nvim-R or whatever else you like
Here is a summary of my experiments, as of October 2021. Long story short: I have a fully working R (complete with tidyverse) on my tablet, and I can access it from jupyter (which is not my first choice in general, but is probably appropriate for this platform and my use - I'm not planning to develop on Android, merely to try code snippets etc).
This draws mostly from the present article as well as from How do I install Jupyter notebook on an Android device? , and links therein.
Also, caveat reader: I'm not an expert in Linux, even less so on Android, and my understanding of Python is minimal. Sorry for any mistake or imprecise statement.
Including trial and errors, the whole process took me some 5 hours. Now I know what to do, I'd probably manage in 1 or 2.
Introduction
Android is a linux in disguise, so in principle it is possible to install all or most linux apps. There are, however, several caveats:
You normally do not have root access to your device (unless it is rooted, which is another can of worms);
When compiling binaries, they must be compiled for this specific architecture.
For these reasons, you can not always use ordinary .deb or .rpm packages and need to go through a somewhat convoluted route.
Also, it appears that R-studio cannot be installed without root privileges (either because it requires root itself, or because it relies on libraries that cannot be installed by a normal user).
Assuming you do not want to go the root route, you can still manage quite a few things.
You will need some basic familiarity with linux commands (cd, ls, chmod, ln...), and at least a basic idea of how packages are installed and managed in linux (apt-get or pkg), in R (install.packages() ) and in python (pip install). Also, it will not harm if you know what a compiler is (gcc), and if you realize that some R/python packages rely on compiled code (binaries) that typically resides somewhere else on the system.
Termux
Termux is "an Android terminal emulator and Linux environment app", a portable Linux distro running on Android (more or less).
Unfortunately the version currently on G-play is not the most recent one, so you want to use the f-droid version. So, install f-droid and then, from f-droid, install termux.
Configure termux
You will need basic tools (compiler etc) up and running on termux. This will require installing packages and in turn, you need packages compiled for this platform. In termux, many packages are maintained by "its-pointless" who also runs the relevant repository. So, in termux :
# basic utilities
pkg install curl gnupg
# Configure its-pointless repo
mkdir -p "$PREFIX/etc/apt/sources.list.d/"
echo "deb https://its-pointless.github.io/files/24 termux extras" > "$PREFIX/etc/apt/sources.list.d/pointless.list"
curl "https://its-pointless.github.io/pointless.gpg" | apt-key add
Now you can install more utilities, from its-pointless repo:
pkg install make \
clang \
gcc-9 \
libgfortran3 \
openssl \
libcurl \
libicu \
libxml2
And configure your compiler
setupclang-gfort-9
R !
Still in termux, run
pkg install r-base
At that stage, you should have a working R on your system (although purely text-based, but you can still use an external editor and source() your scripts).
Something nicer ?
You maybe want a GUI, or more pakages (tidyverse). Shortly, the main problem you will face is that many R packages in linux rely on system libraries, that need to be installed from linux. In principle you should be able to install the full libraries (the binaries as well as the R code) from install.packages(), that launches gcc to compile whatever code is needed; but practically, the compilation seems to fail more often than not. Probably the Android gcc has its quirks...
So the general idea will be to install from termux (or its-pointless) repo the R packages you need, or at least the library they rely on. The same is true for python libs, by the way.
Installing R packages
From within R, try to install what you need with the usual install.packages() command. If something needs to be compiled, it will probably fail. Read carefully the error - eventually you will find a line that says something like "missing libpango.so". This tells you, in this case, that you need to install pango, which happens to belong to the pango package : in termux, run pkg install pango, then return to R and try again.
Also, updating your packages to the latest version (pkg upgrade all) will do no harm...
On stack overflow, you will find various references to zlib, libpng, etc... read and interpret the error message.
In one case (R package repr, that needed base64, itself relying on libicu) I had a version issue - one of the termux packages was (incorrectly) requiring an old version of libicuuc (libicuuc.so.68), whereas the same termux had installed libicuuc.so.69 on my system. In this case, the cure was to create a symbolic link in the approriate directory, as follows
ln -s libicuuc.so.69 libicuuc.so.68
.. and the system happily followed the link :-)
By playing around with this, I managed to install tidyverse, as well as IRkernel that is required to establish a connection between R and jupyter. IRkernel needs to be configured from within R: IRkernel::installspec()
VNC
One user at Installing R on Android refers to the possibility of installing VNC, to obtain Rcmdr. I haven't tried.
Jupyter
It is also possible to install jupyter or jupyter-labs (which is not my favourite IDE under normal circumstances, but is probably an appropriate platform on a Android device).
Jupyter is python based, so you will need to play the same game with Python and python packages. Python packages are installed using pip (from the linux command line), and you will have the same issue with compiled binaries. Fortunately the most tricky python packages are packaged by its-pointless for termux, so here too, you will end up installing quite a few things outside of pip.
Installing R on Android suggests installing ubuntu on top of termux, and then installing jupyter from within ubuntu. I found it unnecessary (How do I install Jupyter notebook on an Android device?)
Termux will need more packages:
pkg install python fftw libzmq freetype libpng pkg-config libcrypt
There are also references to modejs, liblapack-dev and libopenblas-dev, I cannot remember whether they were required or not in the end (or already installed by something else). Also sometimes the relevant code moved out of the dev package to a stable one (so it would be liblapack)
Then, you need to install python stuff. Web sources suggest
LDFLAGS="-lm -lcompiler_rt" pip install jupyter
or/and, depending on the version you want to use
LDFLAGS="-lm -lcompiler_rt" pip install jupyter-lab
and (not required but useful to have in jupyter) :
LDFLAGS="-lm -lcompiler_rt" pip install numpy matplotlib
I'm not sure why we need LDFLAGS...
As previously, some of the "hybrid" packages are best installed from termux, for instance scipy (technically a python package, should be installed with pip install scipy) is easier to install (or must be installed?) using the termux version, so pkg install scipy
Again, some debugging will be required. Read the pip output, try to find out which package failed to install, see what you can do from termux, try again. Sometimes I found that dropping the LMFLAGS helped (or installing packages one by one), for instance a plain pip install cffi and pip install pyzmq worked better than having them installed as dependencies to jupyter. No idea why.
In the end, I launched jupyter (type jupyter notebook, or jupyter lab in termux console, copy the url, open in a browser)... only to have R crash on the first command. Back in termux I could see that it was still missing a library somewhere, which I tracked down and installed from termux (I think it was pango, this time).
So in short: Read the Friendly Error Messages !
I try to attach a process on my rooted Android and create corefile with GDB but it can't create a gcore file because the symbols could not be found.
on my phone, i open Terminal app and i input
su
to grant root access in Terminal. i input
dumpsys meminfo
to show all running processes. i input
gdbserver :1234 --attach 5132
on my computer, i open gdb.exe (from Android NDK) and i input
(gdb) target remote 192.168.1.13:1234
and i got those infomation
(gdb) target remote 192.168.1.13:1234
Remote debugging using 192.168.1.13:1234
warning: Architecture rejected target-supplied description
Reading /system/bin/app_process32_original from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
warning: A handler for the OS ABI "Cygwin" is not built into this configuration
of GDB. Attempting to continue with the default arm settings.
Reading /system/bin/app_process32_original from remote target...
warning: A handler for the OS ABI "Cygwin" is not built into this configuration
of GDB. Attempting to continue with the default arm settings.
Reading symbols from target:/system/bin/app_process32_original...(no debugging symbols found)...done.
0xb6e8b0f8 in ?? ()
With my own compiled gdb.exe, i got another info
(gdb) target remote 192.168.1.13:1234
Remote debugging using 192.168.1.13:1234
warning: Can not parse XML target description; XML support was disabled at compile time
Reading /system/bin/app_process32_original from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /system/bin/app_process32_original from remote target...
Reading symbols from target:/system/bin/app_process32_original...(no debugging symbols found)...done.
Remote 'g' packet reply is too long: fcffffff605fd9be10000000ffffffff0000000008000000000000005a010000a87687b414000000000000004010c032105fd9be005fd9be1352e6b6f8b0e8b610000f204a280000000000003d0000001100000000000000000000006000000004000000d002000068010000680100003702000052000010110000001600000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004a28000072002e0073006500720076006900630065002e0056006f006900700043006f006e006e006500630074006f0072005300e83bb770e83bb77088639b70e83bb770e83bb770e83bb770e83bb770e83bb770e83bb770e83bb770e83bb770e83bb770e83bb770e83bb770e83bb770e83bb77011000060
0xb6e8b0f8 in ?? ()
i type
gcore
and it says
Can't create a corefile
I have installed the correct gdbserver binary that match with my kernel architecture in /system/bin
I tried different Android OSes below Android 4.4.4 which does not have PIE protection thing, but i still getting the same problem. I tried to use ported version gdb client and it works perfectly.
I just wanna save the corefile with my powerful device running Android 5.1.1 with 2 GB RAM, instead using gdb client on my low-end tablet that has 512 MB RAM and running Android 4.4.4, and the gdb could not fully create the corefile due to low RAM.
Just download gdb source and compile with below steps:
1).Compile gdb with below command:
cd gdb-7.11/gdb
./configure --target=arm-linux-androideabi --with-python --prefix=$HOME/mybinaries/bin
make
make install
2).Compile gdbserver with below command:
cd gdb-7.11/gdb/gdbserver
CC=arm-linux-androideabi-gcc LDFLAGS="-fPIC -pie" ./configure --target=arm-linux-androideabi --host=arm-linux-androideabi --prefix=$HOME/mybinaries/bin
make
make install
When compiling the GDB, i must give the target --target arm-linux-androideabi instead --target arm-eabi and it works perfectly
I'm using eclipse to debug my android app which has a c native code.
When i plug in my nexsus4 the ndk external debugging tool works.
when I plug my galaxy note2 it throws the following error when trying to attach to the device:
Android NDK installation path: /Users/eladb/MyWorkspace/android-ndk-r8b
Using specific adb command: /Users/eladb/MyWorkspace/sdk/platform-tools/adb
ADB version found: Android Debug Bridge version 1.0.31
Using ADB flags:
Using auto-detected project path: .
Found package name: com.myPack
ABIs targetted by application: armeabi
Device API Level: 18
Device CPU ABIs: armeabi-v7a armeabi
Compatible device ABI: armeabi
Using gdb setup init: ./libs/armeabi/gdb.setup
Using toolchain prefix: /Users/eladb/MyWorkspace/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/arm-linux-androideabi-
Using app out directory: ./obj/local/armeabi
Found debuggable flag: true
Found device gdbserver: /data/data/com.myPack/lib/gdbserver
ERROR: Could not extract package's data directory. Are you sure that
your installed application is debuggable?
I have enabled developers options
and allowed device debugging via usb
it doesn't work for my samsung galaxy note2 (android 4.3) but works for nexsus4 (4.2)
I think this is a known issue?
but how to workarround this?
It's a problem with Android 4.3 in general. Take a look at the Android bug tracker issue 59558 which in turn links to the root of the issue that run-as is borked. (58373) I'm not sure when/if the Note 2 is getting 4.4, so you may want to look into whether or not you can downgrade back to 4.1.2.
This happens due to two known bugs related to the run-as tool. The tool needs to read information about your app from the /data/system/packages.list file inside your Android device. In the case of the first bug, the tool is actually only reading the first 8KB of the file. Thus, if the line containing your app description is below the first 8 KB, run-as will fail. The second bug affects some 4.2.2 and 4.3 Android images by falsely restricting read rights of the packages.list file that contains all the package names.
Solution here: http://visualgdb.com/KB/?ProblemID=nopkg
In my case, I had rooted my Android 2.3 device and changed the default shell to be bash, which was causing issues when ndk-gdb tried to run a shell command.
sh-3.2$ ls -l /system/bin/sh*
lrwxrwxrwx root root 2014-02-01 10:26 sh -> bash
-rwsr-xr-x root shell 82840 2011-09-30 18:55 sh0
sh-3.2$
This was causing the android r9 ndk-gdb script to abort with that 'Could not extract package data directory..' error.
Make a backup copy of your ndk-gdb script first.
Then, near line 615 of ndk-gdb, make the following edits:
# Find the <dataDir> of the package on the device
# on my rooted Android 2.3 device, the default sh has actually been renamed to sh0
# so try that first.
adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh0 -c pwd # <-- insert this line
if [ $? != 0 ] # <-- insert this line
then # <-- insert this line
adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwd
if [ $? != 0 -o -z "$DATA_DIR" ] ; then
echo "ERROR: Could not extract package's data directory. Are you sure that"
echo " your installed application is debuggable?"
exit 1
fi
fi # <-- insert this line
Basically, it will try to use /system/bin/sh0 first. If the file doesn't exist, then it will then try /system/bin/sh. I've only tested the /system/bin/sh0 case so far, so YMMV..