Android Studio中依赖Jar详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ygc973797893/article/details/55254337

Android Studio中依赖Jar详解

Compile
compile是对所有的build type以及favlors都会参与编译并且打包到最终的apk文件中。

用法:
方式:1:它就会自动把这个包下载下来,并且引用它。节省Git空间,而且修改版本也很方便。
compile 'com.android.support:support-v4:23.3.0'

方式2:引用libs下所有jar包
compile fileTree(dir: 'libs', include: ['*.jar'])

方式3:引用一个jar
compile files('libs/fastjson-1.1.53.android.jar')

方式4:引用一个aar文件,注意并不能像 方式2 那样自动引用全部的aar,而需要对每个aar分别进行引用。
compile(name: 'aar_file_name', ext: 'aar')

方式5:引用库类型的项目
compile project(':xxxsdk')

Provided
Provided是对所有的build type以及favlors只在编译时使用,类似eclipse中的external-libs,只参与编译,不打包到最终apk。

用法:
仅仅在编译时使用,但最终不会被编译到apk或aar里
provided files('libs/glide-3.7.0.jar')

多个Module中依赖同一个jar解决方案

如 环信Module和自己app的Module都要用到定位sdk

1、在自己app的gradle中以compile引入如:
compile files('libs/AMap_Location_V2.4.1_20160414.jar')

2、在环信的Module的gradle中以provided的方式引入如:
provided files('libs/AMap_Location_V2.4.1_20160414.jar')

3、而且环信的gradle中不能存在compile fileTree(include: ['*.jar'], dir: 'libs')

APK
只会打包到apk文件中,而不参与编译,所以不能再代码中直接调用jar中的类或方法,否则在编译时会报错

Test compile
Test compile 仅仅是针对单元测试代码的编译编译以及最终打包测试apk时有效,而对正常的debug或者release apk包不起作用。

Debug compile
Debug compile 仅仅针对debug模式的编译和最终的debug apk打包。

Release compile
Release compile 仅仅针对Release 模式的编译和最终的Release apk打包。

猜你喜欢

转载自blog.csdn.net/ygc973797893/article/details/55254337