51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Android Studio添加jar或aar依赖的两种方法

第一种 {#第一种}

  1. 引用libs目录下所有的jaraar
    直接在dependencies加入以下代码,可以将libs目录下的所有jar和aar文件导入

    |-----------|---------------------------------------------------------------------------| | 1 | implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar']) |

    此种方式简单粗暴,但是不允许同时出现同一模块不同编译类型的jar(或aar),例如:如果libs同时存在test-release.aartest-debug.aar,他们都是来自同一个module的打包,但是只是编译类型不一样,如果同时存在的话会编译不过。

  2. 特定引用libs目录下的名字为xxx的jar包

    |---------------|---------------------------------------------------------------| | 1 2 3 | dependencies { implementation files('libs/xxx.jar') } |

第二种 {#第二种}

  1. 在模块的gradle中添加目标位置

    |-----------------------|---------------------------------------------------------------| | 1 2 3 4 5 6 7 | android { repositories { flatDir { dirs: 'libs' } } } |

  2. 在dependencies中添加

    |-------------|-------------------------------------------------------------------------------------| | 1 2 | implementation(name:'xxx', ext:'aar') implementation(name:'xxx', ext:'jar') |

    或者

    |---------------|---------------------------------------------------------------| | 1 2 3 | dependencies { implementation files('libs/xxx.aar') } |

完整版 {#完整版}

在module如上依赖就会产生问题,所以按照如下方法:

allprojects中加入flatDir,然后再去项目中引用即可。

|------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | allprojects { repositories { google() mavenCentral() jcenter() flatDir { dirs project(':base_lib').file('libs') //base_lib为引入aar文件的module名 } } gradle.projectsEvaluated { tasks.withType(JavaCompile) { options.compilerArgs.add('-Xbootclasspath/p:base_lib/libs/framework.jar') } } } |

|---------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | apply plugin: 'com.android.application' android { compileSdkVersion 27 defaultConfig { applicationId "com.noahedu.testtouch" minSdkVersion 22 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' } } repositories{ flatDir{ dirs 'libs' } } } dependencies { implementation (name:'app-release',ext:'aar') implementation(name: 'appcompat-v7-25.3.1', ext: 'aar') //support v4 implementation files('libs/support-annotations-25.3.1.jar') implementation files('libs/support-compat-25.3.1.jar') implementation files('libs/support-core-ui-25.3.1.jar') implementation files('libs/support-fragment-25.3.1.jar') implementation files('libs/support-media-compat-25.3.1.jar') implementation files('libs/support-core-utils-25.3.1.jar') //support v7 implementation files('libs/support-vector-drawable-25.3.1.jar') implementation files('libs/support-animated-vector-drawable-25.3.1.jar') } |


赞(0)
未经允许不得转载:工具盒子 » Android Studio添加jar或aar依赖的两种方法