Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Android Programming

Android is a mobile operating system based on a modified version of the


Linux kernel and other open source software, designed primarily for
touchscreen mobile devices such as smartphones and tablets.
How Java and Android Work Together

• After we write a program in Java for Android, we click on a button to change our code into another
form that is understood by Android. This other form is called Dalvik EXecutable (DEX) code, and the
transformation process is called compiling. Compiling takes place on the development machine after
we click on that button.
• The part of the Android system that executes (runs) our compiled DEX code is called the Dalvik
Virtual Machine (DVM). The DVM itself is a piece of software written in another language that runs
on a specially adapted version of the Linux operating system. So what the user sees of Android, is
itself just an app running on another operating system.
• The purpose of the DVM is to hide the complexity and diversity of the hardware and software that
Android runs on but, at the same time, its purpose is to expose all of its useful features. This
exposing of features generally works in two ways. The DVM itself must have access to the hardware,
which it does, but this access must be programmer friendly and easy to use. The way the DVM allows
us access is indeed easy to use because of the Android Application Programming Interface (API).
• The Android API is the code that makes it really easy to do exceptional things. The Android API is
mainly a whole bunch of Java code.
What exactly is Android?

• We know that to get things done on Android, we write Java code of our own,
which also uses the Java code of the Android API. This is then compiled into
DEX code and run by the DVM, which in turn has connections to an underlying
operating system called Linux.
• Then the manufacturers of the Android devices and individual hardware
components write advanced software called drivers, which ensure that their
hardware (CPU, GPU, GPS receivers, and so on) can run on the underlying
Linux operating system.
• Our compiled Java code, along with some other resources, is placed in a
bundle of files called an Android application package (APK), and this is what
the DVM needs to run our app.
• This process is explained in the following figure:
APK is Android Application Package
The Development Environment

• A development environment is a term that refers to having everything you


need in order to develop, set up, and be ready to go in one place. We need the
following two things to get started:
• We talked a fair bit about compiling our Java code, as well as other people's
Java code, into DEX code that will run on the DVM, on people's Android
devices. In order to use Java code, we need a free software called the JDK. The
JDK also includes other people's code, which is separate from the Android API.
• There is a whole range of tools that are required to develop for Android, and
we also need the Android API, of course. This whole suite of requirements is
collectively known as the Android software development kit (SDK). Fortunately,
downloading and installing a single application will give us these things all
bundled together. This single application is called Android Studio.
JDK vs SDK
• Most of people forget that Java Platform is not only used to develop
programs in Java language. The JVM supports some other languages
also.
• Thus, making it clear, the SDK is the generic bundle of software that
supports software creation in a variety of languages like Clojure,
Groovy, Scala, JRuby, and others.
• The JDK is the specific bundle to develop software in Java language,
containing all Java standard API to do so.
Java Virtual Machine (JVM)
and
Dalvik Virtual Machine (DVM)

• Both VM's run applications written in Java, but they use different
techniques and processes to compile and run code
• A runtime system provides an environment to translate the
code written in a high-level language like Java to machine
code and understandable by the Central Process Unit (CPU).
Contd...
• The JVM is a virtual machine to run Java desktop, server, and web applications. Another
important thing about Java is it was developed with portability in mind. Thus, the JVM has
been shaped also to support multiple host architectures and run everywhere. But, it is too
heavy for embedded devices.
• Java has an active community and will continue to be widely used in the future.
• The DVM is a virtual machine to run Android applications. The DVM executes Dalvik
bytecode, which is compiled from programs written in the Java language. Note that the
DVM is not a JVM.
• One of the key design principles of the DVM is that it should run on low memory mobile
devices and loads quicker compared to any JVM. Also, this VM is more efficient when it
runs multiple instances on the same device.
• In 2014, Google released Android Runtime (ART) for Android 5 which replaced Dalvik for
improved application performance battery usage. The last version was 1.6.0 on Android 4.4.
Difference Between JVM and DVM

• Architecture
• The JVM is a stack-based VM where all the arithmetic and logic
operations are carried out via push and pop operands and results are
stored on the stack. The stack is also the data structure to store
methods.
• Contrastingly the DVM is a register-based VM. These registers
located in the CPU carry out all the arithmetic and logic operations.
The register is the data structure to store operands
• Compilation
• Java code is compiled inside the JVM to an intermediary format called
Java bytecode (.class files). Then, the JVM parses the resulting Java
bytecode and translates it to machine code.
• On an Android device, the DVM compiles the Java code to an
intermediate format called Java bytecode (.class file) like the JVM. Then,
with the help of a tool called Dalvik eXchange or dx, it transforms Java
bytecode to Dalvik bytecode. Finally, the DVM translates the Dalvik
bytecode to binary machine code.
• Both VMs use the Just-In-Time (JIT) Compiler. The JIT Compiler is a type
of compiler that performs the compilation during the runtime.
• Performance
• As seen previously, JVM is a Stack-based VM and DVM is a Register-based
VM. Stack-based VM bytecode is very compact because the location of
operands is implicitly on the operand stack. Register-based VM bytecode
requires all the implicit operands to be part of an instruction. That
indicates that the Register-based code size will usually be much larger
than Stack-based bytecode.
• On the other hand, register-based VM's can express computations using
fewer VM instructions than a corresponding stack-based VM. Dispatching
a VM instruction is costly, so the reduction in executed VM instructions
is likely to significantly improve the speed of the register-based VM.
• Execution
• Although it is possible to set up an instance of the JVM per running
application, typically we'll only configure one single instance of a JVM
with shared processes and memory space to run all the applications
we have deployed.
• However, Android was designed to run multiple DVM instances. So to
run an application or a service, the Android OS creates a new DVM
instance with a separate process in shared memory space and
deploys the code to run the application.
.class (byte code) vs .dex file
• Android applications are usually written in Java language and are executed in the Dalvik Virtual
Machine (DVM), which is different from the classical Java Virtual Machine (JVM).
• The DVM is developed by Google and optimized for the characteristics of mobile operating systems
(especially for the Android platform).
• The bytecode running in Dalvik is transferred from traditional JVM bytecode to the dex-format by
translating Java .class files with the conversion tool dx. Contrary to the DVM, JVM is using pure Java
class files.
• JVM bytecode is composed of one or more .class files (each of these contains one Java class). During
run time, JVM will dynamically load the bytecode for each class from the corresponding .class file.
While Dalvik bytecode is only composed of one .dex file, containing all the classes of the application.
• After the Java compiler has created JVM bytecode, the Dalvik dx compiler deletes all .class files and
recompiles them to Dalvik bytecode. Afterwards dx merges them into one .dex file.
• Whereas a Java jar file has many class files, each APK file has only a single classes.dex file. According
to Google, the APK format differs from the class-file format for performance and security reasons.
A Java jar file has many class files, each APK file has only a single classes.dex file,
as shown below. According to Google, the APK format differs from the class-file
format for performance and security reasons.
Dalvik and ART
Dalvik is a discontinued process virtual machine (VM) in Android operating system that executes applications
written for Android 。 (Dalvik bytecode format is still used as a distribution format, but no longer at runtime in
newer Android versions.) Dalvik was an integral part of the Android software stack in the (now unsupported)
Android versions 4.4 "KitKat" and earlier, which were commonly used on mobile devices such as
mobile phones and tablet computers,
and more in some devices such as smart TVs and wearables. Dalvik is open-source software, originally written
by Dan Bornstein, who named it after the fishing village of Dalvík in Eyjafjörður, Iceland.
Programs for Android are commonly written in Java and compiled to bytecode for the Java virtual machine,
which is then translate d to Dalvik bytecode and stored in .dex (Dalvik EXecutable) and .odex (Optimized
Dalvik EXecutable) files; related terms odex and de-odex are associated with respective bytecode
conversions.
The compact Dalvik Executable format is designed for systems that are constrained in terms of memory and
processor speed.
The successor of Dalvik is Android Runtime (ART), which uses the same bytecode and .dex files (but not .odex
files), with the succession aiming at performance improvements transparent to the end users.
The new runtime environment was included for the first time in Android 4.4 "KitKat" as a technology preview,[4][5]
and replaced Dalvik entirely in later versions; Android 5.0 "Lollipop" is the first version in which ART is the only
included runtime
Android Studio
• Android Studio is an integrated development environment (IDE) that
takes care of all the complexities of compiling our code and linking
with the JDK and the Android API. Once we have installed the JDK and
Android Studio, we can do everything we need inside this application.
What makes an Android app
• Java code will be compiled into DEX code that runs on the DVM.
• In addition to this, we will also be adding and editing other files as
well. These files are known as Android resources.
Android Resources

• Our app will include resources such as images, sounds, and user interface layouts that are kept in
separate files from the Java code.
• They will also include files that contain the textual content of our app. It is a convention to refer
to the text in our app through separate files because it makes them easy to change, and this
makes it easy to create apps that work for multiple different languages.
• Furthermore, the actual UI layouts of our apps, despite the option to implement them with a
visual designer, are actually read from text-based files by Android.
• Android (or any computer), of course, cannot read and recognize text in the same way that a
human can. Therefore, we must present our resources in a highly organized and predefined
manner. To do so, we will use Extensible Markup Language (XML). XML is a huge topic but,
fortunately, its whole purpose is to be both human and machine readable. We do not need to
learn this language, we just need to observe (and then conform to) a few rules. Furthermore,
most of the time when we interact with XML, we will do so through a neat visual editor provided
by Android Studio. We can tell when we are dealing with an XML resource because the filename
will end with the .xml extension.

You might also like