Android Studio Packaging full Raiders---from Getting started to mastering

Source: Internet
Author: User
<span id="Label3"></p>How to pack manually in a fledgling manual package<p><p>Once the project is finished, it is time to upload the application to the market, and the problem arises-how to turn the code into. apk (installable files for android).<br>1. Create a signature file<br><br>2. Fill in the signature parameters<br><br>3. Build APK<br>Note: signed password and key password attention to keep, do not forget, signature file Don't leak, also don't lose</p></p>Why are you packing?<p><p>I have this question at the beginning, our code is not to click the Run button to install directly to the phone, we can find the new app-debug.apk in the project catalog directory <code>build/outputs/apk</code> . just pass this on to the Market.<br><br>no, Of course not, think about the phone when installing the app how to distinguish the various APK.<br>By the <strong>package name + signature</strong> to determine the uniqueness of an application, the debug.apk is only used to debug the system for us, if uploaded to the market, how to ensure his Security.</p></p>Better channel Packaging<p><p>OK, we've solved the first step-how to Pack. After uploading, the market feedback found that our app is too good to write, this time the boss let's hurry up on some platforms, mainstream platform, Non-mainstream platform to put up.<br>So the problem comes, in order to facilitate the statistics of the installation of each platform, in line with the operation of promotion, the need to count the installation of each PLATFORM.</p></p>Sub-channel Packaging<p><p>yes, we need to use a sub-channel packaging, then we need to solve two problems<br>1. <strong>How to differentiate the identity of each platform</strong><br>2. <strong>How to generate dozens of packages, hundreds of packages per version update</strong><br>The first simple, with friends of the league pack of classmates must not be unfamiliar with this code</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs haskell"><meta-<span class="hljs-typedef"><span class="hljs-keyword">data</span></span> android:name=<span class="hljs-string">"UMENG_CHANNEL"</span> android:value=<span class="hljs-string">"Channel_ID"</span> /></code></pre></pre><p><p>Value is filled in the values of each platform, such as the completion of uc, Yyb (app bao), 360, Baidu replaced <code>Channel_ID</code> , the app is installed, you can read this value and then passed to the background, so as to differentiate the installation requirements of each PLATFORM.<br>Second Question:<br>In the Eclipse era, I was the first to manually hit, a dozen 30 packs, every time the most afraid of version updates, the basic one afternoon are doing mechanical movement, now think up hands are cramping.<br>later, learned ant automatic packaging, fast is fast, just configure too pit dad, Super Trouble.<br>Now with Android Studio, mom doesn't have to worry about me being able to pack up happily.<br>Create a new project and modify the Module:app <code>build.gradle</code> to</p></p><pre class="prettyprint"><code class=" hljs livecodeserver">Apply Plugin:<span class="hljs-string"><span class="hljs-string">' Com.android.application '</span></span>Android {signingconfigs {config {keyalias<span class="hljs-string"><span class="hljs-string">' maker '</span></span>Keypassword<span class="hljs-string"><span class="hljs-string">' 1234make '</span></span>StoreFile<span class="hljs-built_in"><span class="hljs-built_in">file</span></span>(<span class="hljs-string"><span class="hljs-string">'/users/nevermore/androidstudioprojects/blog/jks/makeapp.jks '</span></span>) Storepassword<span class="hljs-string"><span class="hljs-string">' make1234 '</span></span>}} compilesdkversion<span class="hljs-number"><span class="hljs-number"></span> at</span>Buildtoolsversion<span class="hljs-string"><span class="hljs-string">"23.0.3"</span></span>Defaultconfig {applicationid<span class="hljs-string"><span class="hljs-string">"com.example.makeapp"</span></span>Minsdkversion<span class="hljs-number"><span class="hljs-number"></span> the</span>Targetsdkversion<span class="hljs-number"><span class="hljs-number"></span> at</span>Versioncode<span class="hljs-number"><span class="hljs-number">1</span></span>Versionname<span class="hljs-string"><span class="hljs-string">"1.0"</span></span>} buildtypes {debug {minifyenabled<span class="hljs-constant"><span class="hljs-constant">false</span></span>Debuggable<span class="hljs-constant"><span class="hljs-constant">true</span></span>} Release {minifyenabled<span class="hljs-constant"><span class="hljs-constant">true</span></span>Proguardfiles Getdefaultproguardfile (<span class="hljs-string"><span class="hljs-string">' Proguard-android.txt '</span></span>),<span class="hljs-string"><span class="hljs-string">' Proguard-rules.pro '</span></span>Signingconfig Signingconfigs.config debuggable<span class="hljs-constant"><span class="hljs-constant">false</span></span>}} productflavors {uc {manifestplaceholders = [umeng_channel_value:<span class="hljs-string"><span class="hljs-string">"uc"</span></span>] }<span class="hljs-title"><span class="hljs-title">_360</span></span>{manifestplaceholders = [umeng_channel_value:<span class="hljs-string"><span class="hljs-string">"</span> the"</span>]} Baidu {manifestplaceholders = [umeng_channel_value:<span class="hljs-string"><span class="hljs-string">"baidu"</span></span>]} Yyb {manifestplaceholders = [umeng_channel_value:<span class="hljs-string"><span class="hljs-string">"yyb"</span></span>]}}}dependencies {compile Filetree (dir:<span class="hljs-string"><span class="hljs-string">' Libs '</span></span>,<span class="hljs-built_in"><span class="hljs-built_in">include</span></span>: [<span class="hljs-string"><span class="hljs-string">' *.jar '</span></span>]) Testcompile<span class="hljs-string"><span class="hljs-string">' junit:junit:4.12 '</span></span>Compile<span class="hljs-string"><span class="hljs-string">' com.android.support:appcompat-v7:23.3.0 '</span></span>}</code></pre><p><p>We need to configure:</p></p> <ol> <ol> <li><code>signingConfigs</code>This is the new key information we've just Created.</li> <li><code>buildTypes</code>Package type, including Debug and release</li> <li><code>productFlavors</code>The packing channel is configured here at the same time in the Androidmanifest add, channel identification</li> </ol> </ol><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs r"><manifest xmlns:android=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span> package=<span class="hljs-string">"example.com.makeapk"</span>> <meta-data android:name=<span class="hljs-string">"UMENG_CHANNEL"</span> android:value=<span class="hljs-string">"${UMENG_CHANNEL_VALUE}"</span> /> <span class="hljs-keyword">...</span>省略</manifest></code></pre></pre><p><p>3 <strong>now There's another question--how do you generate a channel pack when the code is finished?</strong><br>OK, at the command line input <code>gradlew assembleRelease</code> , indicates that all release packages are generated, the generated package is in the <code>build\outputs\apk</code> directory, if you want to generate the specified package (uc|360|baidu), the specified version (release| Debug), on the right <code>Gradle Project</code> can help you<br><br>4. <strong>Remove the <code>unaligned.apk</code> extra</strong><br>execute-- <code>gradlew assembleRelease</code> , find a problem, generate not only the package we need, the <code>unaligned.apk</code> type of APK is also lost<br><br><code>unaligned.apk</code>is not the implementation of the alignment command package, is the middle form, this need to delete, no need not to know why Gradle did not help us to delete this useless thing, the problem is we do not want to one of the manual Deletion. well, write a script command that executes in the output Folder command line as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs lasso"><span class="hljs-built_in">.</span><span class="hljs-attribute">-name</span><span class="hljs-string">"*-unaligned.apk"</span><span class="hljs-subst">|</span><span class="hljs-attribute">-rf</span></code></pre></pre><p><p>5 <strong>Optimizing Gradle code</strong><br>The code you just wrote</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs perl"> productFlavors { <span class="hljs-keyword">uc</span> { <span class="hljs-string">"uc"</span>] } _36<span class="hljs-number">0</span> { <span class="hljs-string">"360"</span>] } baidu { <span class="hljs-string">"baidu"</span>] } yyb { <span class="hljs-string">"yyb"</span>] } }</code></pre></pre><p><p>Some redundancy, modified to reduce the amount of our code</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm"> productFlavors { uc {} _360 {} baidu {} yyb {} } productFlavors<span class="hljs-preprocessor">.all</span> { flavor -> flavor<span class="hljs-preprocessor">.manifestPlaceholders</span> = [UMENG_CHANNEL_VALUE: name] }</code></pre></pre><p><p>Is it more beautiful?<br>6. <strong> <code>Gradle</code> for new students, Grammar has a learning curve, can be a little easier</strong><br>We have the tools, open the top build.<br><br>Select the red section, the inside of the edit box can help us get acquainted with gradle faster<br><br><br><br><br>Let's take a look at the specifics of the Code and edit Boxes.<br><br>7. <strong>Packing too much, need to clean up</strong><br></p></p>The perfection is afraid to make a quick call modify channel number directly<p><p>Think about it, There's no need to compile the entire project to generate the channel number if you're just hitting the channel Pack.<br>If you can directly modify the APK channel number, and do not need to re-sign can save a lot of packaging time. fortunately, we have found this way. Directly unzip the apk, the extracted root directory will have a meta-inf directory.<br>If you add an empty file within the Meta-inf directory, you do not have to re-sign the app. therefore, you can uniquely identify a channel by adding different empty files for different channels of Application.<br>In this way, one channel package per dozen only needs to copy an apk and add an empty file named after the channel number in META-INF.<br>This packaging is very fast, and more than 900 channels can be played in less than a Minute.<br>yes, That's The strategy of the American Mission.</p></p><p><p>How to Use: Https://github.com/GavinCT/AndroidMultiChannelBuildTool<br>Using this tool, Android programmers only need to put Channelutil.java into the project to use, later packaging things do not have to do their own.<br>Install a Python environment, run a multichannelbuildtool.py, anyone can pack!<br>After all, practice is the only criterion for testing truth:<br>Copy one that we just generated to the <code>app-uc-release.apk</code> project directory<br></p></p><p><p>Sure enough, 1S came out so many bags.</p></p><p><p>Anti-compilation look, The bag is playing right<br>Command line</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm">apktool d xxx<span class="hljs-preprocessor">.apk</span></code></pre></pre><p><p></p></p><p><p>Open the directory and first confirm the identifiers in the XML we generated<br><br>And then we saw that the group was finished with the fast Package.<br><br>however, using Gradle to generate 4 channels, we spent the <code>26.5秒</code> others only spent a visual<code>1s</code></p></p><p><p>1 min 900 pack it's not a Dream.</p></p>Build variants (builds variant Version)<p><p>Cond...</p></p>The pinnacle of packaging security<p><p>Cond...</p></p> <p><p>Android Studio Packaging full Raiders---from Getting started to mastering</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.