欢迎来到 Android人的问与答 ,在这里提问或帮助他人解答。 Question2Answer的1.6.x和1.7.x版本的中文翻译包我修改更新:

Question2answer

如何使用中文语言包

Q2A 1.6.x - Chinese Simplified (简体中文)

Q2A 1.7.x - Chinese Simplified (简体中文)


国内服务器推荐 阿里云服务器
国外服务器推荐 DigitalOcean
VPN推荐 正版Green VPN
阿里云推荐码:ldvq50

如何添加一个库工程到Android Studio

0 喜欢 0 不喜欢
174 浏览
It is whole process of creating HelloWorld with external library (ActionBarSherlock in this case):

Create new project (named HelloWorld in my case)
Now you have file structure with root dir "HelloWorld", and subdir "HelloWorld" (module named "HelloWorld - HelloWorld").
Create new dir "libraries" in root directory (HelloWorld/libraries after creation).
Download ActionBarSherlock, upack it and copy "actionbarsherlock" directory to HelloWorld/libraries (HelloWorld/libraries/actionbarsherlock after this operation).
Now go to Project Structure (⌘ + ; on mac).
Go to Modules and add new one by pressing + and Import module
Pick your actionbarsherlock directory. Then "next" "next" .... "finish".
Now in Actionbarsherlock module delete android-support-library (because this is second copy of this library - it will cause build errors).
In HelloWorld-HelloWorld module add module dependency by clicking "+" on bottom panel. And pick Module dependency -> actionbarsherlock
Now go to Global libraries and add android-support-library from HelloWorld/libs to all projects (HelloWorld and actionbarsherlock). Thanks that you have one, the same support library in all projects.
And now your IDE is well configured. But also you have to configure Gradle separately.

In root folder open settings.gradle and replace it by:

include ":libraries:actionbarsherlock", ':HelloWorld'
In HelloWorld folder edit build.gradle by adding one line in dependencies:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile project(":libraries:actionbarsherlock")
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}
At the end you have to create another build.gradle file in /libriaries/actionbarsherlock/ directory. This is the source:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android-library'

dependencies {
    compile files(':HelloWorld/libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}
Why this code? You can find some good answers here: http://tools.android.com/tech-docs/new-build-system/user-guide

And now everything should work fine.

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.
最新提问 12月 31, 2014 分类:Android Studio | 用户: forlong401 (7,050 分)

登录 或者 注册 后回答这个问题。

...