Android Studio:添加库作为参考,而不是导入

时间:2015-02-28 11:09:31

标签: android android-studio android-library

这个问题是this one的续集。

在现有的Eclipse解决方案中,我有一个库项目,在主应用程序项目中用作Android Dependency。这意味着如果我编辑这个库并进行编译,主项目将使用该库中的最新版本进行更新。

我正在尝试在Android Studio中执行相同的操作。这意味着不导入库,而是将其用作应用程序项目中的引用或依赖项。我已经尝试了两天而没有运气。非常感谢帮助。

build.gradle (应用项目的内容 - 没有什么有趣的,因为我没有改变它)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

2 个答案:

答案 0 :(得分:1)

您可以尝试以下步骤:

  1. 下载图书馆
  2. 文件/导入模块
  3. 选择您的库并重命名:LibraryName
  4. 转到build.gradle并编写。
  5. <强>的build.gradle

    dependencies {
    ......
    compile project(':LibraryName')
    .....
    }
    

    然后,如果导入模块,您可以编辑库并对其更新进行更新

答案 1 :(得分:1)

假设您有2个项目,1个应用程序和1个库项目,您希望将所有外部依赖项推送到库项目,因此您的应用程序项目仅依赖于库项目:

<强> settings.gradle

include ':app'
include ':library'

应用/的build.gradle

apply plugin: 'com.android.application'

dependencies {
    compile project(':library')
}

<强>库/的build.gradle

apply plugin: 'android-library'

dependencies {
    compile 'external-dependencies-1'
    compile 'external-dependencies-2'
    ...
}