Skip to content

Instantly share code, notes, and snippets.

@MissaouiChedy
Last active May 17, 2017 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MissaouiChedy/ac4772c11993a72fc1ed13d7fc59c69b to your computer and use it in GitHub Desktop.
Save MissaouiChedy/ac4772c11993a72fc1ed13d7fc59c69b to your computer and use it in GitHub Desktop.
Build and Deploy a Xamarin android application to x86 emulator
#############################################################################################################
# Deploy-X86App
#
# Build and Deploy a Xamarin android application to x86 emulator
#
# Chedy Missaoui -- techdominator.com
#
#############################################################################################################
<#
.SYNOPSIS
Build an x86 apk from a Xamarin Android application and deploy it to a running x86 Android emulator
.DESCRIPTION
This utility performs the following steps(utilities uppercased for emphasis):
1 - builds x86 specific apk by using MSBUILD
2 - signs the generated apk by using JARSIGNER
3 - aligns the apk with ZIPALIGN
4 - deploys the aligned apk by using ADB
The uppercased utilities NEEDS TO BE AVAILABLE IN THE PATH and can be found around
the following locations:
- msbuild -> C:\Program Files (x86)\Microsoft Visual Studio\201X\Community\MSBuild\xx.x\Bin
- jarsigner -> C:\Program Files\Java\jdk1.x.x_xxx\bin
- zipalign -> ~\AppData\Local\Android\sdk\build-tools\xx.x.x\
- adb -> ~\AppData\Local\Android\sdk\platform-tools
It is encouraged to place this utility somewhere under the solution folder(under a tools folder for example)
and to customize the default values of the parameters since don't change often.
.PARAMETER DeviceId
(REQUIRED) The Id of the emulator device which can be fetched by an "adb devices"
.PARAMETER ProjectPath
(REQUIRED) The path to the folder containing the actual Xamarin Android project
It is recommended that you provide a default value in the param statement
.PARAMETER ApplicationPackage
(OPTIONAL) The name of the application package as specified in the AndroidManifest.xml file
The utility is able to figure out this parameter automatically by reading AndroidManifest.xml
.PARAMETER KeyStore
(OPTIONAL) The path to the android keystore
By default the utility will use the debug.keystore, you can specify an alternative keystore
when needed via this parameter
.PARAMETER StorePass
(OPTIONAL) The password for the provided keystore
By default the utility will use the debug.keystore's default password
.PARAMETER CsprojFileName
(OPTIONAL) The name of the project's csproj file
The utility is able to derive the name from the ProjectPath parameter
.EXAMPLE
Deploy-X86App emulator-2149 -ProjectPath "src\MyApp"
Builds the application available under "src\MyApp" and deploys it to
the running "emulator-2149" emulator.
.EXAMPLE
Deploy-X86App emulator-2149
After setting a default value for the project directory as follows:
params (
...
[string] $ProjectPath = "MyProjectsLocation\src\MyApp",
...
)
It is no more necessary to provide the project's location
The previous builds the application available under "MyProjectsLocation\src\MyApp" and deploys it to
the running "emulator-2149" emulator.
.LINK
https://developer.xamarin.com/guides/android/advanced_topics/build-abi-specific-apks
http://blog.techdominator.com/article/deploying-xamarin-android-project-x86-emulator.html
#>
param (
[Parameter(Mandatory=$true)] [string] $DeviceId,
[string] $ProjectPath = "",
[string] $ApplicationPackage = "",
[string] $KeyStore = "$env:USERPROFILE\.android\debug.keystore",
[string] $StorePass = "android",
[string] $CsprojFileName = ""
)
function GetCsprojFilename([string] $projectPath)
{
return [System.Linq.Enumerable]::Last($projectPath.Split("\")) + ".csproj"
}
function GetApplicationPackageName()
{
$content = Get-Content "$ProjectPath\Properties\AndroidManifest.xml"
return [Regex]::Match($content, 'package="([-A-Za-z._0-9]*)"').Groups[1].Value
}
if ([String]::IsNullOrEmpty($CsprojFileName))
{
$CsprojFileName = GetCsprojFilename($ProjectPath)
}
if ([String]::IsNullOrEmpty($ApplicationPackage)) {
$ApplicationPackage = GetApplicationPackageName
}
$finalApkName = "$ApplicationPackage.aligned.apk"
Push-Location
cd $ProjectPath
& msbuild /t:Package /p:AndroidSupportedAbis=x86 /p:IntermediateOutputPath=obj/x86/ /p:AndroidManifest=Properties/AndroidManifest.xml /p:OutputPath=bin/x86/ /p:Configuration=Release $CsprojFileName
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore $KeyStore -storepass $Storepass -signedjar "bin\x86\$ApplicationPackage.apk" "bin\x86\$ApplicationPackage.apk" androiddebugkey
& zipalign -f -v 4 "bin\x86\$ApplicationPackage.apk" "bin\x86\$finalApkName"
adb -s $DeviceId install -r "bin\x86\$finalApkName"
Pop-Location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment