Android is Google's platform for mobile phone development, and published most of the code, most of its applications are developed in Java, after all, it is a commercial product, it is natural to have reservations. For the embedded Linux development of the people we can learn from its strengths, but also is a future direction and trend of development.
Let's take a look at Android's architecture. The following is an open Android architecture diagram.
Android's system architecture, like its operating system, employs a layered architecture. From the architecture diagram, Android is divided into four tiers, from the high-level to the lower layer, which are application layer, application framework layer, system run-level and Linux core. Blue Represents the Java program, yellow code for running Java program implementation of the virtual machine, green part of the C + + language program library, red Code kernel (Linux kernel +drvier)
1. Application
Android is published with a range of core application packages, including email clients, SMS short message programs, calendars, maps, browsers, contact management programs, and more. All applications are written in the Java language.
2. Application Framework
Developers also have full access to the API framework used by the core application. The architectural design of the application simplifies the reuse of components, and any application can publish its block of functionality and any other application can use its published block of functionality (subject to the security restrictions of the framework). Similarly, the application reuse mechanism also makes it easy for users to replace program components.
Hidden behind each app is a series of services and systems, including;
* Rich and extensible view (views) that can be used to build applications, which include lists (lists), grids (grids), text boxes (texts boxes), buttons (buttons), and even web browsers that can be embedded.
* Content Providers allows applications to access data from another application, such as a contact database, or share their own data
* Resource Manager (Resource Manager) provides access to non-code resources such as local strings, graphics, and layout files.
* Notification Manager (Notification Manager) enables applications to display customized prompts in the status bar.
* Activity Manager is used to manage the application lifecycle and provide common navigation fallback functionality.
For more details and how to write an application from scratch, refer to how to write an Android app.
3. System Runtime Library
1) Program Library
Android contains a number of C + + libraries that can be used by different components of the Android system. They provide services to developers through the Android application framework. Here are some core libraries:
* Bionic System C library-a standard C system function library (LIBC) inherited from BSD, which is specifically tailored for embedded Linux-based devices.
* Media Library-based on PacketVideo Opencore; The library supports a variety of commonly used audio, video format playback and recording, while supporting still image files. Encoding formats include MPEG4, H. MP3, AAC, AMR, JPG, PNG.
* Surface Manager-manages the display subsystem and provides seamless integration of 2D and 3D layers for multiple applications. This part of the code
* Webkit,libwebcore-a new web browser engine with support for Android browser and an embeddable Web view. The engine behind the famous Apple Safari is WebKit
* SGL-The underlying 2D graphics engine
* 3D Libraries-based on OpenGL ES 1.0 APIs can be implemented using hardware 3D acceleration (if available) or using highly optimized 3D soft acceleration.
* FreeType-Bitmap (bitmap) and vector (vectors) font display.
* SQLite-a lightweight relational database engine that is available and powerful for all applications.
* There is a hardware abstraction layer that is not shown in the section above. In fact, Android does not put all the device drivers in the Linux kernel, but in the userspace space, the main reason for this is the GPL protocol, Linux is to follow the protocol to publish, it means that any changes to the Linux kernel, you must publish its source code. And now it can be avoided without having to publish its source code, after all, it is used to make money. And in the Linux kernel for these userspace driver code to open a backdoor, you can let the original userspace driver can not directly control the hardware can be accessed. And just publish the backdoor code. In general, if you want to port Android to other hardware to run, you just need to implement this part of the code. Includes: Monitor driver, sound, camera, gps,gsm, etc.
2) Android Runtime
Android includes a core library that provides most of the functionality of the Java programming language core library.
Each Android application runs in its own process and has a separate instance of the Dalvik virtual machine. Dalvik is designed as a device that can run multiple virtual systems at the same time and efficiently. Dalvik Virtual Machine Execution (. dex) Dalvik executable, which is optimized for small memory usage. While the virtual machine is a register-based, all classes are compiled by the Java compiler and then converted to. dex format by the "DX" tool in the SDK to be executed by the virtual machine.
Dalvik virtual machines rely on some of the features of the Linux kernel, such as threading mechanisms and underlying memory management mechanisms.
4.Linux kernel
Android's core system services rely on the Linux 2.6 kernel, such as security, memory management, process management, network protocol stacks, and drive models. The Linux kernel also acts as an abstraction layer between the hardware and the software stack. In addition, it has been partially modified, mainly involving two parts:
1). Binder (IPC): Provides effective inter-process communication, although the Linux kernel itself has provided these features, but many of the Android system services need to use this feature, for some reason it implements its own set.
2). Power management: Mainly in order to save electricity, after all, is a handheld device, low energy consumption is our pursuit.
Finally talking about the language used by Android, its application development in the Java language, we say that Java generally contains three parts:
1) Java language: that is, its syntax, its code-writing program
2). Java Virtual machine: In order to achieve the principle that the compilation can be run everywhere, Java after compiling the connection does not produce the target machine language, but the Java bytecode this Java common instructions, then need a virtual machine to execute the change instruction.
3). Library: Provide some common libraries like our usual C language
The latter combination is Java Runtime environment.
Android uses a virtual machine called Dalvik, originally not designed for Java, it can not run the Java bytecode instructions, but run called Dalvik executable, referred to as DX. For this purpose, Android provides a DX tool to convert Java bytecode to DX.
Android Source code structure:
Google offers Android contains the original Android target machine code, host compilation tools, simulation environment, the code package after decompression, the first level of directories and files as follows:
.
|--Makefile (Global Makefile)
|--Bionic (bionic meaning bionic, which is the source code of some base libraries)
|--bootloader (boot loader)
|--build (the content in the build directory is not the code used by the target, but the scripts and tools needed to compile and configure)
|--Dalvik (Java Virtual machine)
|--Development (templates and tools needed for program development)
|--external (some libraries used by the target machine)
|--Frameworks (application's framework layer)
|--hardware (Hardware-related libraries)
|--Kernel (source code for Linux2.6)
|--Packages (Android for various applications)
|--prebuilt (Android pre-built script compiled on various platforms)
|--recovery (related to the recovery function of the target)
'--system (some libraries at the bottom of Android)
The bionic directory expands to a level of directories as follows:
bionic/
|--android.mk
|--libc
|--LIBDL
|--LIBM
|--libstdc++
|--libthread_db
'--linker
Two levels of directory expansion for the bootloader directory:
bootloader/
'--legacy
|--android.mk
|--README
|--ARCH_ARMV6
|--arch_msm7k
|--Fastboot_protocol.txt
|--include
|--Libboot
|--libc
|--Nandwrite
'--Usbloader
A directory of the level at which the build directory expands is as follows:
build/
|--Buildspec.mk.default
|--cleanspec.mk
|--core (various files ending with MK, which are required for compiling makefile)
|--envsetup.sh
|--Libs
|--Target (contains board and product two directories, files required for the target)
'--tools (required by the host in the compilation process, some need to be compiled and generated)
The makefile in core is the real makefile required for the entire Android compilation and is referenced by the makefile of the top-level directory.
Envsetup.sh is a script that is used to set up the environment when running with the emulator.
The Dalvik directory is used to provide the underlying ———— Java virtual machine for Android Java applications to run.
The development directory expands to a level of directories as follows:
Development
|--Apps (templates for Android apps)
|--build (Compile script template)
|--Cmds
|--data
|--docs
|--Emulator (simulation related)
|--Host (some tools that include the Windows platform)
|--IDE
|--PDK
|--samples (some sample programs)
|--Simulator (mostly tools for target machines)
'--tools
In the emulator directory, Qemud is a daemon that runs on the target machine using QEMU emulation, and skins is the interface of the phone when it is emulated.
The samples contains a lot of Android simple works, which are great for developers learning to develop Android programs and can be used as templates.
The external directory expands to a level of directories as follows:
external/
|--AES
|--apache-http
|--BlueZ
|--Clearsilver
|--Dbus
|--DHCPCD
|--Dropbear
|--elfcopy
|--elfutils
|--Emma
|--ESD
|--Expat
|--FDLIBM
|--FreeType
|--Gdata
|--Giflib
|--googleclient
|--icu4c
|--iptables
|--Jdiff
|--Jhead
|--JPEG
|--Libffi
|--Libpcap
|--libpng
|--LIBXML2
|--Netcat
|--Netperf
|--Neven
|--Opencore
|--OpenSSL
|--Oprofile
|--Ping
|--PPP
|--Protobuf
|--QEMU
|--SAFE-IOP
|--Skia
|--Sonivox
|--SQLite
|--Srec
|--Strace
|--Tagsoup
|--tcpdump
|--TinyXML
|--tremor
|--WebKit
|--wpa_supplicant
|--YAFFS2
'--zlib
In external, each directory represents a module in the Android target system and may have one or several libraries. which
Opencore is a PV (PacketVideo), which is the core of the Android multimedia framework.
WebKit is the core of Android Web browser.
SQLite is the core of the Android database system.
OpenSSL is a secure Socket layer, a network protocol tier that provides security support for data communication.
The frameworks directory expands to a level of directories as follows:
frameworks/
|--Base
|--opt
'--policies
Frameworks is the framework for Android applications.
Hardware is a number of hardware-related libraries
Kernel is the source code of Linux2.6
The packages directory expands the two levels of directories as follows:
packages/
|--Apps
| |--AlarmClock
| |--Browser
| |--Calculator
| |--Calendar
| |--Camera
| |--Contacts
| |--Email
| |--Googlesearch
| |--Htmlviewer
| |--IM
| |--Launcher
| |--Mms
| |--Music
| |--Packageinstaller
| |--Phone
| |--Settings
| |--Soundrecorder
| |--STK
| |--Sync
| |--Updater
| '--Voicedialer
'--providers
|--Calendarprovider
|--Contactsprovider
|--Downloadprovider
|--Drmprovider
|--Googlecontactsprovider
|--Googlesubscribedfeedsprovider
|--Improvider
|--Mediaprovider
'--Telephonyprovider
Packages contains two directories, including apps in Android, and providers is a content provider (a data source in Android).
The contents of the two directories in packages are mostly programs written in Java, and the hierarchy of folders is similar.
The prebuilt directory expands to a level of directories as follows:
prebuilt/
|--android.mk
|--Android-arm
|--Common
|--darwin-x86
|--linux-x86
'--windows
The two levels of directories that are expanded by the system directory are as follows:
system/
|--bluetooth
| |--bluedroid
| '--Brfpatch
|--Core
| |--android.mk
| |--README
| |--ADB
| |--Cpio
| |--Debuggerd
| |--FastBoot
| |--include (header files for each library interface)
| |--Init
| |--Libctest
| |--libcutils
| |--Liblog
| |--Libmincrypt
| |--libnetutils
| |--Libpixelflinger
| |--Libzipfile
| |--Logcat
| |--Logwrapper
| |--mkbootimg
| |--Mountd
| |--netcfg
| |--RootDir
| |--SH
| '--Toolbox
|--Extras
| |--android.mk
| |--Latencytop
| |--Libpagemap
| |--Librank
| |--Procmem
| |--Procrank
| |--Showmap
| |--Showslab
| |--Sound
| |--su
| |--Tests
| '--Timeinfo
'--WLAN
'--Ti
Android System Architecture