> ## Documentation Index
> Fetch the complete documentation index at: https://docs.futurex.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sign APKs with Android APKSigner by using Futurex PKCS #11

> Demonstration of signing Android APKs using APKSigner and Futurex PKCS #11.

This section shows how to sign an APK with APKSigner and provides a demonstration.

## Sign an APK

Find the **apksigner** module at `/Android/sdk/build-tools/<version>/` and perform the following steps to sign an APK with APKSigner:

**Step 1**

If using **Java JDK** version newer than **17**, run the following command:

```shell expandable lines wrap title="Shell" theme={null}
sudo nano /Android/sdk/build-tools/<version>/apksigner
```

and replace `javaOpts=""` with the following line:

```none expandable lines wrap title="None" theme={null}
javaOpts="--add-opens=jdk.crypto.cryptoki/sun.security.pkcs11=ALL-UNNAMED"
```

**Step 2**

To sign an APK with Android APKSigner, run the following command:

> [!NOTE]
>
> Adjust the values for the following arguments to match your environment:
>
> * \--ks-key-alias: The exact on-token alias listed by `keytool -list` in the previous section, such as apksignerdemo-1784141140:apksignerdemo. The CryptoHub mangles the alias into the form `<alias>-<id>:<alias>`, so copy it verbatim from the `keytool -list` output rather than using the short `-alias` value you passed to `keytool -genkeypair`.
>   Note: If there are spaces in the key alias, make sure to wrap the entire alias in double-quotes.
> * \--in: The `.apk` file you want to sign, including the full path to the file if necessary.
> * \--out: The name of the signed `.apk` file, including the full path if necessary.

```shell expandable lines wrap title="Shell" theme={null}
sudo ./apksigner sign --provider-class sun.security.pkcs11.SunPKCS11 --ks NONE --ks-type PKCS11 --ks-key-alias <key-alias> --in <unsigned.apk> --out <signed.apk>

```

When prompted for the `keystore password for signer #1`, enter the identity password configured inside the `<CRYPTO-OPR-PASS>` tag in the `fxpkcs11.cfg` file.

**Step 3**

To verify the signature of the output file, run the following command:

```shell expandable lines wrap title="Shell" theme={null}
sudo ./apksigner verify -verbose <signed.apk> 
```

> [!TIP]
>
> You should see output similar to the following example:
>
> ```shell expandable lines wrap title="Shell" theme={null}
> Verifies
> Verified using v1 scheme (JAR signing): false
> Verified using v2 scheme (APK Signature Scheme v2): true
> Verified using v3 scheme (APK Signature Scheme v3): true
> Verified using v3.1 scheme (APK Signature Scheme v3.1): false
> Verified using v4 scheme (APK Signature Scheme v4): false
> Verified for SourceStamp: false
> Number of signers: 1
> ```

> [!NOTE]
>
> In the preceding example, multiple signatures occurred for different Android signing versions.  To only sign with one signing version type, add the following flags to your command and enable only the version you want to use to sign:
>
> ```shell expandable lines wrap title="Shell" theme={null}
> --v1-signing-enabled <true or false> --v2-signing-enabled <true or false> --v3-signing-enabled <true or false> --v4-signing-enabled <true or false>
> ```

## APKSigner demonstration

The following example demonstrates a signing command with APKSigner:

```shell expandable lines wrap title="Shell" theme={null}
sudo ./apksigner sign --provider-class "sun.security.pkcs11.SunPKCS11" --ks NONE --ks-type PKCS11 --ks-key-alias "apksignerdemo-1784141140:apksignerdemo" --in /root/AndroidStudioProjects/MyApplication/app/build/outputs/apk/debug/app-debug.apk --out /root/AndroidStudioProjects/MyApplication/app/build/outputs/apk/debug/signed-app-debug.apk 
```

The following example demonstrates a verification command with APKSigner:

```shell expandable lines wrap title="Shell" theme={null}
sudo ./apksigner verify -verbose /root/AndroidStudioProjects/MyApplication/app/build/outputs/apk/debug/signed-app-debug.apk 
```

For more information on APKSigner and its functions, refer to the Android Developer Documentation (

[**developer.android.com/tools/apksigner**](https://developer.android.com/tools/apksigner)).
