Android Studio项目引入外部库注意事项(PullToRefresh)

时间:2024-10-30 08:36:32

  Android Studio开发App项目时引入第三方库是个比较麻烦的事情。之前导入Volley就折腾了好久,导入下拉刷新控件PullToRefresh时又碰到了各种问题。在此记录一下,以便查阅。

  一、导入PullToRefresh

  1、将PullToRefresh克隆到本地

git clone https://github.com/chrisbanes/Android-PullToRefresh

  2、克隆到本地后library目录复制到app同级目录下,并将名称改为PullToRefresh

  3、在项目根目录下的settings.gradle下添加第三方库目录

include ':app', ':PullToRefresh'

  4、在app/build.gradle下添加编译依赖

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.1'
compile project(':PullToRefresh'
)
}

  5、在PullToRefresh目录下添加build.gradle

apply plugin: 'android-library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['aidl']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}

  6、最后别忘记Sync Now和Rebuild(Build-->Rebuild Project)

Android Studio项目引入外部库注意事项(PullToRefresh)

  二、例子:

    可查看sample目录下的例子,也可查看测试例子:https://github.com/chrisbanes/Android-PullToRefresh/wiki/Quick-Start-Guide

  三、常见问题:

  1、路径不对

14:59:54 Gradle sync failed: Configuration with name 'default' not found.
Consult IDE log for more details (Help | Show Log)

  Android Studio项目引入外部库注意事项(PullToRefresh)

  导致这种问题是由于

    a、settings.gradle包含了不正确的目录:include ":目录名"

    b、app目录下build.gradle文件目录不正确:compile project(':目录名')

    c、引入的第三方库下是否包含build.gradle文件

  2、编译器版本不对

Error:A problem occurred configuring project ':PullToRefresh'.
> failed to find target with hash string 'android-17' in: D:\Users\ibm\AppData\Local\Android\sdk
<a href="openAndroidSdkManager">Open Android SDK Manager</a>

  这只需要将第三方库中build.gradle下编译器版本改成和工程项目的一致即可。

  如,项目的编译器版本为

    compileSdkVersion 23
buildToolsVersion "23.0.0"

  如果下称如下版本,则会出现以上异常。

    compileSdkVersion 17
buildToolsVersion "21.1.2"

  更多内容见:https://github.com/chrisbanes/Android-PullToRefresh/