Android Studio Package and Gradle configuration build

Source: Internet
Author: User

This article reprinted Guo Lin public number

https://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650241610&idx=1&sn= B8af73f6c288b6617d9fe0ab3618118d&pass_ticket=qk4j37kpmgnlsycecwmb64hxkhevjg5msjubqeqguki%3d

Generate signature files manually packaged

First generate the signature file, click Build---Generate signed APK:

If you do not have a signature file, click Construct One:

The three places circled in the figure are important information, the rest is optional. After the build, you can continue signing the package with the signature file:

Fill in the information just now and continue:

Choose the output path, select the package type, complete!

Automatic Packaging by configuration

If you feel that the packing steps above are too cumbersome, then we will start adding configurations to the Gradle and automatically package them.

Shortcut keys Ctrl+alt+shift+s or File-and Project Structure:

Select application and add a signature configuration under the Signing tab, where the Store file is our signature file. We can also continue to add build types, choose the build Types tag, add release, select the signature configuration just now:

This way we have two types of debug and production type of packaging, and can be automatically packaged as needed later. Select Project, click Build, select Build Variant, choose release:

Then click Build-Build APK, package the files in the. \APP\BUILD\OUTPUTS\APK, so we used our own signature file and made a production package. Also can be typed debug package, if in Build Types we did not set Signing Config, the system by default with the debug signature file package.

Hide signature file sensitive information

After we have set up the package configuration, Gradle Auto Sync will generate the following code in the application Build.gradle file:

 signingconfigs {       config {          &NBSP ; Keyalias  ' gaok_release '            keypassword  ' 199110 '              storefile file< span> ( ' E:/gk/gaok.jks ')            storepassword ' 199110 ' & nbsp      }    }        

These are sensitive information about the signature file, and it's best to hide this information if we're collaborating on the development.

Start by creating a new Keystore.properties file in the project root directory:

storePassword=199110keyPassword=199110keyAlias=gaok_release storeFile=E:/GK/gaok.jks

Note here: None of the attributes have single quotes. Then add the following location in the Build.gradle file:

Apply plugin:' Com.android.application '
Create A variable called Keystorepropertiesfile, and initialize it to your
Keystore.properties file, in the Rootproject folder.

def keystorepropertiesfile = Rootproject.file ("keystore.properties") //Initialize a new properties () object Called Keystoreproperties.

def keystoreproperties = new Properties () //Load your keystore.properties file into the Keystoreproperties object .

Keystoreproperties.load (new FileInputStream (Keystorepropertiesfile)) Android {...}

read Fetch the  keystore.properties  file, and then change the configuration:

 android {   signingconfigs {       config {    &N Bsp      keyalias keystoreproperties[ ' Keyalias ')         & nbsp  keypassword keystoreproperties[ ' Keypassword ')           & nbsp  storefile file  (Keystoreproperties[       

In this way, all the sensitive information is placed in the file, only need to well save the Keystore.properties file, remember not to upload files to git!

Set flavor to achieve different flavor resource substitution

What is flavor? Look at this picture:

We can add multiple "flavors" under the Flavors tab to suit your individual needs.

Once the flavor is set, our Build.gradle file will be generated as follows:

productFlavors {  // 华为商店      baidu {}        // 百度手机助手      yinyongbao {}   // 应用宝  }

For example, I have added a number of channels for the application market, each channel may be adapted to different SDK versions, or I would like to implement different flavors (for example: Production and debug mode) different logo, some code is different, then we can set the source set directory, Specially included code and resource files in different configurations. first switch to Project view, our project by default is only the main source set, that is: /src/main .

Add a source set of debug types below:

The Target Source Set here will contain buildtypes, productflavors, and a combination variant of both. I have set up two types of debug and release, Huawei, Yingyongbao, Baidu three flavors, so there will be so many options. We chose debug and the system automatically generated the corresponding directory for our project.

The same can be added to the XML file directory:

I've added the values file here, so you can add colors, strings, and styles files.

To continue adding a picture resource:

Here is just an example, I chose a different icon:

Here is a hint: the icon name repeats, because the main source set already has the Ic_launcher icon, but it doesn't matter, we select the debug source set in the next step:

The final directory is as follows:

Now that we have the debug source set, there is a different app logo in the source set, which means that if we now hit the debug package, its icon will be the icon under the debug source set, such as the debug package on the left, and the release package on the right.

As for why I can install the debug package and release package at the same time, because I added Applicationidsuffix:

 buildtypes {  Release {
      minifyenabled false       proguardfiles getdefaultproguardfile ( ' proguard-android.txt '), ' Proguard-rules.pro '
      Signingconfig Signingconfigs.config       applicationidsuffix ". Release"  }   Debug {      SIG Ningconfig signingconfigs.config       applicationidsuffix & nbsp }
}

The Applicationidsuffix field indicates that you add a suffix to your default program ID (the package name) without changing it. For example, if your package name is Com.gk.app, but you want to differentiate between test packages and formal packages, this time set Applicationidsuffix to. Debug, then your application's package name becomes Com.gk.app.debug

Multi-channel packaging

Work, the release of the software package is generally done by the operation of small partners, the package name format is generally: App + version number + channels, different channels may be different configurations, so we have to add the corresponding flavor for all channels.

android{Productflavors {///three channels Huawei {}//the company store Baidu {} //Baidu Mobile assistant Yinyongbao {}// app Bao } //Bulk Channel package value substitution (some third-party libraries need to use channel name) Productflavors.all {flavor ///allies, Aurora push channel Pack, Umeng_channel is based on your A        Ndroidmanifest.xml to configure, see below. Flavor.manifestplaceholders = [Umeng_channel:name, Jpush_channel:name]}}     

The Umeng_channel here are configured in the Androidmanifest.xml file:

<!--变量采用${变量名}这样来替换,不局限于<meta-data/> 标签,任何你想替换的都行--> <meta-data    android:name="UMENG_APPKEY"    android:value="${UMENG_APPKEY}"/>

Then batch rename:

android{...The output file is configured in the following format: app-{version number}-{channel name}.apk    applicationvariants.all {variant        vari Ant.outputs.each {output            def outputFile = output. outputFile            if  (outputFile! = null && outputFile.name.endsWith ( apk ')) {               def dirName = outputfile.parent //output folder where   ;              //file name modification           & nbsp    def fileName =  "App-${defaultconfig.versionname}-${variant.flavorname}. APK "               output.outputfile = new File (dir Name, FileName)            }        }    }     

So look again at the Select Build Variant:

We play a Baidu release package:

Similarly, multi-channel packaging is done.

Android Studio Package and Gradle configuration build

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.