Android studio is built with configuration for the first time (3) gradle project, androidgradle

Source: Internet
Author: User
Tags jcenter

Android studio is built with configuration for the first time (3) gradle project, androidgradle

1. gradle Concept

2. Configure the jar package in gradle, which is different from the jar package imported into the libs folder.

3. Signature packaging:

(1) Studio

(2) Command Line

(3) Principle of gradle wrapper

4. Use of BuildConfig

5. Briefly introduce the parameter meaning of the build. gradle file in the module

 

I. gradle concept:

  • Gradle is a build tool
  • Gradle is based on the Groovy Language
  • Gradle mainly targets Java

Open the Android Studio built-in terminal and run the following command to view the gradle version:

gradlew -v

 

If this command is run for the first time, the system will download gradle to the local device. The download path is:

Enter the preceding command and check gradle version 2.2.1, as shown in:

 

2. Use gradle to import the jar package:

In section 3rd of section 2 of the previous article, we talked about importing jar packages by copying files to the libs folder. How to configure the jar package through gradle this time. Take Google's gjson. jar as an example. If you have copied the file before, delete it by yourself.

1. Configure third-party jar packages through gradle

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

Let's take a look at the module named app. Its build. gradle graphic interface is actually the following Project Stucture:

Switch to the dependencies tab, as shown in:

Click Add and select "Library dependency". The following page is displayed:

, Enter "gson" in the search box, and then click OK. The jar package we need at the arrow is displayed, and you can add it:

Later, we will find that a line of code is added in build. gradle of the module of the app, indicating that gson. jar is introduced:

In fact, if you can remember the above line of code, you can directly write the code to import it.

At this point, the gson jar package no longer appears in the libs folder, but in the External Libraries at the bottom, as shown in: (and the latest version)

2. Features of importing a jar package using gradle: (the difference between it and importing a jar package using the libs folder)

  • It is more convenient to import the jar package to gradle, just one line of code can be done. Unlike the latter, you have to go to the official website for download.
  • If the official version of the jar package is updated, you only need to change the version number in build. gradle. You do not need to download it again on the official website.

 

3. Where is the jar package imported through gradle:

Note that not all jar packages can be imported using gradle. Where does the jar package imported through gradle come from? Explanation:

We found that there is a build. gradle file under the directory of the HelloWorld project, open it:

The "jcenter ()" in row 17th indicates that all jar packages imported through gradle are obtained from the central warehouse http://bintray.com/bintray/jcenter. If the required jar package does not exist on this website, it cannot be imported using gradle.

By the way, gradle 1.1.0 in classpath of row 8th is a gradle plug-in of android (also obtained from the central repository ). The gradle version we use is shown in:

The gradle path we downloaded is as follows:

Iii. Two Methods of signature packaging:

Note: The signature for the app we developed represents my own copyright. to upgrade the app in the future, you must use the same signature. A signature represents your identity (that is, the keystore). Multiple apps can use the same signature.

If you do not know what the signature means, Baidu. In eclipse, the signature method is: select the project, and select "export-android-export android application" for the email ",

1. Method 1: sign through Android Studio:

Select the module of the app and choose "Build-Generate signed apk" from the menu ":

The following page is displayed:

If you are using the signature for the first time, click the red box to create a new signature. If you have signed a file before, select the blue box to import it. Select the red box first:

Click "finish". You can see that Gradle is executing a assumerelease task at the bottom of Android Studio, as shown in:

After the signed apk is generated, a prompt is displayed:

 

 

2. Method 2: sign through the command line:

(1) Load Key Store:

First, delete the apk file signed in the first way. The second method is to sign the signature, that is, the command line method.

Open the Project Stucture graphical interface:

Select the module of the app, switch to the singning tab, click Add, generate the release signature information, and then click "OK ". Perform the following operations:

Switch to the Build Types tab, and select Signing config as "release" to configure the generated release signature.

After the operation, we can see that the build. gradle file of the module of the app has the following red box code:

Then, execute "build-clean Project" in the menu bar ":

(2) generate the real‑version apk:

Then enter the following command in the command line Terminal: (AS has integrated the command line Terminal into the software)

gradlew assembleRelease

If the operation is successful, the effect is as follows:

The generated signed apk is in the following position:

 

3. Why use the gradlew command instead of the gradle command:

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

Indicates the version information of gradle on which the HelloWorld project depends. The red line indicates that if our project does not contain gradle, the software will download gradle according to this url. I finally know why it will be so slow when I open the AS for the first time?

If we execute the gradlew command, we actually execute the gradle wrapper above and find the gradle 2.2.1 we have downloaded. If there are many projects, but the gradle versions of each project are different, I have to configure the gradle of each version to the environment variable and execute the gradlew command, this will be avoided.

Iv. BuildConfig file:

BuildConfig is a class automatically generated by IDE. It is stored in the gen directory (such as the R file) in elipse ). In Adroid Studio, the BuildConfig file is stored in app/build/generated/source/buildconfig/dubug/<packagename>/BuildConfig. java. As shown in:

V. Meanings of parameters in the build. gradle file in the module:

It is mainly the module build. gradle, as follows:

Row 01: apply plugin: 'com. android. application' indicates that this module is the module of the application.

Row 15: package name of the applicationId "com. smyhvae. helloworld" Application

Lines 16 and 17: Minimum and compiled versions that are backward compatible. Note: This information is no longer displayed in app/src/main/AndroidManifest. xml.

Line 23: you do not need to use the 24-line proguard file to confuse the code. In the release status, it is best to change it to true.


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

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.