当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] ======================= 第一节 ===========================这里简单的介绍了Android的java环境基础,在后面一节中会结合具体的实例来理解这一节的内容。一、Dalvik虚拟

 ======================= 第一节 ===========================

这里简单的介绍了Android的java环境基础,在后面一节中会结合具体的实例来理解这一节的内容。

一、Dalvik虚拟机

Dalvik是Android的程序的java虚拟机,代码在dalvik/下,

./

|-- Android.mk

|-- CleanSpec.mk

|-- MODULE_LICENSE_APACHE2

|-- NOTICE

|-- README.txt

|-- dalvikvm 虚拟机的实现库

|-- dexdump

|-- dexlist

|-- dexopt

|-- docs

|-- dvz

|-- dx

|-- hit

|-- libcore

|-- libcore-disabled

|-- libdex

|-- libnativehelper 使用JNI调用本地代码时用到这个库

|-- run-core-tests.sh

|-- tests

|-- tools

`-- vm

二、Android的java框架

Android层次中第3层是java框架,第四层就是java应用程序。

Android的java类代码,主要是在frameworks/base/core/java/下,

./

|-- Android

|-- com

|-- jarjar-rules.txt

`-- overview.html

我们再看一下frameworks/base/目录

./

|-- Android.mk

|-- CleanSpec.mk

|-- MODULE_LICENSE_APACHE2

|-- NOTICE

|-- api

|-- awt

|-- build

|-- camera

|-- cmds

|-- common

|-- core

|-- data

|-- docs

|-- graphics

|-- include

|-- keystore

|-- libs

|-- location

|-- media

|-- native

|-- obex

|-- opengl

|-- packages

|-- preloaded-classes

|-- sax

|-- services

|-- telephony

|-- test-runner

|-- tests

|-- tools

|-- vpn

`-- wifi

这里也有Android的java框架代码。

三、JNI

在Android中,通过JNI,java可以调用C写的代码,主要的实现是在frameworks/base/core/jni,通过查看Android.mk,我们可以看到最后生成了libandroid_runtime.so,具体实现JNI功能需要上面我们介绍的libnativehelper.so,

四、系统服务之java

1、binder,提供Android的IPC功能

2、servicemanager,服务管理的服务器端

3、系统进程zygote,负责孵化所有的新应用

======================= 第二节 ==========================

在我平时工作中主要是进行linux网络子系统的模块开发、linux应用程序(C/C++)开发。在学习和从事驱动模块开发的过程中,如果你对linux系统本身,包括应用程序开发都不了解,那么读内核代码就如同天书,毫无意义,所以我分析框架也是从基本系统api开始的,当然也不会太多涉及到应用程序开发。

好,开始这节主要是讲一个简单的adnroid应用程序,从应用程序出发,到框架代码。

分析的应用程序我们也奉行拿来主义:froyo/development/samples/HelloActivity

./

|-- Android.mk

|-- AndroidManifest.xml

|-- res

|-- src

`-- tests

其他的就多说了,看代码

/**

* Copyright (C) 2006 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package Android.util;

import com.Android.internal.os.RuntimeInit;

import java.io.PrintWriter;

import java.io.StringWriter;

/**

* API for sending log output.

*

*

Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e()

* methods.

*

*

The order in terms of verbosity, from least to most is

* ERROR, WARN, INFO, DEBUG, VERBOSE. Verbose should never be compiled

* into an application except during development. Debug logs are compiled

* in but stripped at runtime. Error, warning and info logs are always kept.

*

*

Tip: A good convention is to declare a TAG constant

* in your class:

*

*

private static final String TAG = "MyActivity";

 

*

* and use that in subsequent calls to the log methods.

*

 

*

*

Tip: Don't forget that when you make a call like

*

Log.v(TAG, "index=" + i);

 

* that when you're building the string to pass into Log.d, the compiler uses a

* StringBuilder and at least three allocations occur: the StringBuilder

* itself, the buffer, and the String object. Realistically, there is also

* another buffer allocation and copy, and even more pressure on the gc.

* That means that if your log message is filtered out, you might be doing[!--empirenews.page--]

* significant work and incurring significant overhead.

*/

public final class Log {

/**

* Priority constant for the println method; use Log.v.

*/

public static final int VERBOSE = 2;

/**

* Priority constant for the println method; use Log.d.

*/

public static final int DEBUG = 3;

/**

* Priority constant for the println method; use Log.i.

*/

public static final int INFO = 4;

/**

* Priority constant for the println method; use Log.w.

*/

public static final int WARN = 5;

/**

* Priority constant for the println method; use Log.e.

*/

public static final int ERROR = 6;

/**

* Priority constant for the println method.

*/

public static final int ASSERT = 7;

/**

* Exception class used to capture a stack trace in {@link #wtf()}.

*/

private static class TerribleFailure extends Exception {

TerribleFailure(String msg, Throwable cause) { super(msg, cause); }

}

private Log() {

}

/**

* Send a {@link #VERBOSE} log message.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

*/

public static int v(String tag, String msg) {

return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);

}

/**

* Send a {@link #VERBOSE} log message and log the exception.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

* @param tr An exception to log

*/

public static int v(String tag, String msg, Throwable tr) {

return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '/n' + getStackTraceString(tr));

}

/**

* Send a {@link #DEBUG} log message.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

*/

public static int d(String tag, String msg) {

return println_native(LOG_ID_MAIN, DEBUG, tag, msg);

}

/**

* Send a {@link #DEBUG} log message and log the exception.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

* @param tr An exception to log

*/

public static int d(String tag, String msg, Throwable tr) {

return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '/n' + getStackTraceString(tr));

}

/**

* Send an {@link #INFO} log message.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

*/

public static int i(String tag, String msg) {

return println_native(LOG_ID_MAIN, INFO, tag, msg);

}

/**

* Send a {@link #INFO} log message and log the exception.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

* @param tr An exception to log

*/

public static int i(String tag, String msg, Throwable tr) {

return println_native(LOG_ID_MAIN, INFO, tag, msg + '/n' + getStackTraceString(tr));

}

/**

* Send a {@link #WARN} log message.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

*/

public static int w(String tag, String msg) {

return println_native(LOG_ID_MAIN, WARN, tag, msg);

}

/**

* Send a {@link #WARN} log message and log the exception.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

* @param tr An exception to log

*/

public static int w(String tag, String msg, Throwable tr) {

return println_native(LOG_ID_MAIN, WARN, tag, msg + '/n' + getStackTraceString(tr));

}

/**

* Checks to see whether or not a log for the specified tag is loggable at the specified level.

*

* The default level of any tag is set to INFO. This means that any level above and including[!--empirenews.page--]

* INFO will be logged. Before you make any calls to a logging method you should check to see

* if your tag should be logged. You can change the default level by setting a system property:

* 'setprop log.tag. '

* Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will

* turn off all logging for your tag. You can also create a local.prop file that with the

* following in it:

* 'log.tag.='

* and place that in /data/local.prop.

*

* @param tag The tag to check.

* @param level The level to check.

* @return Whether or not that this is allowed to be logged.

* @throws IllegalArgumentException is thrown if the tag.length() > 23.

*/

public static native boolean isLoggable(String tag, int level);

/**

* Send a {@link #WARN} log message and log the exception.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param tr An exception to log

*/

public static int w(String tag, Throwable tr) {

return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));

}

/**

* Send an {@link #ERROR} log message.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

*/

public static int e(String tag, String msg) {

return println_native(LOG_ID_MAIN, ERROR, tag, msg);

}

/**

* Send a {@link #ERROR} log message and log the exception.

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

* @param tr An exception to log

*/

public static int e(String tag, String msg, Throwable tr) {

return println_native(LOG_ID_MAIN, ERROR, tag, msg + '/n' + getStackTraceString(tr));

}

/**

* What a Terrible Failure: Report a condition that should never happen.

* The error will always be logged at level ASSERT with the call stack.

* Depending on system configuration, a report may be added to the

* {@link Android.os.DropBoxManager} and/or the process may be terminated

* immediately with an error dialog.

* @param tag Used to identify the source of a log message.

* @param msg The message you would like logged.

*/

public static int wtf(String tag, String msg) {

return wtf(tag, msg, null);

}

/**

* What a Terrible Failure: Report an exception that should never happen.

* Similar to {@link #wtf(String, String)}, with an exception to log.

* @param tag Used to identify the source of a log message.

* @param tr An exception to log.

*/

public static int wtf(String tag, Throwable tr) {

return wtf(tag, tr.getMessage(), tr);

}

/**

* What a Terrible Failure: Report an exception that should never happen.

* Similar to {@link #wtf(String, Throwable)}, with a message as well.

* @param tag Used to identify the source of a log message.

* @param msg The message you would like logged.

* @param tr An exception to log. May be null.

*/

public static int wtf(String tag, String msg, Throwable tr) {

tr = new TerribleFailure(msg, tr);

int bytes = println_native(LOG_ID_MAIN, ASSERT, tag, getStackTraceString(tr));

RuntimeInit.wtf(tag, tr);

return bytes;

}

/**

* Handy function to get a loggable stack trace from a Throwable

* @param tr An exception to log

*/

public static String getStackTraceString(Throwable tr) {

if (tr == null) {

return "";

}

StringWriter sw = new StringWriter();

PrintWriter pw = new PrintWriter(sw);

tr.printStackTrace(pw);

return sw.toString();

}

/**

* Low-level logging call.

* @param priority The priority/type of this log message

* @param tag Used to identify the source of a log message. It usually identifies

* the class or activity where the log call occurs.

* @param msg The message you would like logged.

* @return The number of bytes written.

*/

public static int println(int priority, String tag, String msg) {

return println_native(LOG_ID_MAIN, priority, tag, msg);

}

/** @hide */ public static final int LOG_ID_MAIN = 0;

/** @hide */ public static final int LOG_ID_RADIO = 1;

/** @hide */ public static final int LOG_ID_EVENTS = 2;[!--empirenews.page--]

/** @hide */ public static final int LOG_ID_SYSTEM = 3;

/** @hide */ public static native int println_native(int bufID,

int priority, String tag, String msg);

}

我们看到所有代码都是调用public static native int println_native(int bufID,

int priority, String tag, String msg);来实现输出的,这个函数的实现就是C++,调用的方式就是JNI

我们看一下对应的jni代码froyo/frameworks/base/core/jni/Android_util_Log.cpp,最终调用的输出函数是

/*

* In class Android.util.Log:

* public static native int println_native(int buffer, int priority, String tag, String msg)

*/

static jint Android_util_Log_println_native(JNIEnv* env, jobject clazz,

jint bufID, jint priority, jstring tagObj, jstring msgObj)

{

const char* tag = NULL;

const char* msg = NULL;

if (msgObj == NULL) {

jclass npeClazz;

npeClazz = env->FindClass("java/lang/NullPointerException");

assert(npeClazz != NULL);

env->ThrowNew(npeClazz, "println needs a message");

return -1;

}

if (bufID < 0 || bufID >= LOG_ID_MAX) {

jclass npeClazz;

npeClazz = env->FindClass("java/lang/NullPointerException");

assert(npeClazz != NULL);

env->ThrowNew(npeClazz, "bad bufID");

return -1;

}

if (tagObj != NULL)

tag = env->GetStringUTFChars(tagObj, NULL);

msg = env->GetStringUTFChars(msgObj, NULL);

int res = __Android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);

if (tag != NULL)

env->ReleaseStringUTFChars(tagObj, tag);

env->ReleaseStringUTFChars(msgObj, msg);

return res;

}

当然我们发现最终输出是

? 1int res = __Android_log_buf_write(bufID, (android_LogPriority)priority, tag, msg);

用力grep了一下代码,结果如下

./system/core/include/cutils/log.h:int __Android_log_buf_write(int bufID, int prio, const char *tag, const char *text);

./system/core/liblog/logd_write.c:int __Android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)

./system/core/liblog/logd_write.c: return __Android_log_buf_write(bufID, prio, tag, buf);

这个就是和Android专用驱动进行通信的方式,这个分析下去就有点深了,后面分析。

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

9月2日消息,不造车的华为或将催生出更大的独角兽公司,随着阿维塔和赛力斯的入局,华为引望愈发显得引人瞩目。

关键字: 阿维塔 塞力斯 华为

加利福尼亚州圣克拉拉县2024年8月30日 /美通社/ -- 数字化转型技术解决方案公司Trianz今天宣布,该公司与Amazon Web Services (AWS)签订了...

关键字: AWS AN BSP 数字化

伦敦2024年8月29日 /美通社/ -- 英国汽车技术公司SODA.Auto推出其旗舰产品SODA V,这是全球首款涵盖汽车工程师从创意到认证的所有需求的工具,可用于创建软件定义汽车。 SODA V工具的开发耗时1.5...

关键字: 汽车 人工智能 智能驱动 BSP

北京2024年8月28日 /美通社/ -- 越来越多用户希望企业业务能7×24不间断运行,同时企业却面临越来越多业务中断的风险,如企业系统复杂性的增加,频繁的功能更新和发布等。如何确保业务连续性,提升韧性,成...

关键字: 亚马逊 解密 控制平面 BSP

8月30日消息,据媒体报道,腾讯和网易近期正在缩减他们对日本游戏市场的投资。

关键字: 腾讯 编码器 CPU

8月28日消息,今天上午,2024中国国际大数据产业博览会开幕式在贵阳举行,华为董事、质量流程IT总裁陶景文发表了演讲。

关键字: 华为 12nm EDA 半导体

8月28日消息,在2024中国国际大数据产业博览会上,华为常务董事、华为云CEO张平安发表演讲称,数字世界的话语权最终是由生态的繁荣决定的。

关键字: 华为 12nm 手机 卫星通信

要点: 有效应对环境变化,经营业绩稳中有升 落实提质增效举措,毛利润率延续升势 战略布局成效显著,战新业务引领增长 以科技创新为引领,提升企业核心竞争力 坚持高质量发展策略,塑强核心竞争优势...

关键字: 通信 BSP 电信运营商 数字经济

北京2024年8月27日 /美通社/ -- 8月21日,由中央广播电视总台与中国电影电视技术学会联合牵头组建的NVI技术创新联盟在BIRTV2024超高清全产业链发展研讨会上宣布正式成立。 活动现场 NVI技术创新联...

关键字: VI 传输协议 音频 BSP

北京2024年8月27日 /美通社/ -- 在8月23日举办的2024年长三角生态绿色一体化发展示范区联合招商会上,软通动力信息技术(集团)股份有限公司(以下简称"软通动力")与长三角投资(上海)有限...

关键字: BSP 信息技术
关闭
关闭