Android app is native or JavaVM code? - android

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

Related

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

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.

Why is C++ used in Android Studio (NDK)

I have a question that why is C++ used in Android App Development, What Requirements aren't Full filled by Java or Kotlin so C++ Came to Picture.
Android by itself is kind of linux system, where app is run by davlik virtual machine (and byte code for it is compiled form java / kotlin). Sometimes there is a need to be closer to operation system and develop in native code - say in C/C++. NAtive parts of software have access to other APIs not available from davlik VM

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

java swing compiler for programming on android tablet

Any IDE is available for android tablet to run the java swing?
Currently i am using Android Java Editor IDE for Programming in android.
But it's not compile the swing programs.
Which IDE is compile the swing programs?
And also i wonder to know how android apps compile and run the java files without jdk.
Swing isn't available on Android. Android uses Java as the programming language but doesn't include the complete framework as the JDK does.
Swing is a component in Java SE. To see what classes are included in the Android SDK you can lookup the api documentation at http://developer.android.com/reference/packages.html
The compiled java-classes for Android don't run on the normal JVM but on the Dalvik VM which was built specifically for Android.
i wonder to know how android apps run the java files without jdk'
Instead of JVM android uses DVM(DALVIK VIRTUAL MACHINE)is Register Architecture, designed to run on low memory, uses its own byte code and runs .Dex file (Dalvik Executable File),
Java source code is compiled by the Java compiler into .class files. Then the dx (dexer) tool, part of the Android SDK processes the .class files into a proprietary file format called DEX that contains Dalvik bytecode.

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.

Categories

Resources