Can you run the same application and code on Android and Linux? - android

I'm looking to build a new app written in Java, can I write one code that can be executed both on an OS running Linux and OS running android? Or do I need to write two different codes one for each system?

Theoretically yes.
Android apps run in a Dalvik Virtual Machine, that comes with a Java language implementation that compiles down to Dalvik bytecode, but not to JVM bytecode. So, the code has to be written in Java or some language that compiles to Dalvik VM bytecode that uses the Android API.
However, the virtual machine runs on top of the underlying Linux OS, and there are ways to call native code. See NDK documentation.
So, it is technically possible to run native Linux programs on Android, as there is a Linux kernel running beneath everything.

Related

Where is java command located in Android?

I got my android phone rooted and I'm aware that the / doesn't contain the familiar linux /.
Is there a java command? Where is it located? I don't find one in /system/bin.
Is there a java command?
No. Such a command isn't shipped with stock Android images.
Where is it located?
It shouldn't exist on filesystem. What makes you think it should be there?
I don't find one in /system/bin.
You might consider installing any Java programming app for Android to have JDK/JRE installed. In this case the command will be located in the app's sandbox, not /system/bin.
Android does not use Java at all, at runtime.
Android runs programs that have been written in the Java language. Those programs are not compiled to Java bytecodes and will not run on a JVM.
Android executes Dalvik bytecodes. Until somewhere around Android 4, it used a virtual machine called Dalvik, to execute these bytecodes. Now it uses fantastically more complex virtual machine called ART.
Sorry. No Java.

Qt on Android - does the C++ code run in the Dalvik virtual machine

I have heard it said that the C++ code runs "natively" on all platforms. By this I guess that what is being suggested is that the cross-platform ability of Qt is not using something like HTML 5.
But does this mean that on Android Qt code runs in the Dalvik virtual machine?
Android understands either Dalvik or the newer ART. Dalvik and ART both have the ability to link to C/C++ code through the Android NDK which is Android's take on JNI. QT for Android while being C++ based still requires a minimal amount of Java based initialization. What this means is that even if the C++ code runs natively (architecture dependent) to the underlying OS, it is still required to be presented through code that runs on the VM (Android specific) as the VM does not directly understand C++ but can link to it through it's own Java based framework which is the Android NDK.
Please check https://www.qt.io/blog/2013/07/23/anatomy-of-a-qt-5-for-android-application
At the very top of levels, a Qt for Android application consists of two parts:
The Qt application:
This is the cross-platform code and resources that you, as the app developer, manage yourself, and which are summarized by your qmake .pro file.
An Android application launcher:
This is generated for you by Qt Creator the first time you connect your project to a Qt for Android Kit.
So, Android application launcher should run in Dalvik VM. I can't sure if this works well with ART runtime of Android 5.0

Android app is native or JavaVM code?

Probably an stup!## question.
I dont know nothing about developing apps for Android, but I was wondering if the applications for Android, when they are compiled, the code generated is PURE ANDROID NATIVE CODE, or is Java Virtual Machine compatible code.
Thanks a lot.
It has its own virtual machine, Dalvik.
Most apps for Android are Java apps. They are compiled to .class files by the Java compiler, then the dx compiler takes the .class and compiles them to .dex files, which are executable by the Dalvik VM on Android (which is the Java VM).
Although the Dalvik VM is made to run Java code, it is significantly different than the standard Java VM on your computer. The standard VM is a "stack based" machine, whereas the Dalvik VM is "register based".
You can also make apps with the NDK, and write them in C or C++. This is not Java compatible in any way (although you can interface the native C code with the Java code with JNI).
There are at least three ways to do it:
All Java
All Native
Half Java, half native. Android libraries can be written in C and compiled into native code, then called from Java. Your application could be a mix of both, if you wish.
More info: http://en.wikipedia.org/wiki/Android_software_development#Native_development_kit

Why does Android need a Virtual Machine(DVM)?

Virtual Machines are used for running apps on different OS(Java compiles code into OS independent .class files which is interpreted into bytecode). Since android apps run on the same OS (i.e Android), it doesn't require a VM (I could be wrong here). Then why does it need a Dalvik VM?
Android Platform can run on different architectures - ARM, MIPs and x86. To abstract out the need to compile binaries for each architecture VM comes into play. It also does memory management for the developer.
We need someone to compile and convert the java classes into bytecode which can be executed by the interpreter.
It is similar to JVM ... you have .java files which will be compiled by java compiler into .class files. the .class files are nothing but bytecode which will be run by the JVM. JVM can reside on any platform(windows,linux or unix).
In android too the files are compiled into .dex files and run by DVM. just to give an idea, when is application is installed, the Android OS assigns unique linux user id, a DVM is assigned for each app. So in short each app has own linux process, DVM and linux user id.
The java files are compiled into .dex files which consume less memory compared to .class files.
Now assume 10 applications are having 10 individual DVM's and the OS has 10 process to handle.
The dispatcher or scheduler in the android OS is responsible for handling these 10 processes....which is why we have android activity life cycle.
You need DVM to maintain the running state of each process(each app).
Why android needs a virtual machine is on the basis that Google engineered Android API to vastly use a Java interface. Java itself is usually run on a virtual machine.
The JVM itself is a stack machine based VM while Android's VM (called Dalvik) is a register based VM (this is for the sake of less code generated and faster speed to get better performance out of whatever device is using Android)
The purpose of a virtual machine is to be able to abstract hardware by simulating it. If you make a virtual machine and compile it to work on every possible hardware, you get what originally made Java rise to popularity: write once run anywhere portability.
You could write code, without having to change it and run it on any hardware that your virtual machine can run on.
Digressing, Android is mostly built in C (and C++?) but the API that manipulates the OS is interfaced through Java, thus you require a virtual machine.

Android NDk and CLI

Is it possible to use CLI - Command Line Interface in Android NDK?? Is there such a function: exec (query)?
Ie is it possible to make queries from the command line C program?
officiallyi have never seen commandline programs on android, whether written in java or android ndk.unofficiallyi have seen commandline c/c++ programs targeting arm processors and the authors claimed they run on android eg follow this link to whet your appetite
My thoughts?Should you write such 'unofficial' applications, i doubt that you will be able to distribute them to your customers eg through the android market since android applications are dalvik executables(kind of java applications compiled and optimised for the dalvik virtual machine)

Categories

Resources