如何添加一个库项目到Android工作室? [英] How do I add a library project to the Android Studio?

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

问题描述

我如何添加一个库项目(如夏洛克ABS)的 Android的工作室

(不老ADT基于Eclipse的捆绑,但到了新的的Andr​​oid工作室。)

解决方案

更新为Android工作室1.0

由于Android工作室1.0发布(和大量v1.0和从我的previous答案时首创的一个版本之间的)一些事情发生了变化。

我介绍的重点是通过摇篮文件添加外部库项目的手(为了更好地理解这个过程)。如果你想通过机器人工作室的创建者添加库只是检查回答下面视觉引导(有机器人工作室之间的一些型动物1.0和那些来自截图,但是这个过程是非常相似)。

在你开始用手工添加库到您的项目考虑增加外部依赖。它不会弄乱你的项目结构。几乎每一个知名的Andr​​oid库是Maven仓库可用,它的安装只需要一行code。在应用程序/ build.gradle 文件:

 相关性{
     编译com.jakewharton:butterknife:6.0.0
}
 

添加库

下面是增加外部的Andr​​oid库我们的项目的全过程:

  1. 通过机器人工作室的创建者创建新项目。我把它命名的的HelloWorld
  2. 在下面是Android的工作室创建的原始项目结构:

 的HelloWorld /
      应用程序/
            -  build.gradle //本地摇篮配置(用于应用程序只)
           ...
       -  build.gradle //全球摇篮配置(适用于整个项目)
       -  settings.gradle
       -  gradle.properties
      ...
 

<醇开始=3>
  • 在根目录下(的HelloWorld / )创建新的文件夹: /库中,我们会向我们的外部库(这一步是不是需要 - 只有保持清洁项目结构)。
  • 粘贴您的图书馆在新创建 /库文件夹。在这个例子中,我使用 PagerSlidingTabStrip库(只是从Github上下载ZIP,重命名库目录PagerSlidingTabStrip,并将其复制)。这是我们项目的新结构:
  •  的HelloWorld /
          应用程序/
                -  build.gradle //本地摇篮配置(用于应用程序只)
               ...
          库/
               PagerSlidingTabStrip /
                     -  build.gradle //本地摇篮配置(图书馆只)
           -  build.gradle //全球摇篮配置(适用于整个项目)
           -  settings.gradle
           -  gradle.properties
          ...
     

  • 编辑通过添加库 settings.gradle包括。如果使用自定义路径像我一样,你也可以定义为我们的图书馆项目目录。全settings.gradle应如下:

     包括:应用程序',':PagerSlidingTabStrip
    项目(:PagerSlidingTabStrip')PROJECTDIR =新的文件(库/ PagerSlidingTabStrip)。
     

  • 5.1如果您遇到默认配置的错误,然后尝试,而不是一步5本,

     包括:应用程序
    包括':库:PagerSlidingTabStrip
     

    <醇开始=6>

  • 应用程序/ build.gradle 添加我们的库项目作为一个依赖关系:

     相关性{
        编译文件树(导演:库,包括:['的* .jar'])
        编译com.android.support:appcompat-v7:21.0.3
        编制项目(:PagerSlidingTabStrip)
    }
     

  • 6.1。如果按照步骤5.1,然后按照这个来代替6,

     相关性{
        编译文件树(导演:库,包括:['的* .jar'])
        编译com.android.support:appcompat-v7:21.0.3
    
        编制项目(:库:PagerSlidingTabStrip)
    }
     

  • 如果你的库项目没有 build.gradle 文件,你必须手动创建它。下面是例子文件:

     应用插件:com.android.library
    
    依赖{
        编译com.android.support:support-v4:21.0.3
    }
    
    安卓{
        compileSdkVersion 21
        buildToolsVersion21.1.2
    
        defaultConfig {
            的minSdkVersion 14
            targetSdkVersion 21
        }
    
        sourceSets {
            主要 {
                manifest.srcFile的Andr​​oidManifest.xml
                java.srcDirs = ['src'中]
                res.srcDirs = ['水库']
            }
        }
    }
     

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

      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 文件使用它(在应用程序和库模块)如下图所示:

      // ...
    安卓{
        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)
        }
    }
    // ...
     

  • 这就是全部。只需点击,同步项目摇篮图标。你的图书馆应该在你的项目中可用。

  • ...

    (删除过时的描述在这里)

    ...

    在这里,您对建设有摇篮的Andr​​oid应用程序构建系统极大presentation - http://www.youtube。 COM /手表?ν= LCJAgPkpmR0 。正如哈维尔Ducrohet说

      

    Android的工作室是所有关于编辑,调试和分析。   这不是建立了。

    在开始的时候可能会有点混乱(特别是对那些,谁的作品与Eclipse,还从来没见过蚂蚁 - 像我一样;)),但最后摇篮给我们一些很好的机会,它值得你去学习这个版本系统中。

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

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

    解决方案

    Update for Android Studio 1.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.

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

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

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

    Adding the library

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

    1. Create new project via Android Studio creator. I named it HelloWorld
    2. Here is the original project structure created by Android Studio:

    HelloWorld/
          app/
               - build.gradle  // local gradle config (for app only)
               ...
          - build.gradle // global gradle config (for whole project)
          - settings.gradle 
          - gradle.properties
          ...
    

    1. In root directory (HelloWorld/) create new folder: /libs in which we’ll place our external libraries (this step is not required - only for keeping cleaner project structure).
    2. Paste your library in 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 config (for app only)
               ...
          libs/
               PagerSlidingTabStrip/
                    - build.gradle // local gradle config (for library only)
          - build.gradle // global gradle config (for whole project)
          - settings.gradle 
          - gradle.properties
          ...
    

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

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

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

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

    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. 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. 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']
              }
          }
      }
      

    2. Additionaly you can create global config 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
      

      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)
          }
      }
      //...
      

    3. That’s all. Just click ‚Sync project with gradle’ icon . Your library should be available in your project.

    ...

    (Removed outdated description here)

    ...

    Here you have great presentation about building Android apps with Gradle Build System - http://www.youtube.com/watch?v=LCJAgPkpmR0. As Xavier Ducrohet said

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

    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工作室?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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