Basic Gradle use of Android Studio

Source: Internet
Author: User

One feature of Android Studio is the use of the automated build tool gradle.

1. Configuring Gradle Environment variables

Download latest Gradle Full Pack
: http://www.androiddevtools.cn/index.html
Configuring environment Variables for Gradle

    • New Gradle_home Directory:

      Open System--Advanced system settings, select System Variables in the popup menu, click New, enter the Gradle name and the directory address.

    • Add the Gradle_home directory to the system path:

      Locate the path directory in the system variable, click Edit, select New in the pop-up menu, and then enter "%gradle_home%\bin" in the text box;

      If you are accustomed to traditional modes such as Win7, you can click the Edit Text button

      In the text box that pops up, enter ";%gradle_home%\bin" and remember that there must be an English state in front of ";".

      If the configuration succeeds, you can enter%gradle_home% in the file search, if you can jump to the specified GRADLE directory, or if you enter dir%gradle_home% in cmd to list the structure of the GRADLE directory, the configuration is successful.

2. View Gradle version

Open Android Studio built -in terminal

Click on the "Terminal" icon in the lower left corner:

Then the following terminal window appears:

Enter the "gradlew-v" command to view the Gradle version
Enter the "gradlew-v" command in the terminal command to view the current Gradle version information;
Is the first time to run this command, the system will help us download gradle to local (need to flip the wall here, Hong Kong's server faster)

Enter GRADLEW-V in the terminal of Android studio

After the download is successful, you can see that the local directory is saved

Enter "Gradlew-v" again and the current Gradle version appears

3. Common Gradlew Commands

the meaning of the Gradlew command
The Gradlew command is actually the address of the environment variable under the path specified in the last line of the gradle-wrapper.properties execution;
Otherwise, different projects may have different paths to the environment variables, if you use the Gradle command, you need to modify the system environment variable address frequently;
The Gradlew command, however, represents the address of the environment variable under the current project.

gradlew-v
./gradlew-v checking the current Gradlew version

Enter GRADLEW-V in the terminal of Android studio

You can see that the current Gradle version is 2.12.

Gradlew Clean
./gradlew clean clears the build folder and dependencies under the project directory

Enter Gradlew clean in the terminal of Android studio

After entering the appeal order, you can see that the project directory is not in the build folder.

Gradlew Build
./gradlew build check dependencies and compile package

Enter the Gradlew build in the terminal of Android studio and this command will debug/release the environment pack to see BUILD->OUTPUTS->APK

If the following error occurs during compilation, you can view detailed error information in the address pointed to by the error report, the Red section indicates the type of the error, and the workaround is provided at the bottom.

Gradlew Assemble
./gradlew Assembledebug compiles and hits the debug package

Enter Gradlew Assembledebug in the terminal of Android studio

You can view build->outputs->apk more xx-debug.apk and xx-debug-unaligned.apk.

./gradlew Assemblerelease compiling and playing the release package

Enter Gradlew Assemblerelease in the terminal of Android studio

Can see build->outputs->apk more xx-release-unaligned.apk, if the official release only need to play release package, you need to appeal assemblerelease order.

Beyond that
Assemble can also be used in conjunction with productflavors, specifically in the next multi-channel packaging for further explanation.
?./gradlew installrelease Release mode package and install
?./gradlew uninstallrelease Uninstall Release Mode pack
The next article describes the convenient multi-channel packaging with Gradle, as well as the complete Gradle configuration in the project. There are questions or suggestions to welcome you directly blog comments.

4. Command mode signature

Open the project Stucture graphical interface, select the item name, and then select the "+" in the signing item on the right

Rename the config on the right to release and debug and fill in the signature information, click OK below

Then select the corresponding signature configuration information in the Release/debug column (above)

Click OK after this module's Build.gradle file more than the following signature information

Click Clean Project

Enter Gradlew Assemblereleas to generate the above configured signature apk, which can be viewed in build->outputs->apk.

5.BuildConfig file

Auto-generated buildconfig
Buildconfig is a class that is automatically generated by the IDE, which is stored in the Gen directory (such as the R file) in Eclipse, while in Android Studio, the Buildconfig file is stored in the following location: app/build/generated/ The Source/buildconfig/dubug//buildconfig.java.

The key is to specify the project name, version information, and so on, as shown in:

the Build.gradle in module
Specifies that the module for the application is the module

‘com.android.application‘

Specify to exclude redundant files when packaging

/** 打包选项 */packagingOptions {    exclude “…”}

Specify compiled version configuration information, etc.

/** 默认配置信息 */defaultConfig {    "com.xx.xx.xx "    16// 清单文件中不需要再次配置    23    20160523    "1.01.14"    true// dex突破65535的限制}

Specifies that error messages are ignored at build time

/** 执行lint检查 */lintOptions {    false }

Specify signature information for command-mode signing

/** 设置签名路径 */signingConfigs {    debug {        ‘key..‘        ‘password..‘        storeFile file(‘E:/ADS/keystore.jks‘)        ‘ password..‘    }    release {        ‘key..‘        ‘ password..‘        storeFile file(‘E:/ADS/keystore.jks‘)        ‘ password..‘}}

Specify compilation rules, etc.

/** Compile Build type * /Buildtypes {release {Buildconfigfield"Boolean","Log_debug","false"//No logMinifyenabledtrue  //ConfusionZipalignenabledtrue //Zipalign optimizationShrinkresourcestrue //Remove useless resource files        //load Default obfuscation profile Progudard-android.txt in the SDK directory, no tube, proguard.cfg is our own confusing file with <span></span>Proguardfiles Getdefaultproguardfile (' Proguard-android.txt '),' Proguard.cfg 'Signingconfig Signingconfigs.release//Signature}}
6.gradle importing third-party packages

Import Method
First remove the offline gson-2.6.2 package under Libs

Click "+" in the project's dependent "Dependencies" settings screen

Enter Gson in the pop-up screen and click on the search icon on the right to select it in the search results.

After clicking OK, you can see a line of dependencies in the Build.gradle

At this point the Gson library is no longer under Libs, but under external libraries.

Import Pros and cons
Compared to Libs under the Guide package, Gradle import jar package more convenient, a line of code can be done. No need
To download the offline installation package, and if the official jar package has been updated, we just need to change the version number in Build.gradle, without having to go back to the website to download it.

But Gradle import needs to flip the wall.

where does the import come from
At the beginning of the Build.gradle file, there is Jcenter ():

buildscript {    repositories {        // 所有通过gradle导入的jar包都是从http://bintray.com/bintray/jcenter这个中央仓库上扒下来的        jcenter()    }    dependencies {        ‘com.android.tools.build:gradle:2.1.0‘    }}

If the jar package you need is not on this site, you cannot import it by Gradle.

Basic Gradle use of Android Studio

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.