Android鬼点子-引入第三方依赖的无痛指南 | GreendaMi'Blog
在Android开发中,使用第三方的依赖是已经不可避免的事,毕竟世界上已经有了轮子,为什么还要自己造车轮?无论是在Eclipse还是Android Studio里面,引入第三方依赖都是一件很简单的事情,但是这位里面还有一些细节需要注意,防止为后面的开发埋下坑。这里着重说的是AS,Eclipse已经是时候说再见了。
1.在Android Studio中引入jar包
在module下创建一个libs文件夹,然后把jar包放进去(拷贝进去),然后右键点击这个jar文件,在出现的菜单中点击“Add as Library…”。这样就会在该module的build.gradle里面出现下面的设置。
compile files('libs/badgeview.jar')当然,也可以在build.gradle中直接输入下面的内容
compile fileTree(include: ['*.jar'], dir: 'libs')这样就会把libs文件夹下面的所有jar文件引入。如果是arr包,也是类似的配置。
compile fileTree(dir: 'libs', include: '*.aar')除此之外还有另一种方法,右键module根目录,进入“Open Module Setting F12”,进入到下图。
左边选择添加到哪个Module,右边选择“Dependencies”选项卡,点击加号。会出现3个选项,“Library Dependency”——远程依赖,这里会打开一个搜索框进行搜索;“File Dependency”——文件依赖,就是我们说的jar包,这里会让选择你要导入的jar包,所以最好在module下创建一个libs文件夹,然后把jar包放进去,再在这里选择;“Module Dependency”——项目依赖,可以选择一个本地项目作为依赖,前提是这个项目的类型是Library。
我们注意到右边还有一个选项
compile是对所有的build type以及favlors都会参与编译并且打包到最终的apk文件中。
Provided是对所有的build type以及favlors只在编译时使用,类似eclipse中的external-libs,只参与编译,不打包到最终apk。
APK是只会打包到apk文件中,而不参与编译,所以不能再代码中直接调用jar中的类或方法,否则在编译时会报错
Test compile是仅仅是针对单元测试代码的编译编译以及最终打包测试apk时有效,而对正常的debug或者release apk包不起作用。
Debug compile是仅仅针对debug模式的编译和最终的debug apk打包。
Release compile是仅仅针对Release 模式的编译和最终的Release apk打包。
完成上面的步骤,在build.gradle中就会出现和上图一致的配置了。
2.在Android Studio中引入远程依赖
引入远程依赖同样可以在Open Module Setting中配置,也可以在build.gradle中直接写。下面是我在项目中用到远程依赖的配置。
dependencies { compile project(':greendami') //compile fileTree(include: ['*.jar'], dir: 'libs') compile files('libs/badgeview.jar') compile files('libs/butterknife-7.0.1.jar') compile files('libs/easemobchat_2.2.0.jar') compile files('libs/pinyin4j-2.5.0.jar') compile files('libs/weiboSDKCore_3.1.4.jar') compile files('libs/Native_Libs2.jar') compile 'com.squareup.retrofit2:adapter-rxjava:+' compile 'com.squareup.retrofit2:converter-gson:+' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3' compile 'com.squareup.okhttp3:okhttp:3.0.1' compile 'org.greenrobot:eventbus:3.0.0' compile 'au.com.gridstone.rxstore:rxstore:4.0.0' compile 'au.com.gridstone.rxstore:converter-gson:4.0.0' compile 'io.reactivex:rxandroid:1.1.0' compile 'io.reactivex:rxjava:1.1.0' compile 'com.joanzapata.android:base-adapter-helper:1.1.11' compile 'com.squareup.okhttp3:logging-interceptor:+' compile 'com.android.support:design:23.4.0' compile 'com.github.chrisbanes:PhotoView:+' compile files('libs/libammsdk.jar') compile files('libs/AMap_Location_v1.4.1_20150917.jar')}compile project(‘:greendami’)这里是引用的一个本地Module。
compile ‘com.squareup.retrofit2:adapter-rxjava:+’这里后面的“ + ”的意思是使用最新版本。
compile ‘com.android.support:design:23.4.0’的“23.4.0”这里是给出了固定的版本。
远程依赖有冲突的时候,即多个远程依赖,都依赖了同一个的话,可以使用下面的方法去掉。举个例子“com.android.support:design:23.4.0”这里的“com.android.support”是group,“design”是module。
compile('com.google.api-client:google-api-client:1.17.0-rc') {// Exclude artifacts that the Android SDK/Runtime provides. exclude(group: 'xpp3', module: 'xpp3') exclude(group: 'org.apache.httpcomponents', module: 'httpclient') exclude(group: 'junit', module: 'junit') exclude(group: 'com.google.android', module: 'android')}在Module的build.gradle中,中配置了远程库的位置,有3个,会按照先后顺序去查找。
allprojects { repositories { jcenter() mavenCentral() maven { url "https://jitpack.io" } }}在Project的build.gradle中,也有一个这样的配置,当Module没有配置远程库的位置的时候,Project的配置生效。
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.2.2' }}做好了以上的配置,记得要Gradle同步一下,红圈中左边第一个。
Project的视图模式来查看的时候点开External Libraries,这里面就是所有的compile的包了
3.在Android Studio中引入SO库
so文件应该放在哪里?在build.gradle中进行了说明。
sourceSets { main { jniLibs.srcDirs = ['src/main/jniLibs'] aidl.srcDirs = ['src/main/aidl'] } }src/main/jniLibs,在这里。在这个文件夹下面可以收x86,arm64-v8a等文件夹,对应不同的平台。这里Android Studio与Eclipse有所不同。