One of the Android Gradle compilation learning Diaries (building the Gradle environment and compiling Android applications), androidgradle

Source: Internet
Author: User
Tags maven central

One of the Android Gradle compilation learning Diaries (building the Gradle environment and compiling Android applications), androidgradle

If you like my blog, please pay attention to my Weibo, please click here (http://weibo.com/kifile), thank you

Reprinted please indicate the source (http://blog.csdn.net/kifile), thank you again


Android Gradle compilation learning diary (based on Android Studio 1.0 ):

Android Gradle compilation learning diary 2 (Use Gradle to compile Eclipse, introduce dependent resources, and migrate Android Studio)

 

Google recently officially launched Android Studio 1.0. The default development tool on the developer homepage has also been changed to Android Studio. I think it is time for us to complete the transformation to Android Studio development.


In fact, aside from the interface factors, the main difference between Android Studio and Eclipse ADT in building Android applications is the change in the build tool. Eclipse is built through ant, and Android Studio is built through Gradle.


First, let's take a look at why Google chose Gradle for project construction:

The use of automated build tools can help us reduce the time wasted in file compilation. Currently, there are three commonly used automated build tools: ant, maven, and gradle.

Maven has a huge advantage over ant, that is, maven has a large number of third-party resource libraries. You can introduce various resource packages from the Internet through configuration build files, while ant requires

Gradle is actually equivalent to an incremental package of maven. It can use all maven resource libraries, and its syntax for building files is simpler and easier to start. In addition, it is very simple to use Gradle for multi-channel packaging, which is also a very important factor for us to use Gradle.


Now let's build our first Android project from scratch.


1. Build the Gradle compiling environment

First we need to download the latest Gradle package from Gradle's official website (http://www.gradle.org), the latest Gradle version is 2.2.1

After the download is complete, decompress the package and we will start to configure the environment variables of Gradle.

On Linux and Mac Unix-like systems, you can use your environment variable files, such as/etc/profile ,~ /. Configure in bashrc. The related configuration information is as follows:

export GRADLE_HOME=/Users/kifile/Applications/gradlePATH=${PATH}:${GRADLE_HOME}/bin

For Windows environment variable configuration, please query it by yourself. I will not detail it here.

After environment variables are configured, run the following command on the terminal:

source /etc/profile

Change/etc/profile to the environment variable file you have changed.

After the command is executed, enter

gradle -v
The following output is displayed:


It indicates that you have successfully configured the Gradle environment variable.


After the Gradle environment variable is configured, we needConfigure the Android Environment VariablesThe Code is as follows:

export ANDROID_HOME=/Users/kifile/Applications/android-sdk-macosxexport ANDROID_NDK_HOME=/Users/kifile/Applications/android-ndk-r10b

The purpose of configuring these two environment variables is to tell Gradle where your current Android path is, so as to find the corresponding compilation tool


2. Create a simple Android Project

(1) construct the Android directory structure

For a basic Android project, its directory structure should be as follows:


The java directory is used to store the java source code, and the res directory is used to store resource files (of course, we can set the path for storing the source code, but for a basic example, we can use the default structure of the Android project.), build. the gradle file is used to store project building information.

(2) configure the build. grade File

For Android projects, the build. grade file style is as follows:

buildscript{repositories {jcenter()}dependencies {classpath 'com.android.tools.build:gradle:1.0.0'}}apply plugin: 'com.android.application'android {compileSdkVersion 21buildToolsVersion "21.1.1"}


Buildscript declares that it uses maven's central repository to obtain dependency packages, and uses Android Gradle compilation Tool 1.0 for compilation.

If one line is applied, the plug-in type we use is android.

The android tag defines the relevant project information, such as the compiled sdk version and the compiled tool version number.

(3) Configure AndoridManifest. xml

The AndroidManifest. xml file follows the basic Android configuration. It is just a test. My source file is as follows:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.kifile.library.download" >        <application>    </application>    </manifest>

(4) Start Compilation

Before compilation, we first enter the project's top-level directory through the terminal, that is, the android directory in the above directory structure.

After entering the directory, we will execute

gradle check
Check whether the Gradle build runs normally. During the first running, you will find that it takes a long time because it needs to go to the maven central repository to obtain the dependency package, com. android. tools. build: gradle: 1.0.0-rc1

After the check is complete, the following figure is displayed if the check is successful:


The yellow UP-TO-DATE code does not need TO be re-compiled if some resources have not been changed.

After the check is completed, we execute:

gradle tasks

We can see that Gradle currently supports the following compilation types:


Here, the Build task part is a task that you can Build.

gradle assembleDebug
Compile a Debug application. After compilation, SUCCESSFUL is displayed. You can view the compiled apk file in the build/outputs/Apk directory.


Congratulations, you have successfully completed your first Gradle compilation android Application.

OK. I will write it here for the moment, and add the rest.

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.