How to Implement OEM in Android

Source: Internet
Author: User
How to generate 100 or more apps based on a basic Android app a few days ago requires that the app icon and app name be different (there may be configuration files ). This is a bit similar to attaching your own tag to the app, but the specific features are provided by others, a bit similar to OEM, next, let's analyze how to implement it. In fact, this is the APK compilation and decompilation application, and add a signature (it cannot be used without signature ). It's just code implementation. Preparations 1. Configure the Java Development Environment 2. Download the APK compilation and decompilation tools provided by Google (including apktool.jarw.apktool.batw.aapt.exe) 3. Download the signature tool provided by Google (including sign. bat, signapk. jar files) icon overwrite and strings file modification we all know that the application icon and Application name in Android applications are in androidmanifest. the Application name specified in XML may be directly written to death, but most of them are in this case.
             android:icon ="@drawable/ic_launcher"            android:label ="@string/app_name"

We only need to overwrite the icon image of the corresponding name under drawable-* and modify strings under the values-* path. the attribute value corresponding to the name in XML is enough. For the sake of simplicity, the drawable-hdpi and values-ZH-RCN paths are introduced here.

Androidmanifest. XML parsing through the above introduction, we need to get the values of the icon and label attributes from androidmanifest. xml. Below is a simple parsing class, note that there are annotations

/*** @ Author tibib ***/public class androidmanifestparser {Public String NS = "http://schemas.android.com/apk/res/android"; Public appinfo parse (inputstream in) throws exception {try {// Use pull to parse the library xmlpullparser parser = xmlpullparserfactory. newinstance (). newpullparser (); NS = parser. getnamespace (); // set the parser using the namespaces feature. setfeature (xmlpullparser. feature_process_namespaces, true); parser. Setinput (in, "UTF-8"); parser. nexttag (); Return readappinfo (parser);} catch (exception e) {e. printstacktrace (); throw E;} finally {In. close () ;}} private appinfo readappinfo (xmlpullparser parser) throws exception {appinfo = new appinfo (); While (parser. next ()! = Xmlpullparser. end_tag) {If (parser. geteventtype ()! = Xmlpullparser. start_tag) {continue;} string name = parser. getname (); // starts by looking for the general Tag if ("application ". equals (name) {string attrlabelvalue = parser. getattributevalue (NS, "label"); string attriconvalue = parser. getattributevalue (NS, "icon"); appinfo. setappname (attrlabelvalue. split ("/") [1]); appinfo. seticonname (attriconvalue. split ("/") [1]);} else {SKIP (PARSE R) ;}} return appinfo;} // skips tags the parser isn' t interested in. uses depth to handle nested tags. i. E ., // if the next tag after a start_tag isn't a matching end_tag, it keeps going until it // finds the matching end_tag (as indicated by the value of "depth" being 0 ). private void SKIP (xmlpullparser parser) throws xmlpullparserexception, ioexception {If (parser. geteventtype ()! = Xmlpullparser. start_tag) {Throw new illegalstateexception ();} int depth = 1; while (depth! = 0) {Switch (parser. Next () {Case xmlpullparser. end_tag: depth --; break; Case xmlpullparser. start_tag: Depth ++; break ;}}}}

Modify the value of the name attribute in strings. XML to app_name (the specific name depends on the configuration ).

/*** @ Author tibib ***/public class xmlmodifyutil {/*** use the JDOM library */public static void modifyxml (File modifyxmlfile, string appnameattrvalue, string appnametext) {outputstreamwriter Bos = NULL; try {saxbuilder builder = new saxbuilder (); If (modifyxmlfile. exists () {document = (document) builder. build (modifyxmlfile); element root = document. getrootelement (); List <element> stringch Ildlist = root. getchildren ("string"); For (element: stringchildlist) {string nameattrvalue = element. getattribute ("name "). getvalue (); If (nameattrvalue. equals (appnameattrvalue) {element. settext (appnametext) ;}} string xmlfiledata = new xmloutputter (). outputstring (document); // strings. XML defaults to UTF-8 format Bos = new outputstreamwriter (New fileoutputstream (modifyxmlfile), "UTF-8"); Bos. WR ITE (xmlfiledata); Bos. flush ();} else {system. out. println ("file does not exist") ;}} catch (exception ex) {ex. printstacktrace ();} finally {If (Bos! = NULL) {try {Bos. Close ();} catch (ioexception e) {e. printstacktrace ();}}}}}

Execute compilation and signature commands
I put the decompilation and signature tools in the same directory and decomcompiled the basic APK in advance. Now I only need to use code to execute the compilation and signature commands. In Java, doscommands can be executed through the runtime class.

Private Static void createapk (string apkname) throws ioexception, interruptedexception {file dir = new file (wppath); // compile the command, azbz is the decompiled folder string backcommand = "CMD/C apktool. bat B azbz "+ apkname + ". APK "; // SIGNATURE command string signcommand =" CMD/C Java-jar signapk. jar platform. x509.pem platform. pk8 "+ apkname + ". APK "+ apkname +" _signed.apk "; // After the command is executed, an unsigned APK runtime backr = Runtime is generated. getruntime (); process backp = backr.exe C (backcommand, null, DIR); // wait until execution is complete and then execute backp. waitfor (); // signature APK. Here, the Google-provided certificate runtime signr = Runtime is used. getruntime (); process signp = signr.exe C (signcommand, null, DIR); signp. waitfor ();}

The following example shows how to generate an APK with two icons and different names.

Public class execdoscommand {static string wppath_app = "E:" + file. separator + "decode APK" + file. separator + "azbz" + file. separator; static string iconpath = wppath_app + "res" + file. separator + "drawable-hdpi" + file. separator; static string stringpath = wppath_app + "res" + file. separator + "values-ZH-RCN" + file. separator + "strings. XML "; static string manifestpath = wppath_app +" androidmanifest. X ML "; static string wppath =" E: "+ file. separator + "decode APK" + file. separator; public static void main (string [] ARGs) throws exception {androidmanifestparser parser = new androidmanifestparser (); appinfo = parser. parse (New fileinputstream (manifestpath); For (INT I = 0; I <2; I ++) {covericon (appinfo, I); modifyappname (appinfo, I ); createapk ("modify" + (I + 1);} Private Static void Mo Difyappname (appinfo, int I) {xmlmodifyutil. modifyxml (new file (stringpath), appinfo. getappname (), "modify" + (I + 1);} Private Static void covericon (appinfo, int I) throws filenotfoundexception, ioexception {bufferedoutputstream Bos = new bufferedoutputstream (New fileoutputstream (iconpath + appinfo. geticonname () + ". PNG "); bufferedinputstream Bis = new bufferedinputstream (New File Inputstream (wppath + file. separator + "image" + file. separator + "icon" + (I + 1) + ". PNG "); byte [] buffer = new byte [1024]; int temp = 0; while (temp = bis. read (buffer ))! =-1) {Bos. write (buffer, 0, temp);} Bos. flush (); Bos. close (); bis. close ();} Private Static void createapk (string apkname) throws ioexception, interruptedexception {file dir = new file (wppath); // compile the string backcommand = "CMD/C apktool. bat B azbz "+ apkname + ". APK "; // SIGNATURE command string signcommand =" CMD/C Java-jar signapk. jar platform. x509.pem platform. pk8 "+ apkname + ". APK "+ apkname +" _signed.apk "; // After the command is executed, an unsigned APK runtime backr = Runtime is generated. getruntime (); process backp = backr.exe C (backcommand, null, DIR); // wait until execution is complete and then execute backp. waitfor (); // signature APK. Here, the Google-provided certificate runtime signr = Runtime is used. getruntime (); process signp = signr.exe C (signcommand, null, DIR); signp. waitfor ();}}
Related Article

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.