Xamarin.Android 打包Apk

來源:互聯網
上載者:User

標籤:


由於大部分內容來自Xamarin的官網,因此這篇文章定義為翻譯,原文地址: http://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/publishing_an_application/part_1_-_preparing_an_application_for_release/

轉載請註明出處:http://blog.csdn.net/supluo/article/details/43950105

個人搞了個部落格App,平時上個廁所,睡覺前等隨便看兩篇文章,總能有些收穫,希望大家支援!http://blog.csdn.net/supluo/article/details/43489475


一個應用經過編碼測試之後,接下來就是應該打包發布,Xamarin.Android的打包需要經過一下幾個步驟:

1、以Release模式編譯器

        --這一步包括設定一些應用程式的屬性。

2、建立一個私密金鑰

        --Apk是需要進行數字簽證之後才可以在手機上啟動並執行,我們調試的時候,編譯器會預設以一個debug.keystore(通常在C:\Users\{pc name}\.android路徑下)進行簽名;

因此這一步我們需要建立一個屬於我們自己的秘鑰用於對apk進行簽名(此秘鑰不可丟棄,否則後續的升級之類會出現簽名問題)。這一步通常只需要做一次,也就是一個秘鑰是可以重複使用的。

3、對Apk簽名

        --用私密金鑰對Apk進行數位簽章

4、對Apk進行Zipalign操作

        --zipalign其實是Android SDK中包含的一個工具,它能夠對打包的應用程式進行最佳化,使得在運行時互動更為有效。Xamarin.Android會在運行時進行檢查並且不允許沒有經過zipaligned操作的Apk運行。

上面的3、4兩個步驟在Xamarin Studio和Visual Studio上有所不同,下面詳細說明。

一、以Release模式編譯器

    以Release模式編譯器應該處理以下步驟:

    1)、設定運行模式為Release(在編譯器頂部的工具列上面)。

        --這一步是為了防止使用者在裝置上使用ADB或者其他工具進行調試。

    2)、明確程式表徵圖

        --每一個Android程式都應該指定一個應用程式圖示,雖然這個在技術層面上並不是必須的,但是在一些應用市場,比如Goole Play等這些就會要求這個。

    3)、設定版本號碼

        --這一步是為了初始化活著更新版本資訊,這個對於以後程式的更新和確保使用者知道他們安裝的是什麼版本的應用很重要。

    4)、設定Linker

        --Linking是Xamarin.Android特有的步驟,可以通過除去未使用的代碼大大減少最終的APK大小。

    5)、編譯

        --這一步將會把代碼和資源整合到一個APK中用於准本簽名。


在發布的時候,通常我們要設定Application(Debuggable = false);

這裡我們有一個最簡單的方法,將以下代碼加入到AssemblyInfo.cs(項目工程的Properties目錄下)中:

#if DEBUG[assembly: Application(Debuggable=true)]#else[assembly: Application(Debuggable=false)]#endif

二、指定程式表徵圖

在AssemblyInfo.cs中設定屬性:[assembly:Application(Icon="@drawable/ic_launcher")]

Xamarin Studio中可以對著項目右鍵->Project Options ->Android Application 在右邊設定Application icon的值

VS 2012中方法類似,只不過是對著項目右鍵 ->屬性(property )->Android Manifest -> Application Icon

VS 2010好像沒有這個設定,因此需要使用設定屬性的方式。

三、指定版本號碼

這裡有兩個容易混淆的地方versionCode和versionName;

android:versionCode--這是一個整數值代表應用程式的版本。大多數應用程式會以1作為初始版本值,每次發布的時候再遞增這個值。

這個值與android:versionName沒有什麼關係(見下面介紹)。應用和發布服務都不會向使用者展示這個值。

android:versionName--這是一個字串,會在 Google Play等此類的應用市場中向使用者顯示。這個字串不會被Android內部所使用,這可以是任何能夠協助使用者識別安裝在他們裝置上的應用的字串值。


四、用Relase模式編譯器

在用Relase之前,很重要的一步是設定Linker, The linker in Xamarin.Android will use static analysis to determine which assemblies, types, and type members are used or referenced by a Xamarin.Android application. The linker will then discard all the unused assemblies, types, and members that are not used (or referenced). This can result in a significant reduction in the package size. 

上面一段英文就說明了Linker的作用,其實也不是很難,認真看看還是能明白是什麼意思吧。

下面舉了一個Hello World的例子:

Configuration

Xamarin.Android 4.2.5 Size

None

17.4 MB

SDK Assemblies Only

3.0 MB

也就是我們建立一個Hello World工程,如果我們什麼都不設定,編譯的大小為17.4M,但是如果我們設定為SDK Assemnblies Only,APK大小就變為了3M,減少了83%的大小。

在VS 2013中,我們可以通過右鍵項目,選擇屬性進入Linker設定,見


紅色框中包含三個選項:

  • None – This turns off the linker; no linking will be performed.
  • Sdk Assemblies Only – This will only link the assemblies that are required by Xamarin.Android . Other assemblies will not be linked.
  • Sdk and User Assemblies – This will link all assemblies that are required by the application, and not just the ones required by Xamarin.Android.
在Xamarin.Studio中,


上面紅色框也有三個選項,其實與VS2013中的選項意思是相同的


  • Don‘t link – This turns off the linker; no linking will be performed.
  • Link SDK assemblies only – This will only link the assemblies that are required by Xamarin.Android . Other assemblies will not be linked.
  • Link all assemblies – This will link all assemblies that are required by the application, and not just the ones required by Xamarin.Android.
配置Linker可能產生一些副作用,因此設定Linker之後,反覆測試是很重要的,並且這些測試應該在真正的裝置上進行。

配置Linker之後,就可以編譯器了,編譯之後,就可以用之前建立的私密金鑰進行簽名。當然我們也可以在運行之後的發布過程中建立秘鑰進行簽名。

先到這裡,有點工作上的事情要處理,後面再補上。



Xamarin.Android 打包Apk

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.