Original address: http://blog.csdn.net/zivensonice/article/details/51672846
How to pack manually in a fledgling manual package
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).
1. Create a signature file
2. Fill in the signature parameters
3. Build apk
Note: Signed password and key password attention to keep, do not forget, signature file don't leak, also don't lose
Why are you packing?
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 build/outputs/apk
. Just pass this on to the market.
No, of course not, think about the phone when installing the app how to distinguish the various apk.
By the package name + signature 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.
Better channel Packaging
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.
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.
Sub-channel packaging
Yes, we need to use a sub-channel packaging, then we need to solve two problems
1. How to differentiate the identity of each platform
2. How to generate dozens of packages, hundreds of packages per version update
The first simple, with friends of the league pack of classmates must not be unfamiliar with this code
<meta-data android:name="UMENG_CHANNEL" android:value="Channel_ID" />
Value is filled in the values of each platform, such as the completion of UC, YYB (App Bao), 360, Baidu replaced Channel_ID
, 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.
Second question:
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.
Later, learned ant automatic Packaging, fast is fast, just configure too pit Dad, super trouble.
Now with Android Studio, mom doesn't have to worry about me being able to pack up happily.
Create a new project and modify the Module:app build.gradle
to
Apply plugin:' Com.android.application ' Android {signingconfigs {config {Keyalias' Maker ' Keypassword' 1234make ' StoreFileFile'/users/nevermore/androidstudioprojects/blog/jks/makeapp.jks ') Storepassword' Make1234 '}} compilesdkversionBuildtoolsversion"23.0.3" Defaultconfig {ApplicationID"Com.example.makeapp" minsdkversionTargetsdkversionVersioncode1 versionname"1.0"} buildtypes {debug {minifyenabledFalse debuggableTrue} release {minifyenabledTrue Proguardfiles Getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro ' Signingconfig signingconfigs.config debuggable false}} Productflavors {uc {manifestplaceholders = [umeng_channel_value: "UC"]} _360 {manifestplaceholders = [UMENG_CH Annel_value: "The" "" " Baidu {manifestplaceholders = [umeng_channel_value: " Baidu "]} yyb {manifestplaceholders = [Umeng_channel_value: "Yyb"]}}} dependencies {Compile Filetree (dir: ' Libs ', include: [' *.jar ']) testcompile ' junit:junit:4.12 ' Compile ' com.android.support:appcompat-v7:23.3.0 '}
We need to configure:
signingConfigs
This is the new key information we've just created.
buildTypes
Package type, including Debug and release
productFlavors
The packing channel is configured here at the same time in the Androidmanifest add, channel identification
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="example.com.makeapk"> <meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" /> ...省略</manifest>
3 Now there's another question--how do you generate a channel pack when the code is finished?
OK, at the command line input gradlew assembleRelease
, indicates that all release packages are generated, the generated package is in the build\outputs\apk
directory, if you want to generate the specified package (UC|360|BAIDU), the specified version (release| Debug), on the right Gradle Project
can help you
4. Remove the unaligned.apk
extra
Execute-- gradlew assembleRelease
, find a problem, generate not only the package we need, the unaligned.apk
type of APK is also lost
unaligned.apk
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:
. -name "*-unaligned.apk" | xargs rm -rf
5 Optimizing Gradle Code
The code you just wrote
productFlavors { uc { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "uc"] } _360 { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "360"] } baidu { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"] } yyb { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "yyb"] } }
Some redundancy, modified to reduce the amount of our code
productFlavors { uc {} _360 {} baidu {} yyb {} } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] }
Is it more beautiful?
6. Gradle
The grammar has a learning curve for the newcomer, can it be any easier
We have the tools, open the top build.
Select the red section, the inside of the edit box can help us get acquainted with gradle faster
Let's take a look at the specifics of the code and edit boxes.
7. Packing too much, need to clean up
Perfection Dinosaur Fast Play directly modify channel number
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.
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.
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.
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.
This packaging is very fast, and more than 900 channels can be played in less than a minute.
Yes, that's the strategy of the American mission.
How to use: Https://github.com/GavinCT/AndroidMultiChannelBuildTool
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.
Install a Python environment, run a multichannelbuildtool.py, anyone can pack!
After all, practice is the only criterion for testing truth:
Copy one that we just generated to the app-uc-release.apk
project directory
Sure enough, 1S came out so many bags.
Anti-compilation look, the bag is playing right
Command line
apktool d xxx.apk
Open the directory and first confirm the identifiers in the XML we generated
And then we saw that the group was finished with the fast package.
However, using Gradle to generate 4 channels, we spent the 26.5秒
others only spent a visual1s
1 min 900 Pack It's not a dream.
Build variants (build variant version) Toggle URL
Development of each debug interface, I often configure 3 URLs
public class Constant { public static final String URL = "主机IP地址:10.18.3.1";// public static final String URL = "线上环境:http://www....";// public static final String URL = "测试环境http://....";}
- The computer host IP address of the server brother with which I wrote the interface
- Test Server Environment Address
- On-Line environment address
Development time is not a problem, I manually commented out, switch specifically to use that URL debugging on the line, but packaged to test the time of trouble, need three environment to play to them, change a package, change a package, waste time, also often confuse the code.
This time, it's time for build variants to show great divinity.
What is the variant version?
Open mode
Do not need, change the code, to choose to run the environment to switch directly, convenient and fast
Compare two versions of different
Final effect:
Build
One last question,
Can you put a different environment package on the same phone?
The test wants to compare the difference between different environments and apps
At the same time to put on the phone, that is, the package name is different, obviously now we have no need to manually change, direct configuration parameters
Finally, three packs are on the phone.
This method is also suitable for both the debug and release versions of the switch to the phone.
For example, if you go to an interview, you want to show the interviewer both the online version and the currently developing version, or if you want to show the difference between the old version and the new version, you can do it this way.
Build a custom app
See: http://blog.csdn.net/zivensonice/article/details/51684931
The pinnacle of packaging security
Cond...
Code: Https://github.com/zhouruikevin/makeapp
"Go" Android Studio package full guide---from Getting started to mastering