Everything you should know when using Android studio for the first time (iii): Gradle project Build

Source: Internet
Author: User
Tags jcenter

? "declaration"

Welcome reprint, but please keep the original source of the article →_→

Life One number: http://www.cnblogs.com/smyhvae/

Article Source: http://www.cnblogs.com/smyhvae/p/4456420.html

Contact information: [Email protected]

Series

Everything you should know when using Android studio for the first time

Everything you should know when using Android studio for the first time (ii): Create a new project of your own and install the Genymotion simulator

Everything you should know when using Android studio for the first time (iii): Gradle project Build

Directory:

1, the concept of Gradle

2, Gradle configuration jar package, and Libs folder import jar Package Difference

3, Signature Packaging:

(1) Studio

(2) command line

(3) Principle of Gradle Wrapper

4, the use of Buildconfig

5, Simple Introduction module in the Build.gradle file parameter meaning

First, the concept of Gradle:

    • Gradle is a build tool
    • Gradle is based on the groovy language.
    • Gradle is primarily intended for the Java language

Open the Android studio built-in terminal and view the Gradle version by entering the following command:

Gradlew-v

If this is the first time to run this command, the system will help us download gradle to local, the path to download is:

After entering the above command, view the version of Gradle to 2.2.1 as shown in:

Second, through the Gradle to import the jar package:

In the 3rd section of the second paragraph of the previous article, we talked about importing jar packages by copying files to the Libs folder. This time, how to configure the jar package through Gradle. We still take Google's Gjson.jar as an example, if you have previously been copied by the way the file has been dumped, please delete it by yourself.

1. Configure third-party jar packages via Gradle

We see that each module has a build.gradle file , which is actually a configuration file corresponding to the module . We will explain in the last paragraph about the meaning of the specific content in the Build.gradle file.

Let's take a look at this module named app, whose build.gradle corresponds to the graphical interface of the following project Stucture:

, switch to the Dependencies tab, as shown in:

, click Add, and then select "Library dependency" to bring up the following screen:

, we enter "Gson" in the search box and then make sure that we have the jar package that we need at the arrow, and add it:

We will then find that the Build.gradle of the app module has a line of code that introduces the Gson.jar:

In fact, if you can remember to live above this line of code, write the code directly can also be imported.

At this point, Gson this jar package no longer appears in the Libs folder, but appears in the bottom external libraries, as shown in: (and the latest version OH)

2, Gradle Import Jar Package Features: (and Libs folder import jar package difference)

    • Gradle Importing jar packages is more convenient, one line of code can be done. Not like the latter, you have to go to the official download.
    • If the official will update the jar package, we only need to change the version number in Build.gradle, do not have to re-download the official website.

3. Where does the jar package imported through Gradle come from:

It is important to note that not all jar packages can be imported through Gradle. Where does the jar package imported through Gradle come from? The explanations are as follows:

We found HelloWorld this project directory has a build.gradle file that opens it:

The "Jcenter ()" In line 17th means that all jar packages imported through Gradle are stripped from the central warehouse of Http://bintray.com/bintray/jcenter. If the jar package you need is not on this website, then you can't import it by Gradle.

By the way, the Gradle 1.1.0 in Classpath in line 8th is a Gradle plugin for Android (also stripped from the central warehouse). And we use the Gradle version as shown:

The path to our own downloaded Gradle is as follows:

Three, signature packaging two ways:

Note: To our own development of the app signature, it represents my own copyright, in the future to upgrade, you must use the same signature. The signature represents your identity (that is, keystore), and multiple apps can use the same signature.

If you do not know what the signature is, please own Baidu Oh. The way to sign in Eclipse is: Select project, mail Select "Export-android-export android Application",

1. Mode 1: Sign through Android Studio:

Select the app module and choose the menu bar "Build-generate signed apk":

The following screen appears:

, if you are using your signature for the first time, click the Red Box section to create a new signature, or if you have previously signed a file, select the Blue box section to import it. Then I'll select the red-framed part first:

, after clicking "Finish", you can see the bottom of Android Studio: Gradle is performing assemblerelease such a task as shown in:

After generating the signed apk, a prompt will appear:

2, Mode 2: Signed by the command line:

(1) Load key Store:

Let's first delete the APK file signed by the first method. Next, proceed to the second way to sign the command line.

Open the Project Stucture graphical interface:

, select the app module, then switch to the Singning tab bar, then click Add, then generate the release signature information , followed by click "OK". Then do the following:

, switch to the Build Types tab and select signing config as "release" to configure the release signature information that was just generated.

After the operation is complete, we can see that the Build.gradle file of the app module has the following code in the Red-box section:

Then execute "Build-clean Project" on the menu bar:

(2) Generate Realease version of APK:

Immediately after the command line terminal enter the following command: (as has the command line terminal integrated into the software)

gradlew assembleRelease

If the operation succeeds, the effect is as follows:

The generated signature good apk is in the following location:

3. Why use the Gradlew command instead of the Gradle command:

In the HelloWorld project directory there is a Gradle folder, under the Gradle/wrapper directory there is a gradle-wrapper.properties file, open it:

Represents the Gradle version information that HelloWorld this project relies on. The red Line says, if our project does not have Gradle, the software will download gradle according to this URL, finally know why the first time to open as will be so slow?

If we execute the gradlew command, we actually execute the above Gradle wrapper and find the Gradle 2.2.1 We have downloaded. If there are many projects now, but the Gradle version of each project is different, I will have to configure each version of the Gradle to the environment variables, and execute the Gradlew command, will avoid this trouble.

Iv.. buildconfig file:

Buildconfig is a class that is automatically generated by the IDE and is stored in the Gen directory (such as an R file) in Elipse. In Adroid Studio, the Buildconfig file is stored in the following location: app/build/generated/source/buildconfig/dubug/<packagename>/ The Buildconfig.java. As shown in the following:

Five, module in the build.gradle file parameter meaning:

The main module is the Build.gradle, as follows:

01 Line: Apply plugin: ' com.android.application ' means that the module is the module of this application

15 Line: ApplicationID "com.smyhvae.helloworld" Application package name

16, 17 Lines: backward-compatible minimum version, compiled version. Note: This message no longer appears in the App/src/main/androidmanifest.xml.

Line 23: You need to confuse the code with 24 rows of proguard files. In the release state, it is best to change to true.

Everything you should know when using Android studio for the first time (iii): Gradle project 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.