MSBuild命令行编译Xamarin 项目
Xamarin平台:
- C#language–使用C#语言构建应用程序
- Mono.NETframework–微软的跨平台框架
- Compiler–根据不同平台生成不同产品的编译器
- IDEtools–集成开发环境,包含了创建,构建,部署,编译等。
Compilation
我们先看一下官网的描述:
TheC#sourcemakesitswayintoanativeappinverydifferentwaysoneachplatform:
iOS–C#isahead-of-time(AOT)compiledtoARMassemblylanguage.The.NETframeworkisincluded,withunusedclassesbeingstrippedoutduringlinkingtoreducetheapplicationsize.AppledoesnotallowruntimecodegenerationoniOS,sosomelanguagefeaturesarenotavailable(seeXamarin.iOSLimitations).
Android–C#iscompiledtoILandpackagedwithMonoVM+JIT’ing.Unusedclassesintheframeworkarestrippedoutduringlinking.Theapplicationrunsside-by-sidewithJava/ART(Androidruntime)andinteractswiththenativetypesviaJNI(seeXamarin.AndroidLimitations).
WindowsPhone–C#iscompiledtoILandexecutedbythebuilt-inruntime,anddoesnotrequireXamarintools.DesigningWindowsPhoneapplicationsfollowingXamarin’sguidancemakesitsimplertore-usethecodeoniOSandAndroid.
先学习下Android的编译过程,可以对比下java语言开发的Android应用程序:
java开发的Android应用程序编译执行过程:
Java---(JavaC)---->.class--->JVMloadclass--->main方法执行
Xamarin平台C#应用程序编译执行过程:
C#(.cs文件)---(C#complier)--->IL--->MonoVM+JITexecute
C#complier:
gmcs:compilertotargetthe2.0mscorlib.
smcs:compilertotargetthe2.1mscorlib,tobuildMoonlightapplications.
dmcs:compilertotargetthe4.0mscorlib.
Xamarin编译打包可执行程序:
先看一下官网介绍:
TheXamarin.AndroidbuildprocessisbasedonMSBuild,whichisalsotheprojectfileformatusedbyXamarinStudioandVisualStudio.*OrdinarilyuserswillnotneedtoedittheMSBuildfilesbyhand*-theIDEcreatesfullyfunctionalprojectsandupdatesthemwithanychangesmade,andautomaticallyinvokebuildtargetsasneeded.
AdvancedusersmaywishtodothingsnotsupportedbytheIDE'sGUI,sothebuildprocessiscustomisablebyeditingtheprojectfiledirectly.ThispagedocumentsonlytheXamarin.Android-specificfeaturesandcustomizations-manymorethingsarepossiblewiththenormalMSBuilditems,propertiesandtargets.
Windows使用MSBuild,OSX使用xbuild
Windows编译打包:
1.确保环境已安装好VisualStudio,MonoforAndroidSDK等
2.在Xamarin创建的工程目录里找到后缀为csproj的文件XXX.csproj
3.找到Mono提供的MSBuild.exe
备注:以下是我寻找到的方法:打开XXX.csproj寻找<ImportProject="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets"/>
在文件夹里搜索Xamarin.Android.CSharp.targets搜索到在MSBuild同级目录里找到了C:\ProgramFiles(x86)\MSBuild\14.0\Bin\MSBuild.exe
4.打开命令行,执行命令编译打包:
"C:\ProgramFiles(x86)\MSBuild\14.0\Bin\MSBuild.exe"/t:SignAndroidPackagePath\To\Your\XXX.csproj
打包完成后可以看到工程的\bin\Debug文件夹下找到签名和未签名的apk
如果需要打包Release版本可以加上字段/p:Configuration=Release
参考链接:
Xamaringuide
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