Manage IOS and Android project versions with Xcode and Android Studio
Source: Internet
Author: User
<span id="Label3"></p><p><p>Versioning is a common and fundamental issue in the development and operation of mobile applications, and some of the basic concepts of versions often plague our research and development and operations staff. At the same time, manually managing software releases often results in subsequent release and update issues due to CARELESSNESS.</p></p><p><p>Here I have some basic knowledge of IOS and Android versions, and how to get version information in the app and how to manage version numbers automatically using Xcode and Android Studio.</p></p>Version number explanation<p><p>Two version attributes are defined for both IOS and Android:</p></p> <ul> <li><li><p>Ios<br>In the <code>Info.plist</code> definition</p> <ul> <li><p>Cfbundleshortversionstring<br>Explained in Xcode <code>Version</code> , This is the version number that we often say, the general user is visible, usually consists of <code><主版本号>.<次版本号>.<维护号></code> three parts, mainly used to identify different functions of different periods of the Product.</p></li> <li><p>Cfbundleversion<br>In xcode, It <code>Build</code> is generally used for the application of the market and the internally recognized version of the program as the basis for the update judgment, usually an incrementing <strong>INT</strong> type.</p><p>These two values can be managed in Xcode's project information,</p><br>Img_01.png<p><br>of course, You can also edit directly <code>Info.plist</code> .</p></li> </ul></li></li> <li><li><p>Android<br>In the <code>AndroidManifest.xml</code> definition</p> <ul> <li><p>Android:versionname<br>Corresponds <code>CFBundleShortVersionString</code> to the version number in IOS and is used as product Management.</p></li> <li><p>Android:versioncode<br>Corresponds <code>CFBundleVersion</code> to the build number in IOS as an internal recognition.</p><p>When you use Eclipse development, you can edit the file directly by editing it, and <code>AndroidManifest.xml</code> when using Android Studio, This information is managed by the Gradle script, locating and opening files in the <strong>app</strong> directory under the project directory <code>build.gradle</code> , with version information in <c7 /> segment,</p><br>Img_02.png</li> </ul></li></li> </ul>Get version information in the program<p><p>Generally in the application of the page, we will show the version of the application, convenient customer service location problem, when the application check the update, also often need to use version Information. In fact, whether it's on IOS or Android, it's easier to get stupid information:</p></p> <ul> <li><p><p>Ios<br>Related fields in the index Bundle information:</p></p><pre class="hljs objectivec"><pre class="hljs objectivec"><code class="objectivec"> <span class="hljs-built_in">NSBundle *bundle = [<span class="hljs-built_in">NSBundle mainBundle]; <span class="hljs-built_in">NSString *name = [[bundle localizedInfoDictionary] objectForKey:<span class="hljs-string">@"CFBundleDisplayName"]; <span class="hljs-built_in">NSString *version = [bundle objectForInfoDictionaryKey:<span class="hljs-string">@"CFBundleShortVersionString"]; <span class="hljs-built_in">NSString *build = [bundle objectForInfoDictionaryKey:<span class="hljs-string">@"CFBundleVersion"]; <span class="hljs-built_in">NSString *fullVersion = [<span class="hljs-built_in">NSString stringWithFormat:<span class="hljs-string">@"version: %@ (%@)", version, build];</span></span></span></span></span></span></span></span></span></span></span></code></pre></pre><p><p>In the code above, <code>name</code> for the localized program name, <code>version</code> for the version number, for the build number <code>build</code> , ' Fullversion ' the version number and the build number to form a complete version of the Information.</p></p></li> <li><li><p>Android<br>Get <code>PackageInfo</code> the relevant information in:</p><pre class="hljs java"><code class="java"> <span class="hljs-number">0); String versionName = pi.versionName; <span class="hljs-keyword">int versionCode = pi.versionCode; String fullVersion = String.format(<span class="hljs-string">"version: %s (%d)", versionName, versionCode);</span></span></span></code></pre><p>similarly, <code>versionName</code> for the version number, <code>versionCode</code> for the build number, the difference is that the <code>versionCode</code> <em>int</em> type is stored in ANDROID.</p></li></li> </ul>Xcode and Android Studio build number Self-increment<p><p>The <strong>version number</strong> of the general application is determined by the <em>product Manager</em> or the <em>project manager</em> and determines which segment of the version number is modified according to the stage and function of the Product. But the <strong>compilation number</strong> is more often based on each release increment, and sometimes encounter the same version number corresponding to multiple compilation numbers, such as, emergency fix a key Bug and update release a version, but if each release to manually modify the compilation number back very cumbersome, it is easy to forget and error, Regardless of whether Xcode or Android Studio does not provide the ability to increment the build number, we only have to manually add the compilation script to achieve the purpose of Self-increment.</p></p> <ul> <li><p><p>Xcode<br>To add the compilation process, read and modify <code>Info.plist</code> the version information in:</p></p> <ol> <li><li><p>Open the project, Select the compilation target, and click the <code>Build Phases</code> tab.</p><br>Img_03.png</li></li> <li><li><p>Click <strong>+</strong> Add in the top left corner and choose <code>New Run Script Phase</code> Add a compilation script.</p><br>Img_04.png<br>Img_05.png</li></li> <li><li><p>In the script editor, enter the following script:</p><pre class="hljs bash"><code class="bash"><span class="hljs-comment">#!/bin/bash buildNumber=$(/usr/libexec/PlistBuddy -c <span class="hljs-string">"Print CFBundleVersion" <span class="hljs-string">"<span class="hljs-variable">$INFOPLIST_FILE") buildNumber=$((<span class="hljs-variable">$buildNumber + <span class="hljs-number">1)) /usr/libexec/PlistBuddy -c <span class="hljs-string">"Set :CFBundleVersion <span class="hljs-variable">$buildNumber" <span class="hljs-string">"<span class="hljs-variable">$INFOPLIST_FILE"</span></span></span></span></span></span></span></span></span></span></code></pre><p>The above script uses the Plistbuddy tool to read the <code>Info.plist</code> <code>CFBundleVersion</code> value in +1 and write back <code>Info.plist</code> in.</p></li></li> <li><li><p>We need to get the code to execute before the app is packaged to apply the changes to the app package, so we'll rename the compilation process to a <code>Increase Version Code</code> previous time <code>Copy Bundle Resources</code> .</p><br>Img_06.png</li></li> <li><li><p>thus, every time we compile the Target, we will execute the change script to <code>CFBundleVersion</code> increase the value by 1, but the value will be added 1 when the debug runs, this is not what we want, we just need to add 1 each time we pack the release, so we put the <code>Run script only wehn installing</code> previous tick, This will only execute the script when the app is Packaged.</p><br>Img_07.png</li></li> </ol></li> <li><p><p>Android Studio<br>The Versioncode self-amplification principle of Android Studio is similar to Xcode, but the difference is that we cannot let the compilation script modify itself, but instead use an additional <em>Java Properties</em> to store the version information in the File:</p></p> <ol> <li><li><p>Open the project, Select <code>Module: app</code> the compilation script <code>build.gradle</code> .</p><br>Img_08.png</li></li> <li><p><p>Locate the <code>defaultConfig</code> segment and replace it with the following script:</p></p><pre class="hljs scala"><code class="scala"> <span class="hljs-function"><span class="hljs-keyword">Def<span class="hljs-title">Versionpropsfile = File (<span class="hljs-symbol">' Version.properties ')?<span class="hljs-keyword">If (versionpropsfile.canread ()) {?<span class="hljs-type">Properties Versionprops =<span class="hljs-keyword">New<span class="hljs-type">Properties ()? Versionprops.load (<span class="hljs-keyword"><span class="hljs-keyword">new <span class="hljs-type">fileinputstream (versionpropsfile))?? <span class="hljs-function"><span class="hljs-keyword">def <span class="hljs-title">vercode = VersionProps[<span class="hljs-symbol"> ' version_code '].tointeger ()? Versionprops[<span class="hljs-symbol"> ' version_code '] = (++vercode). toString ()? versionprops.store ( Versionpropsfile.newwriter (), <span class="hljs-literal">null)?? Defaultconfig {applicationid <span class="hljs-string"> "com.example.versionexample"? minsdkversion <span class=" Hljs-number ">19? Targetsdkversion <span class="hljs-number">23? versioncode vercode? versionname <span class="hljs-string"> "1.0.1"?} <span class="hljs-keyword">else {? <span class="hljs-keyword">throw <span class="hljs-keyword">new <span class="hljs-type">GradleException (<span class="hljs-string"> "Could not read version.properties!")? </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre><p><p> This script will open the in the app directory, <code> version.properties </code> config file, read <code> version_code </code> field and add 1 to write back, assigning values to <code> defaultconfig </code> <code> versioncode </code> . </p></p></li> <li><li> <p> At this point, clicking Sync will error <code> Could not read version.properties! </code>, which we just threw in the script because was not found; <code> Version.properties </code> file, We need to create a new ONE. Open the command line to the project directory: </p> <pre class="hljs bash"><code class="bash"> $ <span class="hljs-built_in">cd app/$ <span class=" hljs-built_in ">echo <span class=" hljs-string ">" version_code=1 "> version.properties </span> </span> </span> </code> </pre> <p> Click Sync again to not get an Error. View <code> version.properties </code> files, </p> <pre class="hljs cpp"><code class="cpp"> $ cat Version.properties <span class="hljs-preprocessor"> #Mon Nov <span class="hljs-number">02 <span class="hljs-number">15:<span class="hljs-number">18:<span class="hljs-number">49 CST <span class="hljs-number">2015 version_code=<span class="hljs-number">3 </span> </span> </span> </span> </span> </span> </span> </code> </pre> <p> Found that the Version_code has been increased to 3, indicating that the script was executed two times, but that does not meet our Expectations. </p></li></li> <li><p><p>As with Xcode, we expect to add 1 only when the app is packaged <code>versionCode</code> . So we need to get the compilation parameters, which <code>release</code> will only <code>versionCode</code> increase by 1. The modified script is as Follows:</p></p><pre class="hljs scala"><code class="scala"> <span class="hljs-function"><span class="hljs-keyword">Def<span class="hljs-title">Versionpropsfile = File (<span class="hljs-symbol">' Version.properties ')?<span class="hljs-keyword">If (versionpropsfile.canread ()) {?<span class="hljs-type">Properties Versionprops =<span class="hljs-keyword">New<span class="hljs-type">Properties ()? Versionprops.load (<span class="hljs-keyword">New<span class="hljs-type">FileInputStream (versionpropsfile))??<span class="hljs-function"><span class="hljs-keyword">Def<span class="hljs-title">Vercode = versionprops[<span class="hljs-symbol"><span class="hljs-symbol"> ' version_code '].tointeger ()? <span class="hljs-function"><span class="hljs-keyword">def <span class="hljs-title">runtasks = Gradle.startParameter.taskNames <span class="hljs-keyword">if (': app:assemblerelease ' in Runtasks) {versionprops[< Span class= "hljs-symbol" > ' version_code '] = (++vercode). toString () versionprops.store (versionpropsfile.newwriter (), <span class="hljs-literal">null)} Defaultconfig {applicationid <span class="hljs-string"> "com.example.versionexample"? minsdkversion <span class=" Hljs-number ">19? Targetsdkversion <span class="hljs-number">23? versioncode vercode? versionname <span class="hljs-string"> "1.0.1"?} <span class="hljs-keyword">else {? <span class="hljs-keyword">throw <span class="hljs-keyword">new <span class="hljs-type">GradleException (<span class="hljs-string"> "Could not read version.properties!")? </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre><p><p>This gets the name of the current task, which will be added 1 only if the task is contained <code>:app:assembleRelease</code> <code>versionCode</code> , otherwise the value read to is used Directly.</p></p></li> </ol></li> </ul>Summary<p><p>Versioning is a very important part of product and project management, but it is often easy to overlook in the early stages of development, and it is only a headache when problems are Encountered. In fact, product managers and developers in the product development phase should have a consistent understanding of the product version, I personally usually at the beginning of the project framework to add these rules scripts to the project file, in order to reduce the cost of subsequent version Maintenance.</p></p><p><p>Manage IOS and Android project versions with Xcode and Android Studio</p></p></span>
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