如何从android mobile以编程方式签署apk文件?

时间:2013-05-29 15:19:49

标签: android apk sign

我在.apk文件中做了一些更改之后我想从android mobile以编程方式签名它是怎么可能的?

1 个答案:

答案 0 :(得分:0)

我是通过使用以下代码获得的:

private void signAPK() {

    try {
        String generatedApkPath = GENERATE_APK_TO_DIRECTORY + apkname + ".apk";

        String signedApkPath = GENERATE_APK_TO_DIRECTORY + apkname + "-signed.apk";
        Log.i("Sign APK", "Going to create signed apk in:" + signedApkPath);

        ZipSigner zipSigner = null;
        zipSigner = new ZipSigner();

        zipSigner.setKeymode("testkey");

        zipSigner.signZip(generatedApkPath, signedApkPath);

        // After signing apk , delete unsigned apk
        new File(generatedApkPath).delete();

        Toast.makeText(getApplicationContext(),
                "'" + apkname + "-signed.apk' is generated at " + GENERATE_APK_TO_DIRECTORY, Toast.LENGTH_LONG)
                .show();

    } catch (Throwable t) {
        Log.e("Signing apk", "Error while signing apk to external directory", t);
        t.printStackTrace();
    }
}