Skip to content

Commit

Permalink
feat(cli): add apksigner as a build option (#6442)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Anderson <[email protected]>
  • Loading branch information
yooouuri and markemer authored Jun 26, 2023
1 parent d7856de commit 9818a76
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 14 deletions.
78 changes: 67 additions & 11 deletions cli/src/android/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ export async function buildAndroid(
: `assemble${flavor}Release`;
const gradleArgs = [arg];

if (
!buildOptions.keystorepath ||
!buildOptions.keystorealias ||
!buildOptions.keystorealiaspass ||
!buildOptions.keystorepass
) {
throw 'Missing options. Please supply all options for android signing. (Keystore Path, Keystore Password, Keystore Key Alias, Keystore Key Password)';
}

try {
await runTask('Running Gradle build', async () =>
runCommand('./gradlew', gradleArgs, {
Expand Down Expand Up @@ -63,6 +54,73 @@ export async function buildAndroid(
`-release-signed.${releaseType.toLowerCase()}`,
);

if (buildOptions.signingtype == 'jarsigner') {
await signWithJarSigner(
config,
buildOptions,
releasePath,
signedReleaseName,
unsignedReleaseName,
);
} else {
await signWithApkSigner(
config,
buildOptions,
releasePath,
signedReleaseName,
unsignedReleaseName,
);
}

logSuccess(`Successfully generated ${signedReleaseName} at: ${releasePath}`);
}

async function signWithApkSigner(
config: Config,
buildOptions: BuildCommandOptions,
releasePath: string,
signedReleaseName: string,
unsignedReleaseName: string,
) {
if (!buildOptions.keystorepath || !buildOptions.keystorepass) {
throw 'Missing options. Please supply all options for android signing. (Keystore Path, Keystore Password)';
}

const signingArgs = [
'sign',
'--ks',
buildOptions.keystorepath,
'--ks-pass',
`pass:${buildOptions.keystorepass}`,
'--in',
`${join(releasePath, unsignedReleaseName)}`,
'--out',
`${join(releasePath, signedReleaseName)}`,
];

await runTask('Signing Release', async () => {
await runCommand('apksigner', signingArgs, {
cwd: config.android.platformDirAbs,
});
});
}

async function signWithJarSigner(
config: Config,
buildOptions: BuildCommandOptions,
releasePath: string,
signedReleaseName: string,
unsignedReleaseName: string,
) {
if (
!buildOptions.keystorepath ||
!buildOptions.keystorealias ||
!buildOptions.keystorealiaspass ||
!buildOptions.keystorepass
) {
throw 'Missing options. Please supply all options for android signing. (Keystore Path, Keystore Password, Keystore Key Alias, Keystore Key Password)';
}

const signingArgs = [
'-sigalg',
'SHA1withRSA',
Expand All @@ -85,6 +143,4 @@ export async function buildAndroid(
cwd: config.android.platformDirAbs,
});
});

logSuccess(`Successfully generated ${signedReleaseName} at: ${releasePath}`);
}
3 changes: 0 additions & 3 deletions cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,6 @@ async function loadAndroidConfig(
const buildOptions = {
keystorePath: extConfig.android?.buildOptions?.keystorePath,
keystorePassword: extConfig.android?.buildOptions?.keystorePassword,
keystoreAlias: extConfig.android?.buildOptions?.keystoreAlias,

This comment has been minimized.

Copy link
@trinitiwowka

trinitiwowka Jul 17, 2023

Why????

This comment has been minimized.

Copy link
@trinitiwowka

trinitiwowka Jul 17, 2023

It's mistake

keystoreAliasPassword:
extConfig.android?.buildOptions?.keystoreAliasPassword,
releaseType: extConfig.android?.buildOptions?.releaseType,
};

Expand Down
8 changes: 8 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ export interface CapacitorConfig {
* @default "AAB"
*/
releaseType?: 'AAB' | 'APK';

/**
* Program to sign your build with
*
* @since 5.1.0
* @default "jarsigner"
*/
signingType?: 'apksigner' | 'jarsigner';
};

/**
Expand Down
1 change: 1 addition & 0 deletions cli/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export interface AndroidConfig extends PlatformConfig {
keystoreAlias?: string;
keystoreAliasPassword?: string;
releaseType?: 'AAB' | 'APK';
signingType?: 'apksigner' | 'jarsigner';
};
}

Expand Down
8 changes: 8 additions & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ export function runProgram(config: Config): void {
'Android release type; APK or AAB',
).choices(['AAB', 'APK']),
)
.addOption(
new Option(
'--signing-type <signingtype>',
'Program used to sign apps (default: jarsigner)',
).choices(['apksigner', 'jarsigner']),
)
.action(
wrapAction(
telemetryAction(
Expand All @@ -168,6 +174,7 @@ export function runProgram(config: Config): void {
keystorealias,
keystorealiaspass,
androidreleasetype,
signingtype,
},
) => {
const { buildCommand } = await import('./tasks/build');
Expand All @@ -178,6 +185,7 @@ export function runProgram(config: Config): void {
keystorealias,
keystorealiaspass,
androidreleasetype,
signingtype,
});
},
),
Expand Down
5 changes: 5 additions & 0 deletions cli/src/tasks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface BuildCommandOptions {
keystorealias?: string;
keystorealiaspass?: string;
androidreleasetype?: 'AAB' | 'APK';
signingtype?: 'apksigner' | 'jarsigner';
}

export async function buildCommand(
Expand Down Expand Up @@ -46,6 +47,10 @@ export async function buildCommand(
buildOptions.androidreleasetype ||
config.android.buildOptions.releaseType ||
'AAB',
signingtype:
buildOptions.signingtype ||
config.android.buildOptions.signingType ||
'jarsigner',
};

try {
Expand Down

0 comments on commit 9818a76

Please sign in to comment.