Dependency management under the Androidstudio

Source: Internet
Author: User
Tags naming convention maven central jcenter

It is common to have third-party libraries in development, how do you manage these dependencies under Androidstudio? This is the purpose of this article.

Directory
    • Maven/ivy Warehouse Dependencies
    • Module dependency
    • AAR file Dependency
    • Jar File Dependency
    • Example complete code
One, Maven/ivy warehouse dependence

This relies on the widest range of applications in Androidstudio, and is one of the highlights compared to Eclipse+adt development. Let's give an example:

dependencies {  //omitted the default configuration    ' com.android.support:appcompat-v7:23.4.0'     com.jakewharton:butterknife:7.0.1//Other dependencies}    

Above is an example of a MAVEN warehouse dependency, which relies on the Compatibility Pack and Jakewharton big ' butter knives ', omitting other types of dependencies (as described later) to highlight the focus.

1) Configure the Warehouse

(This is the basic concept, you can choose to skip over) before configuring MAVEN dependencies we need to understand what a warehouse is (repositories) and how to configure a warehouse, what is a warehouse? The warehouse is plainly the place where the code is stored, it can be very good version control and access. There are two types of warehouses that are widely used: Ivy and Maven,ivy are widely used in ant-built systems, and are more famous than Ivy,maven warehouses. The MAVEN repository known to programmers now has Jcenter and Maven Central, both of which are implementations of the MAVEN repository. These two types of warehouses can be used in Androidstudio with Gradle as the build tool, and the use is simple, just add a few lines of code to the corresponding configuration file.

A. Configuring the Maven repository

This androidstudio under the new project, in the project root directory of the Build.gradle, another such a piece of code:

allprojects {    repositories {        jcenter ()    }}

This specifies the default repository for all module (including Appilcation module and library module, which will be followed by the concept of module) Jcenter. This is a question, and the same Maven repository why Androidstudio default is not maven central? Jcenter and Maven Central are both Maven libraries Yes, but Jcenter is a superset of MAVEN Central, which means you can find dependencies in Maven Jcenter, and vice versa. If you have to use Maven Central, just Change Jcenter () to Mavencentral (). However, it is not recommended to modify the build.gradle of the project, but to modify it in your module so that the scope of the impact can be controlled to a single module.

B. Import dependencies

After the warehouse is configured to import dependencies, it is also recommended to import the required libraries into the module's Build.gradle file instead of importing them in the project's Build.gradle. The warehouse to be imported is written into the dependencies code block, similar to the opening example. Tell me how the warehouse distinguishes between different libraries that have different versions of the same library, mainly by three elements: Group: library Name: Version number

    • Group name: Generally indicates the developer of the library, commonly used inverted domain name to represent, such as Com.jakewharton
    • Library Name: Describes the name of this library, such as Butterknife
    • Version number: The version number of the library

These three are separated, together make up the library's unique identity, so we import the library depends on the format is: Compile ' Group name: library Name: Version number ' start not necessarily compile, may be provided, APK, Testcompile, Androidtestcompile, they illustrate the addition of the library to that part of the compilation, specifically what, their own Google.

# # # C. Dynamic version Import what is dynamic version import? is to let gradle to you to download the latest library, not every time you have to modify the version number. As follows

dependencies {   ' com.android.support:support-v4:22.2.+'   com.android.support: appcompat-v7:22.2+' com.android.support:recyclerview-v7:+'}    

This involves a naming convention for the version number: the major version number. minor version number. Debug version number, different version number what do you mean Google. Explain what the above three dependencies mean:

    • Required to be the latest debug version
    • Requires a minimum of more than 2 minor versions
    • The latest library
2) configuration of other Maven/ivy warehouses

How does the MAVEN warehouse be configured for non-jcenter and maven central warehouses? How is ivy configured? As follows:

Repositories {  maven {    "http://baidu.com"//here fill in the warehouse address    credentials {         //Here fill in the certification information      ' user' Secretpassword'       }}

Similar to ivy configuration, just change maven to Ivy, and again, to keep the scope of the impact to a minimum, write this configuration to the module's Build.gradle file.

Second, module dependence

On Androidstudio, an application is made up of one or more module, one application module, 0 or more library module. The application module can finally be packaged into a apk,library module, which is packaged as a jar or an AAR, which can then be packaged into either a jars or a (different Google). In this section we'll talk about how to import the module that you or someone else wrote, and the next section on how to import a packaged AAR. Completing module dependencies requires only three steps to complete:

    • Copy Library to the root of the project, and the app module sibling
    • Add the module name to the Setting,gradle of the project. As follows
  ': App': Volley' 

With this step, you can see the imported items in the Android view of the Project navigator.

The app is Android-generated application Module,volley is the library module I imported. But this just tells the gradle to go to the manager two module, and can no longer apply the API in the app volley, need to complete the following steps to use.

    • Add Dependencies Declaration
dependencies {    //To highlight the focus, omit other    compile project (': Volley')}  

This allows the module to be imported successfully. Finally, how to create a new application module and library in Android studio. File->new->new module selects the module type in the pop-up dialog box, and the module's type start is based on the type of plug-in in its build.gradle. Application module:

Apply plugin' com.android.application' 

Library module:

Apply plugin' com.android.library' 
Iii. Arr File Dependency

The arr file is a packaged library module that shows how to introduce an AAR file into the project, first to say how to package the module to generate the Aar file, and after the Gradle widget such as Build->make module name is completed, An AAR file is generated in the path shown

Starting with how to add an AAR file to your project, it also requires three steps:

    • Copy the Aar file to the module directory, you can copy it to the Libs file, or any new folder

      I created a new folder called Aars, and then put the previously generated AAR file in.
    • Add a file warehouse add a code block to the module's Build.gradle
repositories{    flatdir{        ' Aars'    }} 
    • Add dependency
dependencies {    //Ignore other    compile (name:' volley-debug', ext:' aar')} 

This adds the Aar file dependency complete.

Iv. jar file Dependencies

Android Studo file Dependency is very simple, the jar package into the Libs file in the first place is OK. This is due to the default configuration of Androidstudio:

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

The phrase is to put all the jars in the Libs directory once all upside down. If you switch to compile file (jar file path), you can import only one jar package at a time.

Five, example complete code

Github-fallblank

Dependency management under the Androidstudio

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.