/
AndroidApkSigner.cs
153 lines (125 loc) · 4.81 KB
/
AndroidApkSigner.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Xamarin.Android.Tools;
using Microsoft.Android.Build.Tasks;
namespace Xamarin.Android.Tasks
{
public class AndroidApkSigner : JavaToolTask
{
public override string TaskPrefix => "AAS";
[Required]
public string ApkSignerJar { get; set; }
[Required]
public string ApkToSign { get; set; }
[Required]
public ITaskItem ManifestFile { get; set; }
public string KeyStore { get; set; }
public string KeyAlias { get; set; }
public string PlatformKey { get; set; }
public string PlatformCert { get; set; }
/// <summary>
/// The Password for the Key.
/// You can use the raw password here, however if you want to hide your password in logs
/// you can use a preview of env: or file: to point it to an Environment variable or
/// a file.
///
/// env:<PasswordEnvironentVariable>
/// file:<PasswordFile>
/// </summary>
public string KeyPass { get; set; } = string.Empty;
/// <summary>
/// The Password for the Keystore.
/// You can use the raw password here, however if you want to hide your password in logs
/// you can use a preview of env: or file: to point it to an Environment variable or
/// a file.
///
/// env:<PasswordEnvironentVariable>
/// file:<PasswordFile>
/// </summary>
public string StorePass { get; set; } = string.Empty;
public string AdditionalArguments { get; set; }
void AddStorePass (CommandLineBuilder cmd, string cmdLineSwitch, string value)
{
if (value.StartsWith ("env:", StringComparison.Ordinal)) {
cmd.AppendSwitchIfNotNull ($"{cmdLineSwitch} ", value);
}
else if (value.StartsWith ("file:", StringComparison.Ordinal)) {
cmd.AppendSwitchIfNotNull ($"{cmdLineSwitch} file:", value.Replace ("file:", string.Empty));
} else {
cmd.AppendSwitchIfNotNull ($"{cmdLineSwitch} pass:", value);
}
}
protected override string GenerateCommandLineCommands ()
{
var cmd = new CommandLineBuilder ();
var manifest = AndroidAppManifest.Load (ManifestFile.ItemSpec, MonoAndroidHelper.SupportedVersions);
int minSdk = MonoAndroidHelper.SupportedVersions.MinStableVersion.ApiLevel;
int maxSdk = MonoAndroidHelper.SupportedVersions.MaxStableVersion.ApiLevel;
if (manifest.MinSdkVersion.HasValue)
minSdk = manifest.MinSdkVersion.Value;
if (manifest.TargetSdkVersion.HasValue)
maxSdk = manifest.TargetSdkVersion.Value;
minSdk = Math.Min (minSdk, maxSdk);
cmd.AppendSwitchIfNotNull ("-jar ", ApkSignerJar);
cmd.AppendSwitch ("sign");
if (!string.IsNullOrEmpty (PlatformKey) && !string.IsNullOrEmpty (PlatformCert)) {
cmd.AppendSwitchIfNotNull ("--key ", PlatformKey);
cmd.AppendSwitchIfNotNull ("--cert ", PlatformCert);
} else {
cmd.AppendSwitchIfNotNull ("--ks ", KeyStore);
AddStorePass (cmd, "--ks-pass", StorePass);
cmd.AppendSwitchIfNotNull ("--ks-key-alias ", KeyAlias);
AddStorePass (cmd, "--key-pass", KeyPass);
}
cmd.AppendSwitchIfNotNull ("--min-sdk-version ", minSdk.ToString ());
cmd.AppendSwitchIfNotNull ("--max-sdk-version ", maxSdk.ToString ());
if (!string.IsNullOrEmpty (AdditionalArguments))
cmd.AppendSwitch (AdditionalArguments);
cmd.AppendSwitchIfNotNull (" ", Path.GetFullPath (ApkToSign));
return cmd.ToString ();
}
protected override void LogEventsFromTextOutput (string singleLine, MessageImportance importance)
{
singleLine = singleLine.Trim ();
if (singleLine.Length == 0)
return;
if (singleLine.StartsWith ("Warning:", StringComparison.OrdinalIgnoreCase)) {
Log.LogCodedWarning ("ANDAS0000", ApkSignerJar, 0, singleLine);
return;
}
Log.LogMessage (singleLine, importance);
}
protected override string ToolName {
get { return OS.IsWindows ? "java.exe" : "java"; }
}
protected override bool ValidateParameters ()
{
if (!string.IsNullOrEmpty (PlatformKey) && !string.IsNullOrEmpty (PlatformCert)) {
if (!File.Exists (PlatformKey)) {
Log.LogCodedError ("XA4310", Properties.Resources.XA4310, "$(AndroidSigningPlatformKey)", PlatformKey);
return false;
}
if (!File.Exists (PlatformCert)) {
Log.LogCodedError ("XA4310", Properties.Resources.XA4310, "$(AndroidSigningPlatformCert)", PlatformCert);
return false;
}
} else {
if (!string.IsNullOrEmpty (KeyStore) && !File.Exists (KeyStore)) {
Log.LogCodedError ("XA4310", Properties.Resources.XA4310, "$(AndroidSigningKeyStore)", KeyStore);
return false;
}
if (string.IsNullOrEmpty (KeyPass)) {
Log.LogCodedError ("XA4314", Properties.Resources.XA4314, "$(AndroidSigningKeyPass)");
return false;
}
if (string.IsNullOrEmpty (StorePass)) {
Log.LogCodedError ("XA4314", Properties.Resources.XA4314, "$(AndroidSigningStorePass)");
return false;
}
}
return base.ValidateParameters ();
}
}
}