Android执行文件apk的组成结构
fmms
13年前
<div id="article_content" class="article_content"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span lang="EN-US"><span style="font-family:Times New Roman;">APK</span> </span><span style="font-family:宋体;">是</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Android<span> </span>PacKage</span> </span><span style="font-family:宋体;">的缩写,即</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Android</span> </span><span style="font-family:宋体;">安装包。将</span> <span lang="EN-US"><span style="font-family:Times New Roman;">APK</span> </span><span style="font-family:宋体;">文件直接传到</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Android</span> </span><span style="font-family:宋体;">模拟器或</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Android</span> </span><span style="font-family:宋体;">手机中执行即可安装。</span> <span lang="EN-US"><span style="font-family:Times New Roman;">APK</span> </span><span style="font-family:宋体;">文件其实是</span> <span lang="EN-US"><span style="font-family:Times New Roman;">zip</span> </span><span style="font-family:宋体;">格式,但后缀名被修改为</span> <span lang="EN-US"><span style="font-family:Times New Roman;">apk</span> </span><span style="font-family:宋体;">,在</span> <span lang="EN-US"><span style="font-family:Times New Roman;">windows</span> </span><span style="font-family:宋体;">上可以通过</span> <span lang="EN-US"><span style="font-family:Times New Roman;">winrar</span> </span><span style="font-family:宋体;">等程序直接解压查看。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;"> </span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">解压</span> <span lang="EN-US"><span style="font-family:Times New Roman;">apk</span> </span><span style="font-family:宋体;">后,一般的可看到的目录结构如下:</span> </span></p> <p> </p> <table style="border-collapse:collapse;" class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0"> <tbody> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:140.4pt;padding-right:5.4pt;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top" width="187"> <p style="text-align:center;margin:0cm 0cm 0pt;" class="MsoNormal" align="center"><strong><span style="font-size:small;"><span style="font-family:宋体;">文件或目录</span> </span></strong></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:285.7pt;padding-right:5.4pt;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top" width="381"> <p style="text-align:center;margin:0cm 0cm 0pt;" class="MsoNormal" align="center"><strong><span style="font-size:small;"><span style="font-family:宋体;">作用</span> </span></strong></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:140.4pt;padding-right:5.4pt;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top" width="187"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">META-INF/</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:285.7pt;padding-right:5.4pt;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top" width="381"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">也就是一个</span> <span lang="EN-US"><span style="font-family:Times New Roman;">manifest</span> </span><span style="font-family:宋体;">,从</span> <span lang="EN-US"><span style="font-family:Times New Roman;">java jar</span> </span><span style="font-family:宋体;">文件引入的描述包信息的目录</span> </span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:140.4pt;padding-right:5.4pt;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top" width="187"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:285.7pt;padding-right:5.4pt;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top" width="381"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-family:宋体;"><span style="font-size:small;">存放资源文件的目录</span> </span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:140.4pt;padding-right:5.4pt;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top" width="187"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">libs/</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:285.7pt;padding-right:5.4pt;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top" width="381"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">如果存在的话,存放的是</span> <span lang="EN-US"><span style="font-family:Times New Roman;">ndk</span> </span><span style="font-family:宋体;">编出来的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">so</span> </span><span style="font-family:宋体;">库</span> </span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:140.4pt;padding-right:5.4pt;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top" width="187"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">AndroidManifest.xml</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:285.7pt;padding-right:5.4pt;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top" width="381"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-family:宋体;"><span style="font-size:small;">程序全局配置文件</span> </span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:140.4pt;padding-right:5.4pt;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top" width="187"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">classes.dex</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:285.7pt;padding-right:5.4pt;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top" width="381"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">最终生成的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">dalvik</span> </span><span style="font-family:宋体;">字节码</span> </span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:140.4pt;padding-right:5.4pt;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top" width="187"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">resources.ars</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:5.4pt;width:285.7pt;padding-right:5.4pt;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top" width="381"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-family:宋体;"><span style="font-size:small;">编译后的二进制资源文件</span> </span></p> </td> </tr> </tbody> </table> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;"> </span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">这里资源文件时相对比较重要的,</span> <span lang="EN-US"><span style="font-family:Times New Roman;">sdk </span></span><span style="font-family:宋体;">文档</span> <span lang="EN-US"><span style="font-family:Times New Roman;">(</span> </span><span style="font-family:宋体;">中文指南</span> <span lang="EN-US"><span style="font-family:Times New Roman;">)</span> </span><span style="font-family:宋体;">上有相关的详细介绍:</span> </span></p> <p> </p> <table style="width:100%;border-collapse:collapse;" class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0"> <tbody> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top"> <p style="text-align:center;margin:0cm 0cm 0pt;" class="MsoNormal" align="center"><strong><span style="font-size:small;"><span style="font-family:宋体;">目录</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Directory</span> </span></span></strong></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:0cm;width:80%;padding-right:0cm;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top" width="80%"> <p style="text-align:center;margin:0cm 0cm 0pt;" class="MsoNormal" align="center"><strong><span style="font-size:small;"><span style="font-family:宋体;">资源类型</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Resource Types</span> </span></span></strong></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/anim/</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">Define pre-determined animations.<br /> Tween animations are saved in res/anim/ and accessed from the R.anim class.<br /> Frame animations are saved in res/drawable/ and accessed from the R.drawable class.</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">定义的是预置的动画对象。一般是逐帧动画</span> <span lang="EN-US"><span style="font-family:Times New Roman;">(Frame animations)</span> </span><span style="font-family:宋体;">或补间动画(</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Tween animations</span> </span><span style="font-family:宋体;">)。而实际使用上,都是一些淡入淡出、缩放和移动等的补间动画居多。</span> </span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/color</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">Define a color resources that changes based on the View state.<br /> Saved in res/color/ and accessed from the R.color class</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">定义一些</span> <span lang="EN-US"><span style="font-family:Times New Roman;">android view</span> </span><span style="font-family:宋体;">状态变化时,使用的颜色值。这通常是绑定到一个界面元素上的,比如一个</span> <span lang="EN-US"><span style="font-family:Times New Roman;">button</span> </span><span style="font-family:宋体;">被按下,弹起或</span> <span lang="EN-US"><span style="font-family:Times New Roman;">disable</span> </span><span style="font-family:宋体;">时的颜色。</span> </span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/drawable/</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/drawable-hdpi</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/drawable-land-hdpi</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/drawable/mdpi</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/drawable/ldpi</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/drawable/port</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">…</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">Define various graphics with bitmaps or XML.<br /> Saved in res/drawable/ and accessed from the R.drawable class.</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">定义要被用到的位图资源文件。这些位图资源可以是</span> <span lang="EN-US"><span style="font-family:Times New Roman;">bitmap</span> </span><span style="font-family:宋体;">,也可以是用</span> <span lang="EN-US"><span style="font-family:Times New Roman;">xml</span> </span><span style="font-family:宋体;">描述的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">bitmap</span> </span><span style="font-family:宋体;">。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">注意:文件后缀为</span> <span lang="EN-US"><span style="font-family:Times New Roman;">9.png</span> </span><span style="font-family:宋体;">是一种特殊的位图,一般是通过</span> <span lang="EN-US"><span style="font-family:Times New Roman;">draw9patch</span> </span><span style="font-family:宋体;">生成的,是一种可自动伸缩的位图资源。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span lang="EN-US"><span style="font-family:Times New Roman;">Drawable</span> </span><span style="font-family:宋体;">的其他目录形势,</span> <span lang="EN-US"><span style="font-family:Times New Roman;">land</span> </span><span style="font-family:宋体;">是横屏的,</span> <span lang="EN-US"><span style="font-family:Times New Roman;">port</span> </span><span style="font-family:宋体;">是竖屏的;</span> <span lang="EN-US"><span style="font-family:Times New Roman;">hdpi</span> </span><span style="font-family:宋体;">是高分辨率的,</span> <span lang="EN-US"><span style="font-family:Times New Roman;">ldpi</span> </span><span style="font-family:宋体;">是低分辨率的,</span> <span lang="EN-US"><span style="font-family:Times New Roman;">hdpi</span> </span><span style="font-family:宋体;">是高分辨率的。这里可以放置为特定情况下的界面优化资源。</span> </span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/layout/</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">Define the layout for your application UI.<br /> Saved in res/layout/ and accessed from the R.layout class.</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">定义的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">UI layout</span> </span><span style="font-family:宋体;">,被</span> <span lang="EN-US"><span style="font-family:Times New Roman;">aapt parser</span> </span><span style="font-family:宋体;">后,可由</span> <span lang="EN-US"><span style="font-family:Times New Roman;">android</span> </span><span style="font-family:宋体;">直接</span> <span lang="EN-US"><span style="font-family:Times New Roman;">render</span> </span><span style="font-family:宋体;">成</span> <span lang="EN-US"><span style="font-family:Times New Roman;">view</span> </span><span style="font-family:宋体;">界面。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">这里也有横竖屏和</span> <span lang="EN-US"><span style="font-family:Times New Roman;">dpi</span> </span><span style="font-family:宋体;">之分。</span> </span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/values/</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">可以被编译成很多种类型的资源的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">XML</span> </span><span style="font-family:宋体;">文件。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">注意</span> <span lang="EN-US"><span style="font-family:Times New Roman;">: </span></span><span style="font-family:宋体;">不像其他的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">res/</span> </span><span style="font-family:宋体;">文件夹,它可以保存任意数量的文件,这些文件保存了要创建资源的描述,而不是资源本身。</span> <span lang="EN-US"><span style="font-family:Times New Roman;">XML</span> </span><span style="font-family:宋体;">元素类型控制这些资源应该放在</span> <span lang="EN-US"><span style="font-family:Times New Roman;">R</span> </span><span style="font-family:宋体;">类的什么地方。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-family:宋体;"><span style="font-size:small;">尽管这个文件夹里的文件可以任意命名,不过下面使一些比较典型的文件(文件命名的惯例是将元素类型包含在该名称之中):</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span lang="EN-US"><span style="font-family:Times New Roman;"> array.xml </span></span><span style="font-family:宋体;">定义数组</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span lang="EN-US"><span style="font-family:Times New Roman;"> colors.xml </span></span><span style="font-family:宋体;">定义</span> <span lang="EN-US"><a title="http://code.google.com/android/reference/available-resources.html#colordrawableresources" href="/misc/goto?guid=4959517550079505267"><span style="font-family:Times New Roman;">color drawable</span> </a></span><span style="font-family:宋体;">和</span> <span lang="EN-US"><a title="http://code.google.com/android/devel/resources-i18n.html#colorvals" href="/misc/goto?guid=4959517550165756332"><span style="font-family:宋体;" lang="EN-US"><span lang="EN-US">颜色的字符串值</span> </span><span style="font-family:Times New Roman;">(color string values)</span> </a></span><span style="font-family:宋体;">。使用</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Resource.getDrawable()</span> </span><span style="font-family:宋体;">和</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Resources.getColor()</span> </span><span style="font-family:宋体;">分别获得这些资源。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span lang="EN-US"><span style="font-family:Times New Roman;"> dimens.xml</span> </span><span style="font-family:宋体;">定义</span> <span lang="EN-US"><a title="http://code.google.com/android/reference/available-resources.html#dimension" href="/misc/goto?guid=4959517550243504701"><span style="font-family:宋体;" lang="EN-US"><span lang="EN-US">尺寸值</span> </span><span style="font-family:Times New Roman;">(dimension value)</span> </a></span><span style="font-family:宋体;">。使用</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Resources.getDimension()</span> </span><span style="font-family:宋体;">获得这些资源。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span lang="EN-US"><span style="font-family:Times New Roman;"> strings.xml</span> </span><span style="font-family:宋体;">定义</span> <span lang="EN-US"><a title="http://code.google.com/android/reference/available-resources.html#stringresources" href="/misc/goto?guid=4959517550326969609"><span style="font-family:宋体;" lang="EN-US"><span lang="EN-US">字符串</span> </span><span style="font-family:Times New Roman;">(string)</span> </a></span><span style="font-family:宋体;">值。使用</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Resources.getString()</span> </span><span style="font-family:宋体;">或者</span> <span lang="EN-US"><span style="font-family:Times New Roman;">Resources.getText()</span> </span><span style="font-family:宋体;">获取这些资源。</span> <span lang="EN-US"><span style="font-family:Times New Roman;">getText()</span> </span><span style="font-family:宋体;">会保留在</span> <span lang="EN-US"><span style="font-family:Times New Roman;">UI</span> </span><span style="font-family:宋体;">字符串上应用的丰富的文本样式。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span lang="EN-US"><span style="font-family:Times New Roman;"> styles.xml </span></span><span style="font-family:宋体;">定义</span> <span lang="EN-US"><a title="http://code.google.com/android/reference/available-resources.html#stylesandthemes" href="/misc/goto?guid=4959517550410394213"><span style="font-family:宋体;" lang="EN-US"><span lang="EN-US">样式</span> </span><span style="font-family:Times New Roman;">(style)</span> </a></span><span style="font-family:宋体;">对象。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">多国语言,由</span> <span lang="EN-US"><span style="font-family:Times New Roman;">values-xxx</span> </span><span style="font-family:宋体;">的后缀组成。比如简体中文</span> <span lang="EN-US"><span style="font-family:Times New Roman;">:res/values-zh-rCN</span> </span></span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/xml/</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">任意的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">XML</span> </span><span style="font-family:宋体;">文件,在运行时可以通过调用</span> <span lang="EN-US"><a title="http://code.google.com/android/reference/android/content/Resources.html#getXml(int)" href="/misc/goto?guid=4959517550494227824"><span style="font-family:Times New Roman;">Resources.getXML()</span> </a></span><span style="font-family:宋体;">读取。</span> </span></p> </td> </tr> <tr> <td style="border-bottom:windowtext 1pt solid;border-left:windowtext 1pt solid;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:windowtext 1pt solid;border-right:windowtext 1pt solid;padding-top:0cm;" valign="top"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;">res/raw/</span> </span></p> </td> <td style="border-bottom:#ece9d8;border-left:#ece9d8;padding-bottom:0cm;background-color:transparent;padding-left:0cm;padding-right:0cm;border-top:#ece9d8;border-right:#ece9d8;padding-top:0cm;"> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">直接复制到设备中的任意文件。它们无需编译,添加到你的应用程序编译产生的压缩文件中。要使用这些资源,可以调用</span> <span lang="EN-US"><a title="http://code.google.com/android/reference/android/content/Resources.html#openRawResource(int)" href="/misc/goto?guid=4959517550585735279"><span style="font-family:Times New Roman;">Resources.openRawResource()</span> </a></span><span style="font-family:宋体;">,参数是资源的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">ID</span> </span><span style="font-family:宋体;">,即</span> <span lang="EN-US"><span style="font-family:Times New Roman;">R.raw.somefilename</span> </span><span style="font-family:宋体;">。</span> </span></p> </td> </tr> </tbody> </table> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;"> </span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;"> </span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span lang="EN-US"><span style="font-family:Times New Roman;">Android</span> </span><span style="font-family:宋体;">应用程序的生成过程,输入就是我们在</span> <span lang="EN-US"><span style="font-family:Times New Roman;">eclipse</span> </span><span style="font-family:宋体;">或源码中监理的工程及其下面的源文件。输出就是处理后的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">apk</span> </span><span style="font-family:宋体;">文件。整个过程可以如下图所示:</span> </span></p> <p style="text-align:center;margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;"><img alt="Android执行文件apk的组成结构 " src="https://simg.open-open.com/show/5bc934aa3c00ac5e3a8e8620c76aa6e4.png" width="600" height="368" /> </span></span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;"> </span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"> </p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"> </p> <p style="text-indent:-18pt;margin:0cm 0cm 0pt 18pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">整个过程的第一步,资源打包的工作,就是</span> <span lang="EN-US"><span style="font-family:Times New Roman;">aapt</span> </span><span style="font-family:宋体;">这个工具完成的。</span> </span></p> <p style="text-indent:-18pt;margin:0cm 0cm 0pt 18pt;" class="MsoNormal"><span style="font-size:small;"><span style="font-family:宋体;">1. </span> </span><span style="font-size:small;"><span style="font-family:宋体;">在最后打包的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">apk</span> </span><span style="font-family:宋体;">中,所有的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">xml</span> </span><span style="font-family:宋体;">文件已经不是原来的文本文件了,是被</span> <span lang="EN-US"><span style="font-family:Times New Roman;">aapt parser</span> </span><span style="font-family:宋体;">后,直接保存下来的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">xml</span> </span><span style="font-family:宋体;">数据结构,这样做的一大好处就是:到手机中无需再次</span> <span lang="EN-US"><span style="font-family:Times New Roman;">parser xml</span> </span><span style="font-family:宋体;">文件,直接读到定义好的数据结构中就可以了。</span> </span></p> <p style="text-indent:-18pt;margin:0cm 0cm 0pt 18pt;" class="MsoNormal"><span style="font-family:Times New Roman;"><span lang="EN-US"><span><span style="font-size:small;">2.</span> <span style="LINE-HEIGHT: normal; FONT-VARIANT: normal; FONT-STYLE: normal; FONT-SIZE: 7pt; FONT-WEIGHT: normal" roman?;?="" new="" times=""> </span></span></span><span lang="EN-US"><span style="font-size:small;">drawable</span> </span></span><span style="font-size:small;"><span style="font-family:宋体;">中的</span> <span lang="EN-US"><span style="font-family:Times New Roman;">png</span> </span><span style="font-family:宋体;">图片也被</span> <span lang="EN-US"><span style="font-family:Times New Roman;">aapt</span> </span><span style="font-family:宋体;">给优化过了。</span> </span></p> <p style="text-indent:-18pt;margin:0cm 0cm 0pt 18pt;" class="MsoNormal"><span lang="EN-US"><span><span style="font-family:Times New Roman;"><span style="font-size:small;">3.</span> <span style="LINE-HEIGHT: normal; FONT-VARIANT: normal; FONT-STYLE: normal; FONT-SIZE: 7pt; FONT-WEIGHT: normal" roman?;?="" new="" times=""> </span></span></span></span><span style="font-size:small;"><span style="font-family:宋体;">所有的资源文件都被自动生成一个索引,并生成到</span> <span lang="EN-US"><span style="font-family:Times New Roman;">R.java</span> </span><span style="font-family:宋体;">中。为什么这么做?我想一个是效率,另一个好处就是最大限度的在编译过程中由编译器给你找错</span> <span lang="EN-US"><span style="font-family:Times New Roman;">(</span> </span><span style="font-family:宋体;">通过</span> <span lang="EN-US"><span style="font-family:Times New Roman;">string</span> </span><span style="font-family:宋体;">来索引很难做到</span> <span lang="EN-US"><span style="font-family:Times New Roman;">)</span> </span><span style="font-family:宋体;">。</span> </span></p> <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Times New Roman;font-size:small;"> </span> </span></p> <p><span style="font-family:宋体;font-size:10.5pt;">其实这里,我们最关心的就是android对资源究竟做了些什么,还是看看</span> <span style="FONT-FAMILY: " lang="EN-US" new="" times="" roman?;font-size:10.5pt;?="">aapt</span> <span style="font-family:宋体;font-size:10.5pt;">的代码吧</span> </p> </div>