简体   繁体   English

在Java中以编程方式编辑APK文件

[英]Programmatically editing apk files in java

This question gets a little complicated so please bear with me: I have an apk file I am attempting to modify programmatically after installation. 这个问题有点复杂,所以请忍受:我有一个apk文件,我试图在安装后以编程方式进行修改。 I am editing it with another app. 我正在用另一个应用程序对其进行编辑。 I first attempted to do this with the built in java classes for zip file management but when i attempt to completely copy all the files except the file i want to modify and change the file i want to modify the app will not run anymore. 我首先尝试使用内置的Java类对zip文件进行管理,但是当我尝试完全复制除要修改的文件以外的所有文件并更改要修改的文件时,我将不再运行。 I assume this is because of signing or something but when I do the same thing with winRAR (unzip all files, create a new zip archive, copy all files into new zip archive with normal compression) and ssh the new apk into my /data/app folder, it works fine. 我认为这是由于签名或其他原因造成的,但是当我用winRAR做同样的事情时(解压缩所有文件,创建新的zip归档文件,将所有文件以正常压缩方式复制到新的zip归档文件中),然后将新的apk ssh到我的/ data /应用程序文件夹,它工作正常。 My question is why this is happening. 我的问题是为什么会这样。 I think it may have to do with zip headers or something like that does anyone have any ideas? 我认为这可能与zip标头有关,或者有人有任何想法吗?

PS: this is an example of the java process i am using to re-write the zip file: PS:这是我用来重写zip文件的Java流程的示例:

p = Runtime.getRuntime().exec("su");
ZipInputStream in = new ZipInputStream(new FileInputStream("This is the apk containing the asset I want to use to replace the other asset"));
ZipEntry e;
while((e = in.getNextEntry()) != null)
{
    if(e.getName().equals("asset i want to use to replace the other asset"))
        break;
}
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("the output zip file"));
ZipInputStream original = new ZipInputStream(new FileInputStream("the apk i am trying to modify"));
ZipEntry f;
byte[] buf = new byte[1024];
while((f = original.getNextEntry()) != null)
{
    if(!f.getName().equals("the asset i want to replace"))
    {
        out.putNextEntry(new ZipEntry(f.getName()));
        int len;
        while ((len = original.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        original.closeEntry();
        out.closeEntry();
    }
    else
    {
        out.putNextEntry(new ZipEntry(e.getName()));
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
    }
}
in.close();
out.close();
original.close();

I have run this process on the android system and on windows using regular software, i can open the output in winRAR and it appears to be the same except for the asset file i changed. 我已经在android系统和使用常规软件的Windows上运行了此过程,我可以在winRAR中打开输出,除了我更改的资产文件外,它似乎相同。

The only one thing you should know is how works signing apk tool (work around check sums - they are zip entries. A target folder is META-INF with CERT.RSA - there's present encoded checksum and else debug info, CERT.SF and MANIFEST.MF - with SHA1-Digest - Base64 encoded hashes (they are easy to detect by yourself)). 您应该知道的唯一一件事是apk工具签名的工作方式(解决校验和-它们是zip条目。目标文件夹是CERT.RSA的META-INF-存在已编码的校验和,以及调试信息,CERT.SF和MANIFEST .MF-具有SHA1-Digest-Base64编码的哈希(它们很容易由您自己检测)。 But this is a real hacking! 但这是一个真正的黑客! That code can be useful (and used by many "smart programmers" for dinamically changing of picture of application for ex.) and can be dangerous - because your apk file can be increased to GB sizes - if you'll write so. 该代码可能是有用的(例如,许多“聪明的程序员”使用它来动态更改应用程序的图片。)并且可能很危险-因为您可以将apk文件增大为GB大小。

This trick shouldn't be spreaded between world-wide comunity people and I think - it will be kept in short circles of programmers. 我认为,这种技巧不应该在全世界的社区人士之间传播,我会把它保留在简短的程序员圈子中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM