Android Studio添加依赖方式

《Android Studio添加依赖方式》 添加依赖图

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)

    原文作者:翻译不了的声响
    原文地址: https://www.jianshu.com/p/b6704aa3b6b6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞