Android Studio项目整合PullToRefresh的问题记录

时间:2023-03-08 17:02:39

  PullToRefresh下拉刷新在App中应用非常频繁,然而PullToRefresh是在ADT下开发完成的。如果要将其整合到Android Studio目录下的话颇费周折。前面的文章“Android Studio项目引入外部库注意事项(PullToRefresh)”介绍了如何将PullToRefresh的library引入到项目中。如果想直接运用PullToRefresh的例子的话还需要引入extras下的PullToRefreshListFragment和PullToRefreshViewPager两个库。

  然而,PullToRefreshListFragment和PullToRefreshViewPager两个库的引入却大动干戈。其实问题不大,只是对Gradle不熟悉,折腾了好久才知道问题所在。记录如下:

  1、包重复引入

Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'D:\Program Files\Java\jdk1.8.0_60\bin\java.exe'' finished with non-zero exit value 2

Android Studio项目整合PullToRefresh的问题记录

  将android-support-v4.jar包删除以后又报如下异常:

D:\AndroidStudioProjects\AndoridNewsApp\PullToRefreshExtras\PullToRefreshListFragment\src\com\handmark\pulltorefresh\extras\listfragment\PullToRefreshBaseListFragment.java
Error:(19, 30) 错误: 程序包android.support.v4.app不存在
Error:(28, 106) 错误: 找不到符号
符号: 类 ListFragment
Error:(32, 2) 错误: 方法不会覆盖或实现超类型的方法
Error:(34, 17) 错误: 找不到符号
符号: 变量 super
位置: 类 PullToRefreshBaseListFragment<T>
其中, T是类型变量:
T扩展已在类 PullToRefreshBaseListFragment中声明的PullToRefreshBase<? extends AbsListView>

Android Studio项目整合PullToRefresh的问题记录

  解决办法:

    a、将extras\PullToRefreshListFragment\libs下的android-support-v4.jar删除

    b、添加包依赖( File --> project structure --> dependencies )

Android Studio项目整合PullToRefresh的问题记录

    也可以直接在extras\PullToRefreshListFragment\build.gradle中的dependencies下直接添加compile 'com.android.support:support-v4:23.0.1'

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

  2、最小sdk版本

Error:Execution failed for task ':PullToRefreshExtras:PullToRefreshListFragment:processDebugAndroidTestManifest'.
> java.lang.RuntimeException: Manifest merger failed : uses-sdk:minSdkVersion 1 cannot be smaller than version 4 declared in library [AndoridNewsApp:PullToRefresh:unspecified] D:\AndroidStudioProjects\AndoridNewsApp\PullToRefreshExtras\PullToRefreshListFragment\build\intermediates\exploded-aar\AndoridNewsApp\PullToRefresh\unspecified\AndroidManifest.xml
Suggestion: use tools:overrideLibrary="com.handmark.pulltorefresh.library" to force usage

  在extras\PullToRefreshListFragment\build.gradle中最小sdk版本设置

android {
......//其它配置 defaultConfig {
minSdkVersion 14
targetSdkVersion 23
} ......//其它配置
}

  最终extras\PullToRefreshListFragment\build.gradle如下:

apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0" defaultConfig {
minSdkVersion 14
targetSdkVersion 23
} sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['aidl']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
} dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':PullToRefresh')
compile 'com.android.support:support-v4:23.0.1'
}