如何将库项目添加到Android Studio? [英] How do I add a library project to Android Studio?

查看:81
本文介绍了如何将库项目添加到Android Studio?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将库项目(例如Sherlock ABS)添加到 Android Studio ?

How do I add a library project (such as Sherlock ABS) to Android Studio?

(不是旧的基于ADT Eclipse的捆绑包,而是新的 Android Studio .)

(Not to the old ADT Eclipse-based bundle, but to the new Android Studio.)

推荐答案

Android Studio 1.0更新

自从发布Android Studio 1.0(以及许多版本在v1.0到我上次回答之时的第一个)以来,某些事情已经发生了变化.

Since Android Studio 1.0 was released (and a lot of versions between v1.0 and one of the firsts from the time of my previous answer) some things has changed.

我的描述主要集中在通过Gradle文件手动添加外部库项目(以更好地理解过程).如果您想通过Android Studio创建者添加库,只需查看以下指南即可答案(Android之间存在一些差异Studio 1.0和屏幕快照中的内容,但过程非常相似.

My description is focused on adding external library project by hand via Gradle files (for better understanding the process). If you want to add a library via Android Studio creator just check the answer below with visual guide (there are some differences between Android Studio 1.0 and those from screenshots, but the process is very similar).

在开始手动向项目中添加库之前,请考虑添加外部依赖项.它不会弄乱您的项目结构. Maven 存储库中几乎提供了每个知名的Android库,其安装仅需一行app/build.gradle文件中的代码:

Before you start adding a library to your project by hand, consider adding the external dependency. It won’t mess in your project structure. Almost every well-known Android library is available in a Maven repository and its installation takes only one line of code in the app/build.gradle file:

dependencies {
     compile 'com.jakewharton:butterknife:6.0.0'
}

添加库

这是将外部Android库添加到我们的项目的完整过程:

Here is the full process of adding external Android library to our project:

  1. 通过Android Studio创建者创建一个新项目.我将其命名为.
  2. 这是Android Studio创建的原始项目结构:

HelloWorld/
      app/
           - build.gradle  // local Gradle configuration (for app only)
           ...
      - build.gradle // Global Gradle configuration (for whole project)
      - settings.gradle
      - gradle.properties
      ...

  1. 在根目录(HelloWorld/)中,创建一个新文件夹:/libs,我们将在其中放置外部库(不需要此步骤-仅用于保持更干净的项目结构).
  2. 将库粘贴到新创建的/libs文件夹中.在此示例中,我使用了 PagerSlidingTabStrip库(只需从GitHub下载ZIP,将库目录重命名为"PagerSlidingTabStrip"并复制它是我们项目的新结构:
  1. In the root directory (HelloWorld/), create new folder: /libs in which we’ll place our external libraries (this step is not required - only for keeping a cleaner project structure).
  2. Paste your library in the newly created /libs folder. In this example I used PagerSlidingTabStrip library (just download ZIP from GitHub, rename library directory to „PagerSlidingTabStrip" and copy it). Here is the new structure of our project:

HelloWorld/
      app/
           - build.gradle  // Local Gradle configuration (for app only)
           ...
      libs/
           PagerSlidingTabStrip/
                - build.gradle // Local Gradle configuration (for library only)
      - build.gradle // Global Gradle configuration (for whole project)
      - settings.gradle
      - gradle.properties
      ...

    通过将库添加到include
  1. 编辑settings.gradle.如果像我一样使用自定义路径,则还必须为我们的库定义项目目录.整个settings.gradle应该如下所示:

  1. Edit settings.gradle by adding your library to include. If you use a custom path like I did, you have also to define the project directory for our library. A whole settings.gradle should look like below:

include ':app', ':PagerSlidingTabStrip'
project(':PagerSlidingTabStrip').projectDir = new File('libs/PagerSlidingTabStrip')

5.1如果遇到默认配置"错误,请尝试执行此操作,而不是执行步骤5,

5.1 If you face "Default Configuration" error, then try this instead of step 5,

    include ':app'
    include ':libs:PagerSlidingTabStrip'

  1. app/build.gradle中,将我们的库项目添加为依赖项:

  1. In app/build.gradle add our library project as an dependency:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile project(":PagerSlidingTabStrip")
}

6.1.如果您按照第5.1步进行操作,请遵循此步骤(而不是6)

6.1. If you followed step 5.1, then follow this instead of 6,

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.3'

        compile project(":libs:PagerSlidingTabStrip")
    }

  1. 如果您的图书馆项目没有build.gradle文件,则必须手动创建它.这是该文件的示例:

  1. If your library project doesn’t have build.gradle file you have to create it manually. Here is example of that file:

    apply plugin: 'com.android.library'

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

    android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"

        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 21
        }

        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                res.srcDirs = ['res']
            }
        }
    }

  • 此外,您可以为项目创建全局配置,其中将包含SDK版本和每个模块的构建工具版本,以保持一致性.只需编辑gradle.properties文件并添加行:

  • Additionally you can create a global configuration for your project which will contain SDK versions and build tools version for every module to keep consistency. Just edit gradle.properties file and add lines:

    ANDROID_BUILD_MIN_SDK_VERSION=14
    ANDROID_BUILD_TARGET_SDK_VERSION=21
    ANDROID_BUILD_TOOLS_VERSION=21.1.3
    ANDROID_BUILD_SDK_VERSION=21
    

    现在,您可以在build.gradle文件(在应用程序和库模块中)中使用它,如下所示:

    Now you can use it in your build.gradle files (in app and libraries modules) like below:

    //...
    android {
        compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
        buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
    
        defaultConfig {
            minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
            targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
        }
    }
    //...
    

  • 仅此而已.只需单击与Gradle"图标同步项目.您的库应该在您的项目中可用.

  • That’s all. Just click‚ synchronise the project with the Gradle’ icon . Your library should be available in your project.

    Google I/O 2013-新型Android SDK构建系统 是有关使用Gradle Build System构建Android应用程序的精彩演讲:正如Xavier Ducrohet所说:

    Google I/O 2013 - The New Android SDK Build System is a great presentation about building Android apps with Gradle Build System: As Xavier Ducrohet said:

    Android Studio全部涉及编辑,调试和概要分析. 不再需要构建任何东西.

    Android Studio is all about editing, and debugging and profiling. It's not about building any more.

    一开始它可能有点令人困惑(特别是对于那些使用 Eclipse的人并从未见过像我这样的蚂蚁;)),但是最终Gradle给了我们很多机会,值得学习这个构建系统.

    At the beginning it may be little bit confusing (especially for those, who works with Eclipse and have never seen the ant - like me ;) ), but at the end Gradle gives us some great opportunities and it worth to learn this build system.

    这篇关于如何将库项目添加到Android Studio?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆