Android Gradle One of the compilation Learning Diaries (build Gradle environment and compile Android app)

Source: Internet
Author: User

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

Reprint please indicate the source (http://blog.csdn.net/kifile), thanks again

Google recently launched the Android Studio 1.0 release, the Developer homepage of the default development tools have been changed to Android studio, I think we are the time to fully transition to the development of Android studio.


In fact, the main difference between Android studio and Eclipse ADT in building Android apps is the change in the build tool, which is built through Ant, and Android Studio is built with Gradle for the project.


First, let's look at why Google chooses Gradle to build the project:

Using automated build tools can help us reduce the time wasted in compiling files, and there are three types of automated build tools available today, Ant, Maven, and Gradle.

One of the great advantages of maven over Ant is that Maven has a large, three-party repository of resources that you can use to introduce various resource packs from the Web, and ant needs to

Gradle is actually equivalent to one of MAVEN's incremental packages, it can use all of Maven's repositories, and the syntax of the build file he uses is simpler and easier to get started with. And Gradle is a very simple multi-channel package, which is a very important factor in our use of Gradle.


So now we start from scratch and build our first Android project


First, build Gradle build 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, the decompression is no longer said, then we begin to configure GRADLE environment variables.

On Linux and Mac class Unix systems you can configure in your environment variable files, such as/etc/profile, ~/.BASHRC, with the following configuration information:

Export Gradle_home=/users/kifile/applications/gradlepath=${path}:${gradle_home}/bin

For the environment variable configuration of Windows please check it yourself, I will not go into detail here.

After configuring the environment variable, execute the following command in the terminal:

Source/etc/profile

Note Change the/etc/profile to your own changed amount environment variable file

After executing the command, you enter in the terminal

Gradle-v
can see the following output:


It means you've successfully configured the GRADLE environment variable.


After configuring the GRADLE environment variable, we also need to configure the environment variables of Android , as above, the 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, in order to find the corresponding compilation tool


Second, build a simple Android project

(1) Build Android directory structure

For one of the most basic Android projects, his directory structure should look like this:


Java directory is used to store Java source code, res directory for the storage of resource files (of course, we can set the path of the source code, but for a most basic example, we are using the Android project default structure is good), Build.gradle file for the project build information

(2) Configuring the Build.grade file

For Android projects, his Build.grade file style is as follows:

Buildscript {repositories {mavencentral ()}dependencies {classpath ' Com.android.tools.build:gradle:1.0.0-rc1 '}} Apply plugin: ' Android ' Android {compilesdkversion 21buildToolsVersion "21.1.1"}

Buildscript stated that he would use Maven's central repository to get the dependency package and compile with the 1.0 version of the Android Gradle compilation tool

The Apply line defines the type of plugin we use for Android

The android tag is defined for the relevant project information, such as the compiled SDK version, and the compiled tool version number

(3) Configuration Andoridmanifest.xml

Androidmanifest.xml file just follow the most basic Android configuration, anyway, 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 compiling

Before compiling, we first enter the project's top-level directory through the terminal, which is the Android directory in the above directory structure.

After entering the directory, we execute the

Gradle Check
Check to see if the Gradle build works, and you'll find it takes a long time to run for the first time because he needs to go to the MAVEN central repository to get the dependency package, which is the com.android.tools.build described above in Buildscript: Gradle:1.0.0-rc1

After the check is complete, if successful, a diagram similar to the following is displayed:


Where the yellow up-to-date code part of the resource has not been changed without recompiling.

After the check is complete, we execute:

Gradle Tasks

We can see that he has shown the type of compilation that Gradle currently supports:


Where build tasks is part of the task you can build, we'll simply execute

Gradle Assembledebug
to compile a Debug type of application, after the compilation is complete, display successful, you can see in your directory corresponding to the path build/outputs/apk the compiled APK file


Congratulations, you've succeeded. Completed your first Gradle compiled Android app

Ok, I'll write it here for the time being, and then make up the rest.

Android Gradle One of the compilation Learning Diaries (build Gradle environment and compile Android app)

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.