Project Solution: Unzip App-write file-compress App-jarsigner re-sign App-install app

Source: Internet
Author: User
Tags cos crc32 unique id
<span id="Label3"></p><p><p><span style="font-size:14px">Remember the last time in a project in Nanchang ZTE encountered a such demand: an app can give multiple channels to operate, the channel to promote out to get the corresponding amount of Promotion. In this case, it is necessary to make the app uniquely flag a channel Trader. At that time our solution in this project is: let the user in the app manually fill in the channel Operator's work number, I now think this way is also drunk, I do not know how the project manager at that time think, unexpectedly will give such a Plan.</span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"></span>This project also encountered this problem: the need for the app can be extended to a number of channel vendors, the channel can get the promotion Amount. This time I proposed the solution Is: first unpack the app, and then in the assets directory to write to the channel Provider's unique id, and then compress the app, compress the re-signed app, after the Gaocheng. Users in the first time to enter the app, will be assets in the ID read out, submitted to the server, the perfect solution to this user is the promotion of this channel to get Users.</span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"></span>First Step: unzip the app, delete the Cert.rsa and cert.sf two files in the Meta-inf folder</span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"></span>Second Step: read the extracted assets directory in the ID.txt file, write to the channel vendor ID</span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"><span style= "white-space:pre" ></span>file file = new file ("d:/app/assets/id.txt"); outputstream OutputStream = new FileOutputStream (file), outputstream.write (user.getid (). toString (). getBytes ()); o Utputstream.flush (); Outputstream.close ();</pre></pre><span style="white-space:pre"><span style="white-space:pre"> </span></span>Step three: compress all app files after writing to the channel quotient ID<p><p></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"><span style= "white-space:pre" ></span>zipcompressor zc = new zipcompressor ("d:/play.apk"); Zc.compressexe ("d:/app/");</pre></pre><span style="white-space:pre"><span style="white-space:pre"> </span></span>The specific compression code is as Follows:<p><p></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"></span></span></p></p><pre name="code" class="java">Package Com.xyc.signsystem.utils;import Java.io.bufferedinputstream;import Java.io.file;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.util.zip.crc32;import Java.util.zip.checkedoutputstream;import Org.apache.tools.zip.zipentry;import org.apache.tools.zip.zipoutputstream;/** * @ClassName: zipcompressor * @author: andywuchuanlong qq:312037487 * @Descrip Tion: a common tool class for compressing files-implemented with org.apache.tools.zip.ZipOutputStream, is more Complex. * */public class zipcompressor {static final int BUFFER = 8192;private file zipfile;/** * Compressed Files constructor * * @param pathName * Compressed file storage directory */public zipcompressor (String pathName) {zipfile = new file (pathName);} /** * Perform compression operation * * @param srcpathname * Compressed file/folder */public void Compressexe (String srcpathname) {file file = NE W File (srcpathname), if (!file.exists ()) {throw new runtimeexception (srcpathname + "does not exist!) ");} Try {fileoutputstream fileoutputstream = new FileOutputStream (zipfile); Checkedoutputstream cos = new Checkedoutputstream (fileoUtputstream,new CRC32 ()); Zipoutputstream out = new Zipoutputstream (cos); String basedir = ""; compressbytype (file, out, basedir); out.close ();} Catch (Exception e) {e.printstacktrace (); throw new RuntimeException (e);}} /** * Determines whether a directory or a file, depending on the type (file/folder) to perform a different compression method * * @param file * @param out * @param basedir */private void Compressbytype file fi le, zipoutputstream out, String basedir) {if (basedir.equals ("play/")) {basedir = "";} /* determine whether the directory or file */if (file.isdirectory ()) {this.compressdirectory (file, out, basedir), or else {this.compressfile (file, out, basedir);}} Boolean IsFirst = true;/** * Compresses a directory * * @param dir * @param out * @param basedir */private void compressdirectory (File di r, zipoutputstream out, String basedir) {if (!dir.exists ()) {return;} If (basedir.equals ("play/")) {basedir = "";} file[] files = dir.listfiles (); for (int i = 0; i < files.length; i++) {/* recursive */compressbytype (files[i], out, Basedir + Dir.getname () + "/");}} /** * Compress a file * * @param file * @param out * @param basedir */private void compressfile (file file, zipoutputstream out, String basedir) {if (!file.exists ()) {isFirst = false;return;} If (basedir.equals ("play/")) {basedir = "";} Try {bufferedinputstream bis = new Bufferedinputstream (new fileinputstream (file)); ZipEntry entry = new ZipEntry (basedir + file.getname ()); out.putnextentry (entry); int count;byte data[] = new byte[buffer]; While ((count = Bis.read (data, 0, BUFFER))! =-1) {out.write (data, 0, count);} Bis.close ();} Catch (Exception e) {throw new RuntimeException (e);}}}</pre><br><p><p></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><span style="white-space:pre"></span>Fourth step: after the compression is complete, the package is not signed at this time, so also need to sign, signature can use the Jarsigner tool, first we have to find the Java installation directory</span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><span style="white-space:pre"></span></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"><span style= "white-space:pre" ></span>public string Getjavapath () {string Javapath = (string) System.getenv ("Path"); String paths[]= Javapath.split (";"); String myPath = null;for (string path:paths) {if (path.contains ("Java") &&!path.contains ("jre") && Path.contains ("bin")) {myPath = path;break;}} return mypath+ "\";}</pre></pre><span style="white-space:pre"><span style="white-space:pre"></span></span><p><p></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><span style="white-space:pre"></span>Signature:</span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><span style="white-space:pre"><span style="white-space:pre"></span></span></span></span></p></p><pre name="code" class="java"><pre name="code" class="java"><span style= "white-space:pre" ></span>string javapath = Getjavapath (); Runtime RT = Runtime.getruntime (); String cmd = javapath+ "jarsigner-verbose" + "-keystore" + keystorepath+ "-storepass Player"//password + "-signedjar" +signed apkpath//signed apk storage location + "-digestalg SHA1 -sigalg md5withrsa" + unsignedapkpath//unsigned apk+ "player";//alias process Child = Rt.exec (cmd);</pre></pre><span style="white-space:pre"><span style="white-space:pre"></span></span><p><p></p></p><p><p><span style="font-size:14px"><span style="white-space:pre">OK, the signature is successful. <span style="white-space:pre"> <span style="white-space:pre"></span> </span></span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><br></span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><br></span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><br></span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><br></span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><br></span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><br></span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><br></span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><br></span></span></p></p><p><p><span style="font-size:14px"><span style="white-space:pre"><br></span></span></p></p><p><p>Project Solution: Unzip App-write file-compress App-jarsigner re-sign App-install app</p></p></span>

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.