Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

android - 如何将库项目添加到Android Studio?(How do I add a library project to Android Studio?)

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

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

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

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

  ask by Alexander Kulyakhtin translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Update for Android Studio 1.0

(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.

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

My description is focused on adding external library project by hand via Gradle files (for better understanding the process).

(我的描述着重于通过Gradle文件手动添加外部库项目(以更好地理解过程)。)

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

(如果您想通过Android Studio创建者添加库,只需通过视觉指南检查以下答案 (Android Studio 1.0与屏幕截图有所不同,但过程非常相似)。)

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:

(Maven存储库中几乎提供了每个知名的Android库,其安装仅需在app/build.gradle文件中使用一行代码:)

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

Adding the library

(添加库)

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

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

  1. Create a new project via Android Studio creator.

    (通过Android Studio创建者创建一个新项目。)

    I named it HelloWorld .

    (我将其命名为HelloWorld 。)

  2. Here is the original project structure created by Android Studio:

    (这是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. 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).

    (在根目录( HelloWorld/ )中,创建一个新文件夹: /libs ,我们将在其中放置外部库(不需要此步骤-仅用于保持更简洁的项目结构)。)

  2. Paste your library in the newly created /libs folder.

    (将您的库粘贴到新创建的/libs文件夹中。)

    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:

    (在此示例中,我使用了PagerSlidingTabStrip库 (只需从GitHub下载ZIP,将库目录重命名为“ PagerSlidingTabStrip”并复制它),这是我们项目的新结构:)

 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 ... 
  1. Edit settings.gradle by adding your library to include .

    (通过添加库编辑settings.gradle 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:

    (整个settings.gradle应该如下所示:)

     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,

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

    include ':app'
    include ':libs:PagerSlidingTabStrip'
  1. In app/build.gradle add our library project as an dependency:

    (在app/build.gradle添加我们的库项目作为依赖项:)

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

6.1.

(6.1。)

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

(如果您执行了步骤5.1,请遵循此步骤(而不是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.

    (如果您的库项目没有build.gradle文件,则必须手动创建它。)

    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. 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.

    (另外,您可以为项目创建全局配置,其中将包含SDK版本和每个模块的构建工具版本,以保持一致性。)

    Just edit gradle.properties file and add lines:

    (只需编辑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 

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

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

     //... 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? synchronise the project with the Gradle' icon

    (只需单击“与Gradle”图标同步项目) 与Gradle同步 .

    (。)

    Your library should be available in your project.

    (您的库应该在您的项目中可用。)

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:

(Google I / O 2013-新的Android SDK构建系统是有关使用Gradle构建系统构建Android应用的精彩演示:正如Xavier Ducrohet所说:)

Android Studio is all about editing, and debugging and profiling.

(Android Studio全部涉及编辑,调试和性能分析。)

It's not about building any more.

(它不再是要构建的。)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...