0 x00 overview

The last article sorted out the packaging process, this article from a practical point of view to teach you how to package?

0 x01 Gradle configuration

Android apps are compiled and built on Gradle. We just need to do some configuration in build. Gradle in the main Module.

The following is the configuration file of the landlord, and each parameter is explained in great detail.

Sorting out here is also convenient for their knowledge management.

// Apply the Android plugin: 'com.android.application' android {// Specify the Android API version for compilation compileSdkVersion COMPILE_SDK_VERSION as int // Specify the SDK build tool version BuildToolsVersion BUILD_TOOLS_VERSION defaultConfig {// Default configuration // uniquely identifies app applicationId "com.mouxuejie.fun" // Specify the minimum API version required for running minSdkVersion MIN_SDK_VERSION // Specify the API version used for testing targetSdkVersion TARGET_SDK_VERSION // App version versionCode VERSION_CODE as int //app versionName versionName VERSION_NAME // subcontract multiDexEnabled true} lintOptions {// ignore Lint errors AbortOnError false} signingConfigs {// Signature release {keyAlias KEY_ALIAS keyPassword KEY_PASSWORD storeFile File (STORE_FILE) storePassword STORE_PASSWORD}} buildTypes {debug {//debug} release {// enable code to confuse minifyEnabled true ProguardFiles getDefaultProguardFile(' proGuard-android.txt '), 'ProGuard-rules.pro' // sign signingConfig signingConfigs.release // Remove useless Resource shrinkResources true // Resource file alignment Dev {// applicationId is an alternative app that can be installed on the same device. // ".debug"} Googlepay {} baidu {} qihoo360 {} xiaomi {} Tencent {} Variants -> variants. Outputs. Each {output -> output.outputFile = new  File( output.outputFile.parent, "techexplore-${variant.buildType.name}-${variant.versionName}-$ {variant.productFlavors[0].name}.apk".toLowerCase()) } } } dependencies {compile fileTree(dir: 'libs', include: [' *. Jar ']) compile 'com. Android. Support: appcompat - v7:23.1.0' compile 'IO. Reactivex: rxjava: 1.1.0' compile 'the IO. Reactivex: rxandroid: 1.1.0' compile 'com. Jakewharton. Rxbinding: rxbinding: 0.4.0' compile 'com. Squareup. Retrofit: retrofit: 1.9.0' compile 'com. Squareup. Okhttp: okhttp: 2.4.0'}Copy the code

Here are a few caveats:

  1. The common constants are placed ingradle.propertiesFile, and then inbuild.gradleTo facilitate centralized management.
  2. A signature is required. Only the signed package can be installed on the device. The Debug package uses the Debug signature by default and is automatically generated. The release package uses the Release signature and needs to be generated by the developer.
  3. productFlavorsSet channel number, you can achieve multi-channel packaging, through modificationapplicationIdDifferent build packages for the same app can be installed on the same device at the same time.

The gradle.properties file has the following constants:



Copy the code

0x02 Certificate Signature

1. Why sign your name?

As mentioned earlier, any installation package needs to have a signature. The essence of signing an App is that I created the App, not someone else. Signatures are used to establish a trusted relationship between the application and the developer.

By signing, the Android system ensures the following:

  1. Get an app installation package and know who the author is
  2. When an update is applied, it can be checked if it was submitted by the author
  3. When some files in the application are modified, you can detect whether the changes were made by the author

Therefore, there is no way to upgrade the installation if the signature changes.

2. Debug certificate & Release certificate

From the configuration file above, we know that Android signature needs to be configured with four parameters:

  • Key Alias Indicates the key alias
  • Key password Key password
  • Keystore file Key file
  • Keystore password Key password

When debug packages are generated using the Run button or command line, Gradle automatically signs them with the Debug certificate. The debug certificate keystore default stored in $HOME/android/debug keystore directory. This Debug certificate is generated when the application is first run.

Due to the poor security of the DEBUG certificate, the release signature is normally required for channel packages.

Usually a company or department uses the same certificate.

3. How do I generate a certificate?

There are two ways to generate a digital certificate:

(1) Graphical interface

Open Android Studio and click Build -> Generate Signed APK -> Create new… The New Key Store window is displayed.

As shown above, enter the relevant information and click OK to generate the digital certificate.

(2) Command line mode

$ keytool -genkey -v -keystore com.mouxuejie.jks
-alias mouxuejie -keyalg RSA -keysize 2048 -validity 10000
Copy the code

Meanings of each parameter:

  • -keystore keystore
  • – alias key alias
  • -keyalg Indicates the RSA encryption mode
  • – keysize key size
  • -validity Indicates the validity period of the certificate

The specific execution results are as follows:

0x03 Multi-channel packaging

We can use a graphical interface for multi-channel packaging. However, we will only use gradle command line packaging.

So how does it work?

Run gradle tasks to get the following task:

The meanings of these tasks are as follows:

  • Gradle Assemble generates Debug and Release packages for all channels

  • Gradle assembleAndroidTest generates test packages for all channels

  • Gradle assembleDebug generates Debug packages for all channels

  • Gradle assembleRelease produces Release packages for all channels

  • Gradle assembleAnzhi generates Debug and Release packages for a channel such as Anzhi

There is no gradle assembleWandoujiaRelease as mentioned in stormzhang’s article.

Executable Gradle Assemble generates a stack of files under app/build/outputs/ APK, as shown below:

0x04 Reference Documents

Concerned about Internet technology development or talent related, please scan code to pay attention to a senior student’s public number: senior student’s IT column