Android Note 4 Virtual Machine Dalvik, various Android java package functions, Android-related file types, application structure analysis, ADB

Source: Internet
Author: User

Content of this section

  1. Android Virtual Machine Dalvik
  2. Various java package functions in Android
  3. Android-Related File Types
  4. Application Structure Analysis
  5. ADB (Android Debug Bridge)
Android Virtual Machine DalvikDalvik impact

With the release of Google's Android SDK, there are numerous discussions about its APIs and the expected impact on the mobile phone field. However, one of the topics in the Java Community has stirred up waves, which is the foundation of the Android platform-the Dalvik virtual machine.

Primary differences between Dalvik and standard Java Virtual Machine (JVM)

Dalvik is based on registers, while JVM is based on stacks ., Register-based virtual machines take less time to compile larger programs.

Differences between Dalvik and Java runtime Environments

Dalvik is optimized to allow instances of multiple virtual machines to run simultaneously in limited memory, and each Dalvik application is executed as an independent Linux Process. Independent processes can prevent all programs from being shut down when the Virtual Machine crashes.

Dalvik situation

The birth of Dalvik has also led to concern that the first large-scale splitting of the Java platform may have been in progress-some people have linked Davlik with Microsoft's JVM and Sun's lawsuit against Microsoft, wait to see if similar things will happen on Google. others point out that Google did not claim that Dalvik is a Java implementation, but Microsoft did. Sun also expressed concern about the possible camp split and proposed to work with Google to ensure the compatibility between Dalvik and JVM. Google explained that, dalvik is an attempt to solve the current split on the JavaME platform. It also aims to provide a platform with fewer restrictions and licenses. Some people may even doubt whether this is a large-scale contest between the Sun and Google camps on the future of Java.

Various java package functions in Android

In Android Application Development, the JAVA language is usually used. In addition to the basic knowledge of the JAVA language, you also need to understand the extended JAVA functions provided by Android.
In general JAVA applications, if you need to reference the base class library, you usually need to use the following method:
Import javax. swing. *; the preceding code references the java gui Component Swing. javax. swing is a JAVA package.
Android provides some extended JAVA class libraries. The class libraries are divided into several packages. Each package contains several classes.

Important Package Description:

Android. app

Provides high-level program models and basic runtime Environments

Android. content

Contains various classes for accessing and publishing device data

Android. database

Browse and operate databases through content providers

Android. graphics

The underlying graphics library, including canvas, Color Filtering, vertices, and rectangles, can be directly drawn to the screen.

Android. location

Positioning and related service classes

Android. media

Provides media interfaces for managing multiple types of audio and video.

Android.net

Class that provides network access assistance, exceeding the normal java.net. * interface

Android. OS

Provides system services, message transmission, and IPC Mechanisms.

Android. opengl

Provide OpenGL tools

Android. provider

Provides content providers for class access to Android

Android. telephony

Provides API Interaction Related to dialing

Android. view

Provides basic user interface framework

Android. util

Tools are involved, such as time and date operations.

Android. webkit

Default browser operation interface
Android. widget Contains various UI elements (most visible) used on the application Screen
Android-Related File Types

Java file-application source file

A considerable part of android is written in java (the blue part in the basic shelving diagram is developed in Java), and android applications must be developed in java.

Class file ------ target file after Java Compilation

Unlike J2se, java can be directly compiled into a class, and class files on the android platform cannot be directly run on android. Because Google uses its own Dalvik to run the application, the class here certainly cannot be run in the AndroidDalvik java environment. The android class file is actually only the intermediate target file in the compilation process, you need to link to the dex file before running on dalvik.

Dex file ----- executable files on the Android platform

The bytecode file format supported by the Android Virtual Machine Dalvik is defined by Google on the newly released Android platform using its own Dalvik Virtual Machine. Such virtual machines do not execute Java bytecode, it is another bytecode: dex format. After compiling Java code, you can use tools on the Android platform to convert Java bytecode into Dex bytecode. Although Google calls Dalvik for mobile devices
But many people in the industry think this is to avoid applying for Javalicense from sun. This DalvikVM is the best for cell phone programs/CPUs and can run many VMS at the same time without occupying too much Resource.

Apk file ------- Installation File on Android

Apk is the extension of the Android installation package. An Android installation package contains all files related to an Android Application. In the apk file, compress the AndroidManifest. xml file, application code (. dex file), resource file, and other files into a compressed package. A project can only input a .apk file.

Application Structure Analysis of Android: HelloActivity

This example uses a simple HelloActivity program as an example to briefly introduce the source code structure of the Android application. In fact, Android applications are not very complex, but generally involve JAVA programs, XML files, and makefiles. HelloActivity is simple, but it is small and dirty. It is the best example for learning Android applications.

Part 1: HelloActivity source code

The source code of the HelloActivity project is in the development/samples/HelloActivity/directory of the Android directory. The code structure is as follows:
Tests is an independent project, which can be ignored for the moment. Other parts are considered as an Android Application project. The main components of this project are as follows:
AndroidManifest. xml: The description file of the project, which is useful at runtime.
Android. mk: Makefile of the entire project

HelloActivity source code development/samples/HelloActivity/| -- Android. mk | -- AndroidManifest. xml |-res |-layout | -- hello_activity.xml | -- values | -- strings. xml |-src | -- com | -- example | -- android | -- helloactivity | -- HelloActivity. java -- tests | -- Android. mk | -- AndroidManifest. xml -- src -- com -- android -- helloactivity -- HelloActivityTest. java

Res: directory for storing resource files
Src/com/example/android/helloactivity/HelloActivity. java: This is a JAVA class file. The path of this file represents the position in the structure of the JAVA package of Andorid. The usage of this package is
Com. example. android. helloactivity.

Part 2: Intermediate compilation results

This HelloActivity project will be generated after compilation
Out/target/common/obj/APPS/He loactivity_intermediates/directory. The content in this directory is related to the HelloActivity project. More specifically, it is related to Android in development/samples/HelloActivity. mk file.

Out/target/common/obj/APPS/He loactivity_intermediates/
| -- Classes. dex (bytecode)
| -- Classes. jar (JAR file)
| -- Public_resources.xml (xml generated based on the resources structure)
'-- Src
| -- R. stamp
'-- Com
'-- Example
'-- Android
'-- Helloactivity
'-- R. java (resources generated file)

 Classes. dex

Is the most important file. It is a bytecode file for the JAVA Virtual Machine Dalvik running on Android.
Classes. jar
It is a JAR file, which means Java ARchive, that is, Java ARchive. It is a platform-independent file format that can combine multiple files into one file. The decompressed directory structure: (the class compiled by the JAVA standard) each file with the class extension is actually the bytecode of each class after the JAVA program is compiled.

Part 3: Target apk File

The target apk file is the installation and running file of the Android JAVA Virtual Machine Dalvik. In fact, this apk file will be generated by the Intermediate compilation result and the original file. The essence of an apk file is a zip package. The decompressed directory structure of this APK package is as follows:

Out/target/product/generic/obj/APPS/HelloActivity_intermediates/package.apk _ FILES/
| -- AndroidManifest. xml
| -- META-INF
| -- CERT. RSA
| -- CERT. SF
| '-- MANIFEST. MF
| -- Classes. dex
|-Res
| '-Layout
| '-- Hello_activity.xml
'-- Resources. arsc

It is worth noting that the xml file is processed here, which is not the same as the original file and cannot be read as a text file.

Part 4: various source code files

Android. mk is the "Makefile" of the entire project. Its content is as follows:

LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE_TAGS: = samples
# Only compile source java files in this apk.
LOCAL_SRC_FILES: = $ (call all-java-files-under, src)
LOCAL_PACKAGE_NAME: = HelloActivity
LOCAL_SDK_VERSION: = current
Include $ (BUILD_PACKAGE)
# Use the following include to make our test apk.
Include $ (call all-makefiles-under, $ (LOCAL_PATH ))


This file is similar in various Android projects. LOCAL_PACKAGE_NAME indicates the package name. LOCAL_MODULE_TAGS indicates the module standard.
Samples, where eng development is used in most formal applications (applications in the packages directory.

AndroidManifest. xml is the description file of the HelloActivity project. Its content is as follows:
The package is used to describe the name of the package. The content in android: labeapplication indicates the title of the app displayed on the interface. The android: name in activity indicates the name of the Android activity.


<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. example. android. helloactivity">
<Application android: label = "He llo, Activity! ">
<Activity android: name = "He rawactivity">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
</Manifest>

Package android. hello;

Import android. app. Activity;
Import android. OS. Bundle;

Public class helloword extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
}

File src/com/example/android/helloactivity/HelloActivity. java is the main program file, written as com. example. android. helloactivity indicates the package name. Two packages android are introduced in the file header. app. activity is an Android Activity Package. Every Android Activity must inherit the Activity class.
Package android. OS. Bundle is used to map string values.
OnCreate () is an overloaded function that implements the process of creating an application. SetContentView () sets the current View ).
The setting method is to use a file, which determines the content contained in the view. Here we use
R. layout. hello_activity indicates that the hello_activity.xml file is used from the res/layout/directory.
The res/layout/hello_activity.xml file is as follows:
An editable text (EditText) is defined, and the following items are actually its attributes. android: text indicates
Content, string/hello_activity_text_text indicates to find the corresponding file, that is, in the res/value/string. xml file
Hello_activity_text_text.
Res/value/string. xml is as follows:
Hello_activity_text_text is referenced by the res/layout/hello_activity.xml file.
The text displayed on the screen.
Package com. example. android. helloactivity;
Import android. app. Activity;
Import android. OS. Bundle;
Public class HelloActivity extends Activity {
Public HelloActivity (){
}@
Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. hello_activity );
}
}
<? Xml version = "1.0" encoding = "UTF-8"?>
<EditText xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/text"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: textSize = "18sp"
Android: autoText = "true"
Android: capitalize = "sentences"
Android: text = "@ string/hello_activity_text_text"/>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello_activity_text_text"> He llo, World! </String>
</Resources>

AndroidADB tool usage
Adb (Android Debug Bridge) is a common debugging tool provided by Android. With this tool, my mom can manage devices or cell phone models.
The status of the parsers.
Adb function operations:
Quickly update the code in a device or mobile phone simulator, such as an application or Android system upgrade.
Run shell commands on the device
Manage ports on devices or mobile phone Simulators
Copy and paste files on devices or mobile phone Simulators
Common adb operations:
Install the application to the simulator
Android does not provide a command to uninstall an application, but can only be manually deleted:
Enter the Shell of the device or Simulator
With the preceding commands, you can enter the shell environment of the device or simulator. In this Linux Shell, You can execute various Linux commands,
In addition, if you only want to execute a shell command, you can use the following method:
For example:
The kernel debugging information is printed.
Release Port
You can set any port number as the request port from the host to the simulator or device. For example:
Adb install app.apk
Adb shell
Cd data/app
Rm app.apk
Adb shell
Adb shell [command]
Adb shell dmesg
Adb forward tcp: 5555 tcp: 8000
----------------------------------- Android programming Basics
11
Copy a file
A file can be copied to a device or from a device.
Copy a file or directory to the device or simulator:
For example:
Copy a file or directory from a device or Simulator
For example:
Search/Wait For simulators and device instances
Obtains the list of currently running simulators, device instances, and the status of each instance | waiting for a running Device
View Bug reports
Record wireless communication logs
There are many wireless communication records and there is no need to record them during running. You can set records by using commands.
Obtain the device ID and serial number
Access Database SQLite3
Adb push
Adb push test.txt/tmp/test.txt
Adb pull
Adb pull/android/lib/libwebcore. OS
Adb devices
Adb wait-for-device
Adb bugreport
Adb shell
Logcat-B radio
Adb get-product
Adb get-serialno
Adb shell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.