Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add apksigner as a build option #6442

Merged
merged 22 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
keystoreAliasPassword:
extConfig.android?.buildOptions?.keystoreAliasPassword,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mistake, return pls))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so I created a pull request to correct it, but still not merged yet. 😞

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