MSBuild命令行编译Xamarin 项目

Xamarin 平台:

  • C# language – 使用C#语言构建应用程序
  • Mono .NET framework – 微软的跨平台框架
  • Compiler – 根据不同平台生成不同产品的编译器
  • IDE tools – 集成开发环境,包含了创建,构建,部署,编译等。


Compilation
我们先看一下官网的描述:

The C# source makes its way into a native app in very different ways on each platform:

iOS – C# is ahead-of-time (AOT) compiled to ARM assembly language. The .NET framework is included, with unused classes being stripped out during linking to reduce the application size. Apple does not allow runtime code generation on iOS, so some language features are not available (see Xamarin.iOS Limitations ).
Android – C# is compiled to IL and packaged with MonoVM + JIT’ing. Unused classes in the framework are stripped out during linking. The application runs side-by-side with Java/ART (Android runtime) and interacts with the native types via JNI (see Xamarin.Android Limitations ).
Windows Phone – C# is compiled to IL and executed by the built-in runtime, and does not require Xamarin tools. Designing Windows Phone applications following Xamarin’s guidance makes it simpler to re-use the code on iOS and Android.

先学习下Android的编译过程,可以对比下java语言开发的Android应用程序:

java开发的Android应用程序编译执行过程:
Java ---(JavaC)----> .class ---> JVM load class ---> main 方法执行

Xamarin平台C#应用程序编译执行过程:
C#(.cs文件) ---(C# complier)---> IL  --->  MonoVM + JIT execute

C# complier:
gmcs: compiler to target the 2.0 mscorlib.
smcs: compiler to target the 2.1 mscorlib, to build Moonlight applications.
dmcs: compiler to target the 4.0 mscorlib.

Xamarin 编译打包可执行程序:

先看一下官网介绍:
The Xamarin.Android build process is based on MSBuild, which is also the project file format used by Xamarin Studio and Visual Studio. * Ordinarily users will not need to edit the MSBuild files by hand* - the IDE creates fully functional projects and updates them with any changes made, and automatically invoke build targets as needed.

Advanced users may wish to do things not supported by the IDE's GUI, so the build process is customisable by editing the project file directly. This page documents only the Xamarin.Android-specific features and customizations - many more things are possible with the normal MSBuild items, properties and targets.

Windows 使用MSBuild, OS X 使用xbuild

Windows编译打包:
1. 确保环境已安装好 Visual Studio, Mono for Android SDK等
2. 在Xamarin创建的工程目录里找到后缀为csproj 的文件 XXX.csproj
3. 找到Mono提供的MSBuild.exe
备注:以下是我寻找到的方法:打开 XXX.csproj 寻找 <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />

在文件夹里搜索 Xamarin.Android.CSharp.targets 搜索到在MSBuild同级目录里找到了 C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe
4. 打开命令行,执行命令编译打包:
"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"  /t:SignAndroidPackage Path\To\Your\XXX.csproj

打包完成后可以看到工程的\bin\Debug 文件夹下找到签名和未签名的apk
如果需要打包Release版本可以加上字段 /p:Configuration=Release

参考链接:
Xamarin guide
https://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/part_1_-_understanding_the_xamarin_mobile_platform/
https://developer.xamarin.com/guides/android/under_the_hood/build_process/

MSBuild 命令:
https://msdn.microsoft.com/en-us/library/ms164311.aspx

你可能感兴趣的:(android,跨平台,Xamarin)