Android Gradle compilation learning Diary II (compiling Eclipse with Gradle, introducing dependency resources, and migrating Android Studio)

Source: Internet
Author: User
Tags maven central jcenter build gradle

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


Android Gradle compilation Learning Diary (based on Android Studio 1.0):

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


In the previous article, I briefly introduced a Gradle environment variable and how to compile the Android project using Gradle, but for most developers in the country, we usually use Eclipse for Android development. For most people, the cost of migrating from one platform to another is too high. So this article will show you how to compile the Android project under the Eclipse code structure with Gradle and migrate the Andorid project under Eclipse to Android Studio, making it easier to transfer project code.

One, create an Android project under Eclipse

I'm sure you don't want me to show you how to create an Android project under Eclipse, so I'm not spending time on it, first uploading a created project map:


One more Gradle. Default directory structure diagram:


By comparing the two directory structures, we can see that the default code directory structure of Eclipse and the Code directory structure supported by Gradle are different, so if we use the methods in the previous article to compile the Android project directly, we will find that we cannot compile properly. The reason is that the corresponding Android configuration file was not found.


Second, configure the resource path of Gradle

In order for Gradle to find the source and resource files of the response, we first look at the directory structure of the Gradle, and from the second graph we can find that the default compilation structure of Gradle in the program source code in several different parts: Java, JNI, RS, Aidl, and Eclipse directory where Java and aidl are placed in the same directory, so we need to define the corresponding path in the Build.gradle file according to the project structure.

The code that defines 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 ')    }}


Please add the Sourcesets section to the Android domain in Build.gradle.

In fact Gradle default source path settings should be at the outermost, but because the Andorid plug-in has its own code settings, so we will sourcesets placed in the Android domain.

After execution

Gradle Check


you should now be able to do the normal compilation work, specifically how to compile please check my previous article.


Iii. introduction of Project Dependency packages

You're making a mistake, aren't you?

Of course, this is possible, note the first picture, in addition to the Testgradle directory, there is a appcompat_v7, this is the project's dependency package, if you do not configure, then Android compile can not find him, will naturally error.

Now let's see how to introduce a dependency package into Android so that he can compile it properly

(1) using Maven

As I said before, Gradle can use almost all of Maven's features, so when we need to use a dependency package, and the dependency package exists in the MAVEN central repository, we might as well configure it directly in the configuration file, and the configuration method is very simple to appcompat_ V7 Package For example, add the following information to the outermost layer of build.gradle:

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

Compile means that you want to get a MAVEN package into the program at compile time, and the information behind it is relevant to the MAVEN package and is easily accessible from the MAVEN central repository.


Then you execute it again:
Gradle Check

Now is not a hint that your program can be packaged properly.

(2) Introduction of JAR Package

Sometimes we have a dependency on the Libs directory that contains the jar type or so-linked library type, but when you compile, you will find that the resources are not found for one reason: Gradle does not know where you need to compile the resource objects, so we need to manually build.gradle To configure the path, the approximate method is similar to the above, I would like to briefly explain.

Open your Build.gradle, in the dependencise domain, yes, just the domain where we added the APPCOMPA_V7 package, in the domain we add the code:

Compile Filetree (dir: ' Libs ', include: [' *.jar '])

The code introduces all the jar packages in the Libs directory, and if you want to introduce the so library, add a

Compile Filetree (dir: ' Libs ', include: [' */*.so '])

(3) Introduction of dependent projects

If you can't find a corresponding MAVEN project or simply use a jar package to introduce it, you need to use a dependency project, and then we'll have to consider the project association by converting the dependent project first to the Gradle type of Android Library.

In this case, we can put the application project and the dependency project in the same directory as Eclipse, so as to facilitate the association.

We will first remove the build.gradle in Testgradle about the AppCompat dependency package.

In the top level directory, we set up a build.gradle and Setting.gradle file to store the project dependencies separately .

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

Top-level build file where can add configuration options common to all Sub-projects/modules.buildscript {    repos itories {        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 here, we can name it like this:

Include ': Testgradle ', ': Appcompat_v7 '

Testgradle and Appcompat_v7 are the name of the application project and the dependent project, if your project is in a subdirectory, then you can use the form ": LIBRARIES:LIB1" to mark the Libraries folder. Lib1 folder is also a Gradle project

Then we go into the APPCOMP_V7 directory, set up the corresponding Build.gradle configuration file, because we have created a build.gradle in the upper directory and configured the common dependency information, so in this configuration file we can not have to write The related properties of the Buildscript.

Apply plugin: ' Com.android.library ' android {    compilesdkversion    buildtoolsversion "21.1.2"}

note here that the plugin type here is ' com.android.library ' to indicate that he is a dependent package . In addition, if your project configuration is the Eclipse directory type, please configure the corresponding source location according to the above.

As a result, you have already configured the dependent project, and then we need to configure the dependency project in the main project.

method to join in the main project Build.gradle

dependencies {    Compile project (': Appcompat_v7 ')}

You can then use the Gradle command line to compile against the local dependent libraries.


Iv. introduction of Engineering using Android Studio

Here's a brief talk on how to develop using the Eclipse's project directory structure through Android Studio.

In fact, the method is not complex, through the above steps we have successfully configured a Gradle project related environment, so we just need to click File->importproject, select Appcompat_v7 and testgradle the parent directory can be And then all the way Next, and then you can see that your project was introduced into Android Studio


In addition to the code structure, with the Android Studio built exactly like there is no!!!


This blog post is here to focus on how to fully import Eclipse projects into Android Studio by establishing a Gradle profile. Hope you like, see here the point of praise good.

Android Gradle compilation learning Diary II (compiling Eclipse with Gradle, introducing dependency resources, and migrating Android Studio)

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.