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 theAssetManager. 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 theDrawable
resource type.layout/XML files that are compiled into screen layouts (or part of a screen). See theLayout 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 theR 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 theres/ 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.xml/For miscellaneous XML files that configure application components. For example, an XML file that defines aPreferenceScreen,AppWidgetProviderInfo,
orSearchability Metadata. SeeApplication
Resources for more information about configuring these application components.
libs/包含私有庫AndroidManifest.xmlThe 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 informationproject.propertiesThis 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.propertiesCustomizable 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, thelocal.properties
should not be maintained in a source revision control system. If you use Eclipse, this file is not used.ant.propertiesCustomizable 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 codeapk建立工具:把所有不需要編譯的資源(例如:圖片),編譯的資源和Dalvik代碼(.dex)打包成 .apk 文件簽名(jarsigner).apk 建立後,它必須被簽名才能安裝到設備上,否則設備會拒絕安裝。因為編譯工具包含瞭調試簽名的私鑰,所以在編譯時就直接簽名瞭。但是你要發行版本時,你必須自己對apk包進行簽名。對齊工具(zipalign):最後,還需要用zipalign工具對包進行對齊。

簽名:

簽名分兩個部分:

生成私鑰

keytool:

Keytool 選項 描述
-genkey 產生一個鍵值對(公鑰和私鑰)
-v 允許動作輸出
-alias 鍵的別名。隻有前八位字符有效。
-keyalg 產生鍵的加密算法。支持DSA和RSA。
-keysize 產生鍵的長度。如果不支持,keytool用默認值1024 bits.通常我們用2048 bits 或更長的key。
-dname

專有名稱,描述誰創建的密鑰。該值被用作自簽名證書的頒發者和主題字段。註意你可以不在命令行指定。如果沒有指定keytool會提示你(CN, OU, and so on)。

-keypass

鍵的密碼。

主要為瞭安全起見,如果沒提供,keytool會提示你輸入。

-validity

鍵的有效期,單位:天

Note: A value of 10000 or greater is recommended.

-keystore .keystore 用於存儲私鑰的文件。
-storepass

私鑰存儲文件的密碼。

主要為瞭安全起見,如果沒提供,keytool會提示你輸入。這個密碼不會存儲在你的shell歷史記錄中。

keytool -genkey
-v -keystoreHello.keystore -alias HelloWorld-keyalg
RSA -keysize2048-validity10000

用私鑰進行簽名

jarsigner:

Jarsigner 選項 描述
-keystore .keystore 包含你私鑰的存儲文件
-verbose 顯示輸出動作。
-sigalg 簽名算法,用 SHA1withRSA.
-digestalg 消息摘要算法,用 SHA1.
-storepass

存儲文件的密碼。

主要為瞭安全起見,如果沒提供,jarsigner會提示你輸入。這個密碼不會存儲在你的shell歷史記錄中。

-keypass

私鑰的密碼。

主要為瞭安全起見,如果沒提供,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

參考:

android的編譯過程

apk包簽名

對齊工具(zipalign)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *