Executable programs related to Dalvik virtual machines

Source: Internet
Author: User

Executable programs related to Dalvik virtual machines

In the android source code, you will find several executable programs related to the Dalvik concept. Correct distinction between these executable programs will help you understand the internal structure of the Framework. The names and source code paths of these executable programs are shown in Table 9-1.

 

 

The following describes the functions of these executable programs.

9.2.1 dalvikvm

When a Java program is running, a virtual machine is used to explain the Java bytecode. It translates these bytecode into the CPU instruction codes and then executes them. For Java programs, what is responsible for interpreting and executing is a virtual machine. For Linux, this process is just a common process, it is essentially different from a helloworld executable program with only one line of code. Therefore, the method for starting a virtual machine is the same as that for starting any executable program. That is, enter the name of the executable program under the command line, specify the Java class to be executed in the parameter.

Dalvikvm is used to create a virtual machine and execute the Java class specified in the parameter. The following example shows how to use the program.

Create a new Foo. Java file, as shown in the following code:

Class Foo {

Public static void main (string [] ARGs ){

System. Out. println ("Hello Dalvik ");

}

}

Compile the file and generate the JAR file, as shown in the following code:

$ Javac Foo. Java

$ Path =/users/keyd/Android/out/host/darwin-x86/bin: $ path

$ DX -- Dex -- output = Foo. Jar Foo. Class

The function of the DX tool is. class to Dex file, because the program executed by the Dalvik virtual machine is not a standard JAR file, but the JAR file is specially converted to improve execution efficiency, the converted file is the DEX file. The DX tool is part of the android source code and its path is under the out directory. Therefore, you need to add this path before executing dx.

When dx is executed, the -- output parameter is used to specify the output path of the JAR file. Note that the JAR file contains not a pure. Class file, but a DEX file. jar is only a zip package.

After the jar package is generated, you can push the jar package to the device and execute it, as shown in the following code:

$ ADB push Foo. Jar/data/APP

$ ADB shell dalvikvm-CP/data/APP/Foo. Jar foo

Hello Dalvik

The above command First pushes the jar package to the/data/APP directory, because this directory is generally used to store applications, and then uses the ADB shell to execute the dalvikvm program. The execution Syntax of dalvikvm is as follows:

Dalvikvm-CP class path Class Name

We can also feel that dalvikvm serves the same purpose as running Java programs on a PC.

9.2.2 dvz

Dvz incubated a new process from the zygote process, and the new process is also a Dalvik virtual machine. The difference between this process and the virtual machine started by dalvikvm is that most of the classes and resources of the framework have been pre-installed in this process. The following uses a specific example to describe how to use dvz.

First, create an APK project under eclipse. The package name is com. haiii. Android. helloapk. The default activity name is welcome. Its content is as follows:

Public class welcomeextends activity {

/** Called when the activity is firstcreated .*/

@ Override

Public void oncreate (bundlesavedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

}

Public static void main (string [] ARGs ){

System. Out. println ("hellodalvik ");

}

}

This section of code is a very familiar Hello Android code. The only difference is that a static main () function is added, because this function will serve as the entrance to the welcome class.

Next, push the generated APK file to the/data/APP directory and run the Welcome class, as shown in the following code:

# Dvz-classpath/data/APP/helloapk.apk com. haiii. Android. helloapk. Welcome

Hello Dalvik

In mgmain jni_onload

The syntax of dvz is as follows:

Dvz-classpath package name Class Name

At this point, some readers may wonder whether a welcome object can be constructed within the main () function to run the APK? The answer is no, because the Welcome class is not the entry class of the application. In the following sections, we will see that the entry class of an APK is the activitythread class, the activity class is only the class to be called back. Therefore, you cannot use the activity class to start an APK. The dvz tool is only used for debugging the framework development process.

 

This article is based on the analysis of the android kernel.

Book details: http://blog.csdn.net/broadview2006/article/details/6804573

 

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.