Using powershell scripts to implement andoird compilation and signature automation for different markets, greatly improving the efficiency

Source: Internet
Author: User
Tags sha1
Document directory
  • 1. Define parameters
  • 2. Modify androidmanifest. xml
  • 3. Call build. BAT to compile the project
  • 4. Sign the compiled Application
  • 5. zipalign optimized the named APK
  • 6. Batch Processing calls powershell scripts for automatic compilation and signature
  • 7. Complete script download
Preface

Shortly after I wrote Android, I found that more people use an Android app, not only depends on the app itself, but also on the promotion of the app. In China, there are at least 10 marketing needs: 360 open platform, alime bar, Google electronic market, anzhi market, App Exchange, youyi market, Macheng, Android Market, N +, and Android stars.

To measure the effect of each market, I used umeng's distribution channel analysis interface, just for different markets in androidmanifest. define different meta-data umeng_channel values in XML, and then distribute their respective APK values to each market.

Now the problem arises. If my application is updated and there is no automated method, I need to use eclipse to manually export and sign ...... ......, It is very error-prone and time-consuming.

So it took me one day to use powershell scripts to automate the above operations. It feels great to share with you and hope to help you.

Before reading this article, I assume that you already know how to sign Android apps and understand Apache ant. You have integrated umeng's distribution channel analysis interface to learn about powershell.

Implementation

  1. Use powershell to implement a method: Use ant to automatically compile the project. Before each compilation, modify the value of meta-data umeng_channel in androidmanifest. XML to the ID of the specified market.
  2. Store all the market identifiers in a string array, traverse the array, and pass the element into the above method when traversing each element, so that compilation can be performed for different markets, after compilation, you can sign the compiled application.
System Environment
  • Windows 7 untimate
  • JDK 7 update1, assuming the path is: e: \ Program Files \ Java \ jdk1.7.0 _ 01
  • Apache-ant-1.8.2, assuming the path is: e: \ Program Files \ apache-ant-1.8.2
  • Android SDK, assuming the path is: e: \ Program Files \ Android-SDK
Automatically compile a project with ant
  • Assume that the android project path is D: \ workspace \ MyApp.
  • Check whether the file D: \ workspace \ MyApp \ build. xml exists. This is the configuration file used to compile the project using ant.
    • If not, run the following command in the Command window:
      • Cd d: \ workspace \ MyApp
      • D:
      • "E: \ Program Files \ Android-SDK \ tools \ android" Update Project -- path.
    • If a build exists or is generated through the above steps. in the XML file, modify <project name = "XXX" default = "help"> XXX as the name of your project (for details about how to change the project name, see the update at the end of this Article), for example: <project name = "MyApp" default = "help">
  • Create a new batch file D: \ workspace \ MyApp \ build. bat. Use ant to automatically compile the android project. The file content is as follows. Modify the path according to your project and system.
1 Set java_home = E: \ Program Files \ Java \ jdk1.7.0 _ 01
2 cd d: \ workspace \ MyApp
3 D:
4 "E: \ Program Files \ apache-ant-1.8.2 \ bin \ ant" Release

If the system environment is correct, you can view the compiled myapp-release-unsigned.apk in the Directory D: \ workspace \ MyApp \ bin after the build. Bat command is executed.

Note: If a third-party jar package library is referenced in the project, place the class libraries in the "Libs" directory of the Project root directory. Otherwise, ant will make an error during compilation.

Process description 1. Define parameter 1 param ($ projectname, $ projectrootdirectory, $ keystorepath, $ storepass, $ keypass, $ alias)
2
3 $ channels = @ ("360", "aimi8", "anzhi", "appchina", "eoemarket", "gfan", "hiapk", "nduoa ", "starandroid ")
4 $ defachannel channel = "androidmarket"
5 $ jdkpath = "E: \ Program Files \ Java \ jdk1.7.0 _ 01"
6 $ androidsdkpath = "E: \ Program Files \ Android-SDK"
7 $ signedpath = "$ projectrootdirectory \ bin \ signed"
8 $ zipalignedpath = "$ projectrootdirectory \ bin \ zipaligned"
  • $ Projectname: Project name, for example, MyApp
  • $ Projectrootdirectory: Project path, for example, D: \ workspace \ MyApp
  • $ Keystorepath: keystore path, for example, D: \ workspace \ MyApp \ keystore
  • $ Storepass: store password, for example, 123456
  • $ Keypass: key password, for example: 123456
  • $ Alias: keystore alias, for example, MyApp
  • $ Channels: defines all the remaining market IDs except the default market IDs defined in androidmanifest. xml.
  • $ Defaultchannel: the default market ID mentioned above. Why is there a default market ID? Our androidmanifest. the XML file will not be changed frequently, and we do not want to see the file in the modified state after the automatic compilation and signature are completed each time. Therefore, I designed it to compile the file for the default market at the end, in this case, the androidmanifest is modified N times. XML returns to the unmodified status at the end.
  • $ Signedpath: indicates the named APK file storage path.
  • $ Zipalignedpath: indicates the file storage path after zipalign optimization. the last file to be released is the APK file under this directory.
2. Modify androidmanifest. xml1 $ manifestpath = "$ projectrootdirectory \ androidmanifest. xml"
2 $ file = [system. Io. File]: readalltext ($ manifestpath)
3 $ file = [RegEx]: Replace ($ file, '<meta-data \ r? \ N? \ S + Android: Name = "umeng_channel" \ r? \ N? \ S + (. *?)> ',' <Meta-data
4 Android: Name = "umeng_channel"
5 Android: value = "'+ $ channelname +'"/> ', [text. regularexpressions. regexoptions]: singleline)
6 [system. Io. File]: writealltext ($ manifestpath, $ file)

$ Channelname indicates the input market ID for each traversal, for example, anzhi

3. Call build. BAT to compile the project

1 & "$ projectrootdirectory \ build" 4. Sign the compiled Application 1 & "$ jdkpath \ bin \ jarsigner"-keystore $ keystorepath-storepass $ storepass-keypass $ keypass-signedjar" $ signedpath \ your projectname-your channelname.apk "-verbose" $ projectrootdirectory \ bin \ your projectname-release-unsigned.apk "$ alias-digestalg sha1-sigalg md5withrsa
  • Note the last two parameters:-digestalg sha1-sigalg md5withrsa. If your current environment is JDK 7, add these two parameters. Otherwise, the signed application will have an error during installation: failure [install_parse_failed_no_certificates] [1]
  • The signed application is located in the path defined by $ signedpath.
5. zipalign optimize the named apk1 if (test-path "$ zipalignedpath \ export projectname-$channelname.apk "){
2 remove-item "$ zipalignedpath \ your projectname-your channelname.apk"
3}
4
5 & "$ androidsdkpath \ tools \ zipalign"-V 4 "$ signedpath \ export projectname-$channelname.apk" "$ zipalignedpath \ signature" 6. Batch calls powershell scripts for automatic compilation and signature

Assume that the powershell script is stored in: D: \ workspace \ build4differentchannels. ps1, and create a batch processing script: D: \ workspace \ build4differentchannels_myapp.bat:

1 powershell D: \ workspace \ build4differentchannels. PS1 MyApp "D: \ workspace \ MyApp" "D: \ workspace \ MyApp \ keystore" 123456 123456 MyApp

Call the powershell script build4differentchannels. PS1 in batches and pass in the parameters defined previously. When you need to release an application in the future, execute the batch processing and automatically compile and sign the application for each market. This is very convenient.

If an error occurs during script modification:

File d: \ workspace \ build4differentchannels. PS1 cannot be loaded because the execution of scripts is disabled on this system.

Open the command window with the administrator privilege and execute the following command:

1 powershell2 set-executionpolicy remotesigned 7. Complete script download

Build4differentchannels_20111209.zip

Reference

[1]: http://code.google.com/p/android/issues/detail? Id = 19567

[2]: http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.html

Advertisement

Advertisement for two recently developed applications

  • Cat language: "Voice" is changed to "cat language", so that the cat can better understand you; shake, 52 types of cat languages are available at any time; cat jokes, cat fun pictures, and CAT text are updated every day; make fun of cats, raise cats, and understand essential applications of cats
    Application: http://www.appchina.com/soft_detail_179476_0_10.html
    Electronic market: https://market.android.com/details? Id = com. shinboz. Android. human2cat
  • Value observation: read news here
    Application: http://www.appchina.com/soft_detail_179481_0_10.html
    Electronic market: https://market.android.com/details? Id = com. chinavalue. Focus
Update
  • 2011-11-28
    Thanks for reminding wanx: <project name = "XXX" default = "help"> build, then build. the XML section should be changed to: <project name = "ABC" default = "help">
  • 2011-12-02
    Thanks again to wanx's reminder: if there are third-party jar package libraries referenced in the project, put these libraries under the "Libs" directory of the Project root directory. Otherwise, ant will make an error during compilation.
  • 2011-12-07
    Add zipalign optimization script
  • 2011-12-09
    Delete the old APK file before zipalign.

    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.