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

Source: Internet
Author: User
Tags maven central jcenter

Android Gradle compilation learning diary 2 (Use Gradle to compile Eclipse, introduce dependent resources, and migrate Android Studio), 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 ):

 

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


In the previous article, I briefly introduced how to build a Gradle environment variable and how to use Gradle to compile Android projects. However, for most developers in China, we usually use Eclipse for Android development. For most people, the cost of migrating from one platform to another is too high. Therefore, this article will introduce how to compile the Android project under the Eclipse code structure through Gradle, and migrate the Android project under Eclipse to Android Studio, so that you can easily transfer the project code.

1. Create an Android project under Eclipse

I believe that you do not want me to demonstrate how to create an Android project under Eclipse, so I will not spend any time on it. First upload a created project structure:


Here is the default directory structure of Gradle:


By comparing the two directory structure charts, we can find that the default code directory structure of Eclipse is different from the code directory structure supported by Gradle. Therefore, if we use the method in the previous article to directly compile the Android project, we will find that the normal compilation fails because the corresponding Android configuration file is not found.


2. Configure the Gradle resource path

To enable Gradle to find the response source file and resource file, let's take a look at the Gradle directory structure first, from the second figure, we can find that the default compilation structure of Gradle divides the program source code into several different parts: java, jni, rs, aidl, in the Eclipse directory, java and aidl are stored in the same directory, so we need to build. the gradle file defines the corresponding path according to the project structure.

The code for defining the path location is as follows:

android {    sourceSets {        main {            manifest.srcFile 'AndroidManifest.xml'            java.srcDirs = ['src']            resources.srcDirs = ['src']            aidl.srcDirs = ['src']            renderscript.srcDirs = ['src']            res.srcDirs = ['res']            assets.srcDirs = ['assets']        }        androidTest.setRoot('tests')    }}


Add the sourceSets part to the android domain in build. gradle.

In fact, the default source code path of Gradle should be set in the outermost layer, but because the Andorid plug-in has its own code settings, we place sourceSets In the android domain.

Then run

gradle check


Now you should be able to perform normal compilation. For details about how to compile, please refer to my previous article.


3. Introduce the project dependency package

What? You have made an error. Isn't it possible?

Of course, this is actually possible. Check the first figure. Besides the TestGradle directory, there is appcompat_v7, which is the project dependency package. If you do not configure it, if Android cannot find it during compilation, an error is reported.

Now let's take a look at how to introduce the dependency package to Android so that it can be compiled normally.

(1) Use maven

As I mentioned earlier, Gradle can use almost all the features of Maven. Therefore, when we need to use a dependency package and the dependency package exists in the maven Central Library, we may wish to directly configure it in the configuration file. The configuration method is also very simple. The appcompat_v7 package is used as an example in build. add the following information to the outermost layer of gradle:

dependencies {    compile 'com.android.support:appcompat-v7:21.0.3'}

Compile indicates that you want to inject a maven package into the program during compilation, and the following information is the relevant information of the maven package, which can be easily obtained from the maven central repository.


Then run the following command:
gradle check

Is it a prompt that your program can be properly packaged.

(2) introduce the jar package

Sometimes we have dependent resources in the libs directory that store the jar package type or the so Link Library type, but when you compile, you will find that these resources cannot be found for only one reason: gradle does not know where the resource object you need to compile is, so we need to manually build it. the path configuration of gradle is roughly similar to the above method. I just want to briefly describe it.

Open your build. gradle in the dependencise domain. That's right. It's the domain where we just added the appcompa_v7 package. Let's add the code in the domain:

compile fileTree(dir: 'libs', include: ['*.jar'])

This Code introduces all the jar packages in the libs directory. If you want to introduce the so library, add

compile fileTree(dir: 'libs', include: ['*/*.so'])

(3) Introduce dependency Projects

If you cannot find the corresponding maven project or simply use the jar package for introduction, you need to use a dependency project, then we need to consider converting the dependent project to the Gradle type Android Library before project Association.

In this case, we can put application projects and dependency projects in the same directory like Eclipse to facilitate Association.

We will first remove the reference to the appcompat dependency package in build. gradle in TestGradle.

In the top-level directory, weCreate a build. gradle and setting. gradle file to store project dependencies respectively..

Build. gradle is used to store general configuration information, such as general dependency:

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:1.0.0'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}allprojects {    repositories {        jcenter()    }}

Setting. gradle is used to store project information. For example, we can name it like this:

include ':TestGradle',':appcompat_v7'

TestGradle and appcompat_v7 are the names of application projects and dependent projects respectively, If your project is in a subdirectory, you can use a format similar to ': libraries: lib1' to indicate that the lib1 folder under the libraries folder is also a Gradle project.

Then we enter the appcomp_v7 directory and create the corresponding build. the gradle configuration file, because we have created a build in the upper directory. gradle is configured with common dependency information. Therefore, in this configuration file, we do not have to write the relevant attributes of buildscript.

apply plugin: 'com.android.library'android {    compileSdkVersion 21    buildToolsVersion "21.1.2"}

Note that, Here, the plug-in type is 'com. android. library', indicating that it is a dependent package.. In addition, if your project configuration is of the Eclipse directory type, configure the corresponding source code location based on the above.

As a result, you have configured the relevant dependency project. Next we need to configure this dependency project to the main project.

The method is to add it to build. gradle of the main project.

dependencies {    compile project(':appcompat_v7')}

Then you can use the Gradle command line to compile the local dependent libraries.


4. Introduce the project using Android Studio

Here is a brief description of how to use the Eclipse project directory structure for development through Android Studio.

In fact, the method is not complex. Through the above steps, we have successfully configured a Gradle project environment, so we only need to click File-> ImportProject, select the parent directory of appcompat_v7 and TestGradle, and click Next. Then, you can see that your project has been introduced to Android Studio.


Except for the code structure, is it exactly the same as that created by Android Studio !!!


This article introduces how to import the Eclipse project to Android Studio by creating the Gradle configuration file. I hope you will like it. You can enjoy it here.

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.