-
Notifications
You must be signed in to change notification settings - Fork 10
/
Program.cs
87 lines (71 loc) · 4.05 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Linq;
//https://docs.microsoft.com/en-us/xamarin/android/deploy-test/building-apps/build-process
namespace APKBuilder
{
class Program
{
static void Main(string[] args)
{
var packageName = "[[your.package.name or new one]]";
int versionCode = 10;
var versionName = "1.2.3";
var msbuild = @"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe";
//var msbuild = @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\msbuild.exe";
var zipalign = @"C:\Android\sdk\build-tools\27.0.3\zipalign.exe";
var jarsigner = @"C:\Program Files\Java\jdk1.8.0_161\bin\jarsigner.exe";
var buildManifest = "Properties/AndroidManifest.xml";
var androidProjectFolder = @"C:\Git\[[Path to project]].Droid";
var androidProject = $"{androidProjectFolder}\\[[Project]].Mobile.Droid.csproj";
var outputPath = @"C:\temp\" + DateTime.Now.ToString("yyyyMMddHHmmss");
var abis = new string[] { "armeabi", "armeabi-v7a", "x86", "arm64-v8a", "x86_64" };
for (int i = 0; i < abis.Length; i++)
{
var abi = abis[i];
//I don't need this ABI. Do you?
if (abi == "armeabi")
continue;
var specificManifest = $"Properties/AndroidManifest.{abi}_{versionCode}.xml";
var binPath = $"{outputPath}/{abi}/bin";
var objPath = $"{outputPath}/{abi}/obj";
var keystorePath = $"\"{androidProjectFolder}/[[KeyStore file name]].keystore\"";
var keystorePassword = "[[KeyStore password]]";
var keystoreKey = "[[KeyStore Key]]";
var xmlFile = XDocument.Load($"{androidProjectFolder}/{buildManifest}");
var mnfst = xmlFile.Elements("manifest").First();
var androidNamespace = mnfst.GetNamespaceOfPrefix("android");
mnfst.Attribute("package").Value = packageName;
mnfst.Attribute(androidNamespace + "versionName").Value = versionName;
mnfst.Attribute(androidNamespace + "versionCode").Value = ((i + 1) * 100000 + versionCode).ToString();
xmlFile.Save($"{androidProjectFolder}/{buildManifest}");
var unsignedApkPath = $"\"{binPath}/{packageName}.apk\"";
var signedApkPath = $"\"{binPath}/{packageName}_signed.apk\"";
var alignedApkPath = $"{binPath}/{packageName}_signed_aligned.{abi}.apk";
var mbuildArgs = $"{androidProject} /t:PackageForAndroid /t:restore /p:AndroidSupportedAbis={abi} /p:Configuration=Release /p:IntermediateOutputPath={objPath}/ /p:OutputPath={binPath}";
var jarsignerArgs = $"-verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore {keystorePath} -storepass {keystorePassword} -signedjar \"{signedApkPath}\" {unsignedApkPath} {keystoreKey}";
var zipalignArgs = $"-f -v 4 {signedApkPath} {alignedApkPath}";
RunProcess(msbuild, mbuildArgs);
Console.WriteLine("Build is done");
RunProcess(jarsigner, jarsignerArgs);
Console.WriteLine("Jarsigner is done");
//This is should be the last step otherwise Google Play Store will not accept the APK
RunProcess(zipalign, zipalignArgs);
Console.WriteLine("Zipalign is done");
File.Copy($"{alignedApkPath}", $"{outputPath}/{Path.GetFileName(alignedApkPath)}", true);
}
Console.WriteLine("Built and signed");
Console.ReadKey();
}
static void RunProcess(string filename, string arguments)
{
var process = new Process();
process.StartInfo.FileName = filename;
process.StartInfo.Arguments = arguments;
process.Start();
process.WaitForExit();
}
}
}