Android开发教程(十)——android程序建立过程

目录

 

一个android程序建立过程包括:建立工程、编码、编译、打包、签名、运行。

 

 

android工程:

src/
包含 Activity 文件, 存放在  src/your/package/namespace/ActivityName.java. 所有源代码文件 (例如 .java or .aidl files)

bin/

编译输出目录。在这里你可以找到打包文件(.apk)和编译后的资源。
jni/
包含用adroid ndk开发的本地代码。

gen/

包含由ADT工具(aapt、aidl)产生的java文件。例如: R.java 和从AIDL建立的接口文件。

assets/

This is empty. You can use it to store raw asset files. Files that you save here are    compiled into an .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/
      包含应用程序的资源,例如: drawable files, layout files, and string values.
anim/
For XML files that are compiled into animation objects. See the Animation resource        type.
color/
For XML files that describe colors. See the Color Values resource        type.
drawable/
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.
layout/
XML files that are compiled into screen layouts (or part of a screen). See the Layout resource type.
menu/
For XML files that define application menus.        See the Menus        resource type.
raw/
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 the R class. For example, this is a good place for media, such as MP3        or Ogg files.
values/
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 the R class.
xml/
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.
libs/
包含私有库
AndroidManifest.xml
The control file that describes the nature of the application and each of its components.    For instance, it describes: certain qualities about the activities, services, intent receivers,    and content providers; what permissions are requested; what external libraries are needed; what    device features are required, what API Levels are supported or required; and others. See the     AndroidManifest.xml    documentation for more information
project.properties
This file contains project settings, such as the build target. This file is integral to    the project, so maintain it in a source revision control system. To edit project    properties in Eclipse, right-click the project folder and select     Properties.
local.properties
Customizable computer-specific properties for the build system. If you use Ant to build    the project, this contains the path to the SDK installation. Because the content of the file    is specific to the local installation of the SDK, the local.properties should not be maintained in a source revision control system. If you use Eclipse, this file is not used.
ant.properties
Customizable properties for the build system. You can edit this file to override default    build settings used by Ant and also provide the location of your keystore and key alias so that    the build tools can sign your application when building in release mode. This file is integral    to the project, so maintain it in a source revision control system. If you use Eclipse, this    file is not used.
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建立工具(apkbuilder):把所有不需要编译的资源(例如:图片),编译的资源和Dalvik代码(.dex)打包成 .apk 文件.sdk3.0后这个工具删掉了。
  • 签名(jarsigner).apk 建立后,它必须被签名才能安装到设备上,否则设备会拒绝安装。因为编译工具包含了调试签名的私钥,所以在编译时就直接签名了。但是你要发行版本时,你必须自己对apk包进行签名。
  • 对齐工具(zipalign):最后,还需要用zipalign工具对包进行对齐。

1. aapt命令, 生成R文件
   aapt package -f -m -J ./gen -S res -M AndroidManifest.xml -I D:\android.jar  

-f 如果编译生成的文件已经存在,强制覆盖。
-m 使生成的包的目录存放在-J参数指定的目录
-J 指定生成的R.java 的输出目录路径
-S 指定res文件夹的路径
-I 指定某个版本平台的android.jar文件的路径
-A 指定assert文件夹的路径



2. javac命令,生成class文件
 javac -target 1.5 -bootclasspath D:\android-sdk-windows\platforms\android-8\android.jar -d bin src\demo\project\*.java gen\demo\project\R.java  
  -target <版本>               生成特定 VM 版本的类文件
  -bootclasspath <路径>        覆盖引导类文件的位置
  -d <目录>                    指定存放生成的类文件的位置
  -sourcepath <路径>           指定查找输入源文件的位置



3. dx 命令,把class文件转换为.dex文件
 dx --dex --output=D:\ProjectDemo\bin\classes.dex D:\ProjectDemo\bin  
--output=<要生成的classes.dex路径> <要处理的class文件的路径>



4. aapt命令,打包资源
 aapt package -f -M AndroidManifest.xml -S res -I D:\android-sdk-windows\platforms\android-8\android.jar -F bin\resources.ap_  

-f 如果编译生成的文件已经存在,强制覆盖
-M 指定AndroidManifest.xml的路径
-S 指定res文件夹路径
-I 指定某个版本平台的android.jar的路径
-F 指定输出文件完整路径



5. apkbuilder命令,生成apk
apkbuilder D:\ProjectDemo\bin\projectdemo.apk -v -u -z D:\ProjectDemo\bin\resources.ap_ -f D:\ProjectDemo\bin\classes.dex -rf D:\ProjectDemo\src  

-v Verbose 显示过程信息
-u 创建一个无签名的包
-z 指定apk资源路径
-f 指定dex文件路径
-rf 指定源码路径



6. 创建密钥
keytool -genkey -alias release -keyalg RSA -validity 20000 -keystore release.keystore  

-genkey      在用户主目录中创建一个默认文件".keystore",还会产生一个mykey的别名,mykey中包含用户的公钥、私钥和证书
-alias       产生别名
-keyalg      指定密钥的算法 
-validity    指定创建的证书有效期多少天
-keystore    指定密钥库的名称(产生的各类信息将不在.keystore文件中)



7. 签名
jarsigner  -verbose -keystore C:\Users\UserName\Desktop\build\release.keystore -storepass antPassword -keypass antPassword -signedjar D:\ProjectDemo\bin\projectdemo-signed.apk D:\ProjectDemo\bin\projectdemo.apk release  

-verbose  签名/验证时输出详细信息
-keystore 密钥库位置
-storepass          用于密钥库完整性的口令
-keypass            专用密钥的口令(如果不同)
-signedjar          已签名的 JAR 文件的名称 (第一个apk是签名之后的文件, 第二个apk是需要签名的文件)




签名:

签名分两个部分:

  1. 生成私钥

    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

  2. 用私钥进行签名

    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

 

参考:


使用ANT打包Android应用 
Android 自动编译、打包生成apk文件 1 - 命令行方式        
apkbuilder找不到了!!!

android的编译过程

apk包签名

对齐工具(zipalign)




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值