Android, Java, running an Executable resource - android

Environment
Windows (x64) Host
Android 5.0 USB connected to the Windows machine
Un-rooted Samsung Galaxy 5
Use-case
Java APK Application
Native C++ executable packed as a raw resource part of the APK
Upon startup executable is to be ~extracted~ to a temp folder where it would execute
Questions/Discussion
Is the above use-case supported by the Android OS?
To the best of my knowledge, '/data/local/tmp' is not accessible to a running app ( but to shell & root ), Is there a directory where the file can be extracted too and executed? ( dir must have execution rights )
What would be the best approach to achieve the above mentioned?

Yes, you can do this. You will need to make sure that the native executable is for the appropriate target architecture (some type of ARM processor, usually).
But you shouldn't extract it to /data/local/tmp. You will need to extract it to /data/data/application.package.name/lib, which is a directory to which your app has read/write permissions.
There are more details in this question and its accepted answer. It looks as though you can have the executable extracted automatically for you if you name it as if it were a library file rather than a standalone executable.

Related

Porting C based linux application to the Android platform

I have C Linux based application and now I want to port it to Android.
I figured out, that I can extract the toolchain from Android NDK and build my application, but how to make the APK such that I can install it on the android devices without the need of root access.
In Linux, I used to install it using a bash script which used to put my application related files in different folders like /opt, /etc (files shared with other applications) and /var . How can we handle this in Android. Is there a folder similar to /etc in Android where I can put files that other applications can read.
Thanks
-M
First of all, you are lucky if your project compiles "as is" with NDK standalone toolchain. Often, bionic is not enough, and people need to tweak the build environment (from libpthread to full-blown buildroot alternate toolchain with static C runtime).
As for the shared files location, on Android it's named "external storage". Your app and other app may require special permissions to write and read to this location. Directory /opt does not exist here. You don't have write access to /etc, but files like /etc/hosts are available for read.
Regarding the APK. You are right, this is the ultimate way to distribute and install apps on Android. But you can, even without root, to locally install and run a command-line executable. Usually it's done with Developers Options turned on, and enabled USB debugging. Now you can open an adb shell, install and run your program. The trick is that external storage (see above) is marked as 'non-executable'. Therefore, you must find another place for your binary. Usually, /data/local/tmp will be a good choice.
Instead of adb, you can use a terminal emulator on the device.
If you choose to build an APK, you will probably prefer to convert your app to shared library that will perform actions for Java via JNI. But it is also possible to package your command-line binary as part of the APK and use Java Runtime.exec().

The Different between android folders(bin, xbin, sbin)

I've noticed that I have some directories inside my android phone which look the same.
What the different between /sbin, /system/sbin, /system/bin, /system/xbin?
/system is the main Android directory for storing immutable components generated by the build of the AOSP. This includes native binaries, native libraries, framework packages, and stock apps. It’s usually mounted read-only from a separate image from the root file system, which is itself mounted from a RAM disk image.
Android also includes many directories commonly found in any Linux system, such as /dev, /proc, /sys, /sbin, /root, /mnt, and /etc. These directories often serve similar if not identical purposes to the ones they serve on any Linux system, although they are very often trimmed down, as is the case of /sbin and /etc, and in some cases are empty, such as /root.
More than 150 command-line utilities are scattered throughout Android’s root file system. /system/bin contains the majority of them, but some “extras” are in /system/xbin, and a handful are in /sbin.
/sbin: In Linux, this would hold binaries essential to the system administrator. In Android, it contains only ueventd and adbd.
/bin: All native binaries and daemons built as part of the AOSP. All modules built with BUILD_EXECUTABLE are here. The only exception is adbd, which has the LOCAL_MODULE_PATH set to /sbin and is therefore installed there instead.
/xbin: “Extra” binaries generated by some of the packages that are built within the AOSP but aren’t essential to the system’s operation.
Thanks goes to Mr: Karim Yaghmour , The great ; The author of Embedded Android tutorial.

How do I run Android's .apk files on linux without the Android emulator?

I have a requirement, where I need to install the .apk files directly on my host PC, without the Android emulator. I want to install it directly on my Linux machine (x86 arch). I have a few noob questions with regard to the same:
How is an .apk file organized?
Is it just one big binary file?
In this case, if I am using a phone with an x86 atom processor, does this mean that there will be a separate Android market segment which hosts apps ported to the x86 arch?
Is it an archive consisting of executables? (something like .rpm or .deb).
If so, then what is the format of the archive?
If so, do they have different .apk files for different architectures (x86, ARM, etc.).
If not, then how is it orgranized?
What are the requirements to run the .apk file directly on my Linux PC running on x86 arch?
I do not have a very good background of Android and the .apk files, hence these questions.
Thanks and regards,
Hrishikesh Murali
You cannot install .apk files directly onto your PC. Even though Android uses the Linux kernel, the applications runs on a totally different platform. APK files are ZIP file formatted packages based on the JAR file format, with .apk file extensions. They hold all of that program's code (such as .dex files), resources, assets, certificates, and manifest file.
They run only on the Dalvik virtual machine.
Install Android OS on your PC, if you really want to run them from a computer.
You can use the ARChon to run apk in ubuntu:
Install the ARChon Runtime.
http::github.com/vladikoff/chromeos-apk/blob/master/archon.md
Open and enable the flag: chrome://flags/#enable-nacl,then restart Google-Chrome. (Important!)
Install existing android apps in the website or convert your favourite android apps by "Online APK-CRX Converter.
Drag and drop the Google-Chrome extensions into the chrome://extensions/ page in the browser and it will be installed in your browser. Enjor it!
An apk file is not an executable, it's just an archive file contaning all the resources and class files that are to be used by the application.
The apk is set to be unpacked and run by the Dalvik vm.
The elements that are contained inside the apk file are specifically designed and designated to be used in Android mobile phones and not a desktop PC running Linux. Though Android uses the Linux kernel, you definitely cannot run the apk file on a Linux machine, as you would an rpm or a deb file.

accessing android os root files

i have the following doubt:
i have read that android os is based on linux, and i imagine it may have the same structure that ubuntu (in terms of file configurations: /root, /dev, etc).
so, is it possible to run an application written in C in android? just as it is possible to do in ubuntu? if so, how can i do that?
also, how can i get access to the root files through an android application (written in java)? and how to get access to the behavior of the os (in terms of interruptions for example)?
thanks in advance for your answers.
gus
Basic answer: Running a C app on Android isn't supported, except through the Native Development Kit (NDK).
You can't get access to the root files except by rooting a phone.
You can get access to some OS behavior through the API. Basically, if it's not part of the API, you can't access it.
The Android OS is based on Linux, but it's an OS, not a windowing server like X or a desktop environment like Gnome or KDE.
You may run C and C++ code on android using NDK. You may use also QT framework. But code is runing in virtual machine named Davlik. Android have root acount , but it is default not available for user. Therefore, access to directory is dependend for chmod.
If you would like read about access to low level in android:
http://www.phrack.org/issues.html?issue=68&id=6
And about architecture this system:
https://developer.android.com/guide/basics/what-is-android.html
You can run programs using Runtime.exec. As an example, you can see Terminal IDE which runs many different programs including ssh, vim and bash. It's also open source so you can learn from the code. You will probably have to include the executable as a resource or asset and then copy to a local directory, grant execute permissions, then run with Runtime.exec. You still have limited user permissions as your app runs under a restricted account unless the device is rooted and you request root access.
an android smartphone/tablet works with an Arm cpu, not a x85. the architecture is different.
but you CAN run a C application in android if you cross compile it for arm linux. or you can use a c compiler inside android device. people ported c compiler to android. you can try C4DROID and in android market. but you can only run compiled program in system memory because of android permissions about sd card.

Package Android apk with additional executables

As a follow up to an earlier question (Android ioctl - root permissions and usage), is it possible to create a separate native executable and package it in an APK?
The app needs root access and isn't going into the Android marketplace, but I'd like to be able to install it without using a script that pushes an extra executable onto the device.
There is an easy way to package an executable into an APK, and let the system installer take care of unpacking this executable, see How to package native commandline application in apk?.
The trick (tested up to Jelly Bean 4.3) is to name the file "libmyexecutable.so" and put it into libs/armeabi of your Android project (I assume an ADT or ant build). The Package Manager will unpack the file to /data/data/your.package.full.name/lib (it's a symbolic link, for backwards compatibility reasons, so maybe in some future version of Android this will not work anymore) on the device, with executable permissions.
Note that the file has all read-and-execute permissions, so you can use Runtime.getRuntime().exec() or system() from other apps, too.
Update:
These days, you should use libs/armeabi-v7a for 32 -bit ARM executables, and you probably need to prepare 64-bit ARM version of the same executables, too.
You can put it into assets and copy it to the app's private directory on first run. After you set the executable bit, you should be able to run it.

Categories

Resources