【Android Studio安装部署系列】十7、Android studio引用第三方库、jar、so、arr文件

版权声明:本文为HaiyuKing原创文章,转载请注明出处!android

概述

在Android开发过程,常常须要用到第三方库以及jar、so、arr文件,那么如何引用到项目中呢?下面简单介绍下。架构

引用第三方库

通常按照第三方库做者提供的引用方式进行引用便可。app

好比,以引用okhttp-utils开源库为例:post

在module(好比app)的build.gradle文件中添加下面的代码

注意:旧版本Android studio的写法是compile 'com.zhy:okhttputils:2.6.2'gradle

新版本Android Studio的写法略有不一样:implementation 'com.zhy:okhttputils:2.6.2'ui

不过在新版本Android studio中是兼容旧版本的写法的。url

而后Sync Now 或者 Sync Project with Gradle Files

 

引用jar文件

其实当你在新建项目的时候就默承认以编译libs目录下的jar了,由于全部的module的build.gradle文件中含有下面的依赖:spa

因此只须要将引用的jar文件复制到module(为何是module,而不是app,由于jar文件不必定是放到app中,多是任意module中;并且app也是module的一种)的libs目录中便可。.net

将jar包复制到libs目录下

 Sync Project with Gradle Files

 

引用成功的效果

至此,只能算是将jar成功引用到baselibrary中了,也就是说只能在baselibrary中使用这个jar,而不能在app中使用(app中但是依赖这个baselibrary了)。那么如何实现依赖baselibrary的module中也能使用jar文件的方法呢?3d

在当前module的build.gradle文件的dependencies{}中添加如下代码【适用依赖当前module的其余module也须要使用这个jar文件的状况

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            postprocessing {
                removeUnusedCode false
                removeUnusedResources false
                obfuscate false
                optimizeCode false
                proguardFile 'proguard-rules.pro'
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //jsoup 必须compile,不然app那里找不到相关类 compile files('libs/gson-2.2.4.jar')

}

 Sync Project with Gradle Files

 

 

引用arr包

将arr包复制到module的libs目录下

在build.gradle中添加下面的代码

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.why.project.helloworld"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    //在layout文件夹下创建子文件夹
    sourceSets {
        main{
            res.srcDirs = [
                    'src/main/res/layout/home',
                    'src/main/res/layout',
                    'src/main/res'
            ]
        }
    }
}

repositories{ flatDir { dirs 'libs' } }

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation project(':baselibrary')

    compile 'com.zhy:okhttputils:2.6.2'

    compile(name: 'ijkplayer', ext: 'aar') compile(name: 'libp', ext: 'aar')
}

 Sync Project with Gradle Files

 

若是该arr文件是在module中引用的,那么app又依赖了这个module,那么若是不作任何处理的话,编译后会出现failed to resolve的错误提示

好比,我在baselibrary中引用了libnewurl.arr文件。

解决方案,在app(由于app依赖了这个baselibrary)的build.gradle文件中添加如下代码:

repositories{
    flatDir {
        dirs 'libs'
        dirs project(':baselibrary').file('libs')
    }
}

上面代码中的baselibrary是module的名字,libs是module依赖的aar库所在的目录。

 

引用so包

方案一【建议使用这个方案】

在 src/main/ 目录下建立文件夹 jniLibs(若是有就不须要建立了),将so文件复制到这个目录下便可,工程会自动加载src/main/jniLibs目录下的so动态库。

将so文件复制到jniLibs目录中

方案二

在libs目录下放入对应不一样CPU架构的so文件,经过在build.gradle文件的android{}中加入代码: jniLibs.srcDir 'libs' 来讲明so的路径为该libs路径。

    sourceSets {
        main {
            jniLibs.srcDir 'libs'
        }
    }

 

若是想要控制使用的CPU平台,则在build.gradle的defaultConfig下添加

        ndk {
            abiFilters "armeabi", "armeabi-v7a" //选择要使用的平台  ,"x86", "mips"
        }

若是编译不经过,在项目的gradle.properties中添加

android.useDeprecatedNdk=true

参考资料

Android Studio如何引用so、arr、jar包(by 星空武哥)

[Android]依赖了包含aar包的库后出现Failed to resolve库aar包问题的解决办法

相关文章
相关标签/搜索