Java server-side modify APK and repackage the signature

Source: Internet
Author: User

Foreword: Because of the project needs, need to make some changes to the existing app, so spent a day to study this thing, on the internet to look up a lot of information, found that most are simple instructions or similarities, so I hereby share the results, I hope it will be useful to everyone.


  1. Environmental requirements jdk.17 (where to download, needless to say)

  2. Tool Apktool Required

    2.1 This is the Google launch of the tool, just to deal with this situation, because the APK equivalent to a compressed package, can be opened with the compression tool, many people have tried to directly use the compression software to open the APK and modify the save, but finally can not install

    2.2:http://www.softpedia.com/get/programming/debuggers-decompilers-dissasemblers/apktool.shtml

    2.3 Need to download Apktool2.0.2.jar and apktool-install-windows-r05-ibot.tar.bz2

    2.4 Download down will find three files aapt.exe you, Apktool.bat,apktool.jar (renamed the previous Apktool2.0.2.jar to Apktool.jar), and then put the three files under C:\Windows

    2.5 Open Doc window input cmd Enter after input apktool can see the corresponding command

  3. An Android app and a corresponding signature key file (available for Android developers)

    3.1 Put it under a folder E:\apkTest

    3.2 Do not rest assured that you can first put these two files back under

    1. Java Code Section

      Import Java.io.BufferedReader;
      Import Java.io.File;
      Import Java.io.FileInputStream;
      Import Java.io.FileOutputStream;
      Import java.io.IOException;
      Import Java.io.InputStreamReader;
      Import Java.io.OutputStreamWriter;


      /**
      * @author Showlike
      * @version v1.0
      * 2012-6-29 12:05:10
      */
      public class Test {

      public static void Main (String args[]) throws ioexception{


      BufferedReader BR =null;
      OutputStreamWriter OSW =null;
      Process process = NULL;

      try {

      1----Decompression
      Apktool Path
      String Newapppath = "e:\\apktest\\";
      Save path
      String AppPath = "e:\\apktest\\";

      File file =new file (path);
      String cmd_d= "cmd.exe/c apktool D" +apppath+ "Taximeter_1.apk-o" +newapppath+ "app";
      System.out.println (Cmd_d);
      Process = Runtime.getruntime (). exec (Cmd_d);

      if (Process.waitfor ()!=0) System.out.println ("Decompression failed ... ");

      2----Content Modification
      String TargetPath = newapppath+ "App\\assets\\presenter.txt";
      String content= "15926322029";
      System.out.println (TargetPath);
      br = new BufferedReader (new InputStreamReader (New FileInputStream (TargetPath)));
      OSW = new OutputStreamWriter (new FileOutputStream (TargetPath));
      Osw.write (Content,0,content.length ());
      Osw.flush ();

      3----Packaging
      Process = Runtime.getruntime (). EXEC ("cmd.exe/c apktool B" +newapppath+ "App-o" +newapppath+ "app.apk");
      if (Process.waitfor ()!=0) System.out.println ("Package failed ... ");
      4----signature (cannot contain spaces in file name)
      String jdkbinpath= "D:\\Program files\\java\\jdk1.7.0_79\\bin";
      File bin =new file (Jdkbinpath);

      /**

      *e:\\apktest\\taximeter.jks represents a signed key pack

      * 54546645 is the password for the signature package

      *e:\\apktest\\app.apk Modify the original app and repackage it into the app's files

      *E:\\APKTEST\\APPT.APK the name and location of the APK after signing

      **/
      String cmd= "cmd.exe/c jarsigner-verbose-keystore e:\\apktest\\taximeter.jks-storepass 54546645-keypass 54546645-sig Nedjar e:\\apktest\\appt.apk-digestalg sha1-sigalg md5withrsa e:\\apktest\\app.apk taximeter ";
      Process = Runtime.getruntime (). exec (Cmd,null,bin);
      Runtime.getruntime (). Exec execution sometimes blocks, you run but never ends, so add the following function to handle
      Streamgobbler Errorgobbler = new Streamgobbler (Process.geterrorstream (), "ERROR");

      Kick off STDERR
      Errorgobbler.start ();

      Streamgobbler Outgobbler = new Streamgobbler (Process.getinputstream (), "STDOUT");
      Kick off STDOUT
      Outgobbler.start ();
      Process=runtime.getruntime (). exec (Cmdarray);
      if (Process.waitfor ()!=0) System.out.println ("Signature failed ... ");

      }catch (Exception e)
      {
      E.printstacktrace ();
      }finally{

      Br.close ();
      Osw.close ();
      System.out.println ("End ... ");
      }
      }
      }




Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;

/**
* Used to process runtime.getruntime (). Error stream and output stream generated by exec
* @author Ybqin
*
*/
public class Streamgobbler extends Thread {
InputStream is;
String type;
OutputStream OS;

Streamgobbler (InputStream is, String type) {
This (is, type, null);
}

Streamgobbler (InputStream is, String type, outputstream redirect) {
This.is = is;
This.type = type;
This.os = REDIRECT;
}

public void Run () {
try {
InputStreamReader ISR = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
if (OS! = NULL)
PW = new PrintWriter (OS);

ISR = new InputStreamReader (IS);
br = new BufferedReader (ISR);
String Line=null;
while (line = Br.readline ()) = null) {
if (PW = null)
Pw.println (line);
System.out.println (type + ">" + line);
}

if (PW = null)
Pw.flush ();
} catch (IOException IoE) {
Ioe.printstacktrace ();
} finally{
Pw.close ();
Br.close ();
Isr.close ();
}
} catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}

}
}


The above features hope to be useful to everyone, thank you

Java server-side modify APK and repackage the signature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.