一个android程序建立过程包括:建立工程、编码、编译、打包、签名、运行。
android工程:
src/
src/your/package/namespace/ActivityName.java
. 所有源代码文件 (例如
.java
or
.aidl
files)
bin/
jni/
gen/
R.java
和从AIDL建立的接口文件。
assets/
.apk
file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the
AssetManager
. For example, this is a good location for textures and game data.
res/
- For XML files that are compiled into animation objects. See the Animation resource type.
- For XML files that describe colors. See the Color Values resource type.
- For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or focused). See the Drawable resource type.
- XML files that are compiled into screen layouts (or part of a screen). See the Layout resource type.
- For XML files that define application menus. See the Menus resource type.
-
For arbitrary raw asset files. Saving asset files here instead of in the
assets/
directory only differs in the way that you access them. These files are processed by aapt and must be referenced from the application using a resource identifier in theR
class. For example, this is a good place for media, such as MP3 or Ogg files. -
For XML files that are compiled into many kinds of resource. Unlike other resources in the
res/
directory, resources written to XML files in this folder are not referenced by the file name. Instead, the XML element type controls how the resources is defined within them are placed into theR
class. -
For miscellaneous XML files that configure application components. For example, an XML file that defines a
PreferenceScreen
,AppWidgetProviderInfo
, or Searchability Metadata. See Application Resources for more information about configuring these application components.
anim/
color/
drawable/
layout/
menu/
raw/
values/
xml/
libs/
AndroidManifest.xml
project.properties
local.properties
local.properties
should not be maintained in a source revision control system. If you use Eclipse, this file is not used.
ant.properties
build.xml
The Ant build file for your project. This is only applicable for projects that you build with Ant.
建立过程:
典型的编译过程:
- aapt( Android Asset Packaging Tool): 把你的应用程序资源文件(例如:
AndroidManifest.xml
file and the XML files for your Activities),编译成R.java
。它可以让你从java代码中引用你的资源。 - aidl :转换
.aidl
接口成为java接口 - java编译工具(javac):所有的java代码(包括 :java源码,
R.java
,.aidl
转换后的接口文件)由java编译工具(javac) 编译成中间代码(.class) 文件。 - dex工具(dx):转换中间代码和第三方库的中间代码为Dalvik byte code
- apk建立工具:把所有不需要编译的资源(例如:图片),编译的资源和Dalvik代码(.dex)打包成
.apk
文件 签名(jarsigner).apk
建立后,它必须被签名才能安装到设备上,否则设备会拒绝安装。因为编译工具包含了调试签名的私钥,所以在编译时就直接签名了。但是你要发行版本时,你必须自己对apk包进行签名。- 对齐工具(zipalign):最后,还需要用zipalign工具对包进行对齐。
签名:
签名分两个部分:
-
生成私钥
keytool:
Keytool 选项 描述 -genkey
产生一个键值对(公钥和私钥) -v
允许动作输出 -alias <alias_name>
键的别名。只有前八位字符有效。 -keyalg <alg>
产生键的加密算法。支持DSA和RSA。 -keysize <size>
产生键的长度。如果不支持,keytool用默认值1024 bits.通常我们用2048 bits 或更长的key。 -dname <name>
专有名称,描述谁创建的密钥。该值被用作自签名证书的颁发者和主题字段。注意你可以不在命令行指定。如果没有指定keytool会提示你(CN, OU, and so on)。
-keypass <password>
键的密码。
主要为了安全起见,如果没提供,keytool会提示你输入。
-validity <valdays>
键的有效期,单位:天
Note: A value of 10000 or greater is recommended.
-keystore <keystore-name>.keystore
用于存储私钥的文件。 -storepass <password>
私钥存储文件的密码。
主要为了安全起见,如果没提供,keytool会提示你输入。这个密码不会存储在你的shell历史记录中。
keytool -genkey -v -keystoreHello.keystore -alias HelloWorld-keyalg RSA -keysize2048-validity10000
- 用私钥进行签名
jarsigner:
Jarsigner 选项 描述 -keystore <keystore-name>.keystore
包含你私钥的存储文件 -verbose
显示输出动作。 -sigalg
签名算法,用 SHA1withRSA
.-digestalg
消息摘要算法,用 SHA1
.-storepass <password>
存储文件的密码。
主要为了安全起见,如果没提供,jarsigner会提示你输入。这个密码不会存储在你的shell历史记录中。
-keypass <password>
私钥的密码。
主要为了安全起见,如果没提供,jarsigner会提示你输入。这个密码不会存储在你的shell历史记录中。
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1-keystore Hello.keystore my_application.apk HelloWorld
用eclipse工具:
在工程上点右键->android tools->export singedappplication package
新建key存储文件:
身份信息:
签名:
用已经存在的KEY存储文件签名:
对齐工具(zipalign):
一旦你已经签署了APK与你的私钥,在文件上运行zipalign。此工具可确保所有未压缩的数据开始于一个特定的字节对齐,相对于文件的开始。当一个设备上安装,确保对齐在4字节边界提供了性能优化。当对齐,Android系统能够读取文件使用mmap(),即使它们包含与对齐限制二进制数据,而不是复制所有从包中的数据的。其好处是在RAM中的运行应用程序所消耗的量减少。
zipalign -v 4 HelloWorld.apk HelloWorld_Release.apk
参考: