Android Studio添加依赖方式

本文详细介绍了Android Studio中四种依赖管理方式:文件依赖、库依赖、Jar依赖和Module依赖的具体操作步骤,包括如何导入so文件、aar文件、jar包及模块,以及不同版本AS的配置差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载于https://www.jianshu.com/p/b6704aa3b6b6

AS不同于Eclipse的配置 Build Path,AS既可以通过图形界面 Project Structure 来配置 Dependencies,还可以通过 gradle.build 脚本来配置。


AS中添加依赖方式有:库依赖(Library dependency)、Jar依赖(Jar dependency)、Module依赖(Module dependency)以及文件依赖(File dependency)

1. 文件依赖

  • 导入so文件

(1) 在app/src/main的目录下新建名为 jniLibs 文件夹(app/src/main/jniLibs);
(2) 再将so文件复制、粘贴到 jniLibs 目录内;
(3) 在项目gradle.properties文件中加上以下代码,表示我们要使用NDK进行开发:

android.useDeprecatedNdk = true

(4) 在项目local.properties中配置ndk和sdk的路径:

ndk.dir=D\:\\NDK\\android-ndk-bundle   // NDK路径
sdk.dir=D\:\\SDK\\android-sdk-bundle  // SDK路径

(5) 在app下的build.gradle里添加配置:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.example.activitytest"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 10
        versionName "1.0"        
    }
    
    ndk {
      moduleName"myNativeLib" //生成的so文件名字,调用C程序的代码中会用到该名字
      ldLibs "log", "z", "m"
      abiFilters "armeabi", "armeabi-v7a", "x86" //输出指定三种平台下的so库
    }
    
     buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}
  • 导入aar文件
    (1) 将aar文件复制、粘贴到app/libs目录中
    (2) 修改app下的build.gradle配置文件
    AS 3.0之前:
    ...
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile(name:'pullrefreshlibrary', ext:'aar') //引用第三方库
        ...  
    }  
    repositories {
       flatDir {
          dirs 'libs'
       }
    }

AS 3.0之后:

   ...
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        testImplementation 'junit:junit:4.12'
        implementation 'com.android.support:appcompat-v7:25.0.0'
        implementation(name:'pullrefreshlibrary', ext:'aar') //引用第三方库
        ...  
    }  
  ...

(3) 同步更新Gradle (Sync now)

2. 库依赖(导入library)

(1) 项目根目录下的build.gradle里repositories节点下添加仓库地址
AS 3.0之前:

buildscript {
    repositories {   
        //依赖的仓库
        jcenter()
    }
    dependencies {        
        //项目依赖的Gradle版本
        classpath 'com.android.tools.build:gradle:2.3.3'
    }
}

allprojects {
    repositories {    
        jcenter()
        //远程仓库
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

AS 3.0之后:

buildscript {
    repositories {
        google()
        //依赖的仓库
        jcenter()
    }
    dependencies {        
        //项目依赖的Gradle版本
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        //远程仓库
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

(2) 在app下的build.gradle里dependencies节点下添加引用
AS 3.0之前:

    ...
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile 'com.google.code.gson:gson:2.8' //引用第三方module库(Github上开源项目)
        ...  
    }

AS 3.0之后:

   ...
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        testImplementation 'junit:junit:4.12'
        implementation 'com.android.support:appcompat-v7:25.0.0'
        implementation 'com.google.code.gson:gson:2.8' //引用第三方module库(Github上开源项目)
        ...  
    }

(3) 同步更新Gradle (Sync now)

3. Jar依赖(导入jar包)

方法一:
(1) 将jar文件复制、粘贴到app/libs目录中
(2) 右键点击jar文件,并点击弹出菜单中的 Add As Library,将jar文件作为类库添加到项目中

方法二:
(1) 将jar文件复制、粘贴到app/libs目录中
(2) 在app下的 build.gradle里的dependencies节点下添加引用
AS 3.0之前:

    ...
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile files('libs/gson-2.8.jar')//引用gson解析架包
        ...
    } 

AS 3.0之后:

  ...
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        testImplementation 'junit:junit:4.12'
        implementation 'com.android.support:appcompat-v7:25.0.0'
        implementation files('libs/gson-2.8.jar')//引用gson解析架包
        ...
    } 

(3) 同步更新Gradle (Sync now)

4. Module依赖(导入module)

(1) 项目根目录下settigs.gradle里添加第三方库名(例如:testlibrary

:include ':app',':testlibrary'

(2) 在app下的build.gradle里dependencies节点下添加引用
AS 3.0之前:

    ...
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile project(':library')//引用第三方项目
        ...  
    }

AS 3.0之后:

  ...
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        testImplementation 'junit:junit:4.12'
        implementation 'com.android.support:appcompat-v7:25.0.0'
        implementation project(':library')//引用第三方项目
        ...  
    }

(3) 同步更新Gradle (Sync now)



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值