Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signing Apk files programmatically in java

I need to modify preexisting .apk files present in the /data/app folder. After the modifications the signatures change in the Meta-INF folder. So in order to make sure the apk works properly I need to resign them with the correct md5sum.

Is it possible to resign the apks programmatically through java, generating private key and certs through code only?

like image 786
CodeGuru Avatar asked Dec 31 '25 23:12

CodeGuru


1 Answers

I am using Bouncy Castle and this class. Re-sign example:

SignedJar bcApk = new SignedJar(
    new FileOutputStream("out.apk"), signChain, signCert, signKey);
JarFile jsApk = new JarFile(new File("in.apk"));
Enumeration<JarEntry> entries = jsApk.entries();
while (entries.hasMoreElements()) {
    JarEntry entry = entries.nextElement();
    String name = entry.getName();
    if (!entry.isDirectory() && !name.startsWith("META-INF/")) {
        InputStream eis = jsApk.getInputStream(entry);
        bcApk.addFileContents(name, IOUtils.toByteArray(eis));
        eis.close();
    }
}
jsApk.close();
bcApk.close();
like image 134
Mathias M Avatar answered Jan 06 '26 12:01

Mathias M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!