PDFView 是一个用于在 Android 应用中显示 PDF 文档的库。它提供了丰富的功能和灵活的配置选项,使得开发者能够轻松地在应用中嵌入 PDF 阅读器。
一、 添加依赖
- 在模块的 build.gradle 文件中添加以下依赖:
	// pdf
    implementation("com.github.barteksc:android-pdf-viewer:3.2.0-beta.1")
二、添加 阿里云 Maven 仓库
- 此时编译时会报错,提示无法找到该库:
* What went wrong:
Execution failed for task ':app:mergeDebugNativeLibs'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Could not find com.github.barteksc:android-pdf-viewer:3.2.0-beta.1.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/com/github/barteksc/android-pdf-viewer/3.2.0-beta.1/android-pdf-viewer-3.2.0-beta.1.pom
       - https://repo.maven.apache.org/maven2/com/github/barteksc/android-pdf-viewer/3.2.0-beta.1/android-pdf-viewer-3.2.0-beta.1.pom
     Required by:
         project :app
- 在项目的 settings.gradle.kts 文件中增加 阿里云 Maven 仓库 支持。
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        // 阿里云 Maven 仓库
        maven { url = uri("https://maven.aliyun.com/repository/google") }
        maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }
        maven { url = uri("https://maven.aliyun.com/repository/public") }
        maven { url = uri("https://maven.aliyun.com/repository/central") }
    }
}
三、解决库冲突问题
- 下载 android-pdf-viewer 依赖库后,编译提示 “com.android.support” 库冲突:
* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable
   > Duplicate class android.support.v4.app.AppLaunchChecker found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)
     Duplicate class android.support.v4.app.FrameMetricsAggregator found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)
     Duplicate class android.support.v4.app.FrameMetricsAggregator$FrameMetricsApi24Impl found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)
     Duplicate class android.support.v4.app.FrameMetricsAggregator$FrameMetricsApi24Impl$1 found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)
     Duplicate class android.support.v4.app.FrameMetricsAggregator$FrameMetricsBaseImpl found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)
     Duplicate class android.support.v4.app.FrameMetricsAggregator$MetricType found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)
     Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.13.1.aar -> core-1.13.1-runtime (androidx.core:core:1.13.1) and support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)
     Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.13.1.aar -> core-1.13.1-runtime (androidx.core:core:1.13.1) and support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)
     Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.13.1.aar -> core-1.13.1-runtime (androidx.core:core:1.13.1) and support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)
     Duplicate class android.support.v4.app.NavUtils found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)
     Duplicate class android.support.v4.app.TaskStackBuilder found in modules support-compat-28.0.0.aar -> support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) and support-core-utils-26.1.0.aar -> support-core-utils-26.1.0-runtime (com.android.support:support-core-utils:26.1.0)
     ......
     Duplicate class androidx.versionedparcelable.VersionedParcelParcel found in modules versionedparcelable-1.1.1.aar -> versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0.aar -> versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)
     Duplicate class androidx.versionedparcelable.VersionedParcelStream found in modules versionedparcelable-1.1.1.aar -> versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0.aar -> versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)
     Duplicate class androidx.versionedparcelable.VersionedParcelStream$FieldBuffer found in modules versionedparcelable-1.1.1.aar -> versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0.aar -> versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)
     Duplicate class androidx.versionedparcelable.VersionedParcelable found in modules versionedparcelable-1.1.1.aar -> versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0.aar -> versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)
     Duplicate class androidx.versionedparcelable.VersionedParcelize found in modules versionedparcelable-1.1.1.aar -> versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0.aar -> versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)
错误分析
- 这个错误是由于新增的依赖库中包含了Android Support Library 的依赖,跟项目中使用的 AndroidX 发生了类冲突。从 Android 9.0(API 级别 28)开始,Android Support Library 已被废弃,Google 推荐使用 AndroidX 来替代。
解决方法
- 在项目的 gradle.properties 文件中增加自动迁移配置,此时重新编译,即可编译通过:
	android.useAndroidX=true
	android.enableJetifier=true
- android.useAndroidX=true:启用 AndroidX。
- android.enableJetifier=true:启用 Jetifier 工具,自动迁移项目中的第三方库依赖。
四、配置文件权限
1、确保应用有权限读取 PDF 文件,在 AndroidManifest.xml 中添加以下权限:
	<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
	<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
2、如果文件存储在外部存储中,还需要动态申请权限:
	// 运行时权限
    private val requestPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted ->
        if (isGranted) {
            // 权限被授予
            Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show()
        } else {
            // 权限被拒绝
            Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
        }
    }
    
    /**
     * 动态申请权限
     */
    private fun requestPermission1() {
        requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
    }
3、手动申请 “所有文件访问权限”
- 从 Android 10(API 级别 29)开始,引入了分区存储(Scoped Storage),限制了应用对外部存储的访问权限。只有 READ_EXTERNAL_STORAGE 权限,无法打开 PDF 文件,还需要申请 MANAGE_EXTERNAL_STORAGE 权限,并且手动引导用户手动开启 “所有文件访问权限”。
	/**
	* 申请 所有文件访问权限
	*/
	private fun requestManagerPermission() {
	   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
	       if (!Environment.isExternalStorageManager()) {
	           val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
	           val applicationId = "com.example.helloworld"//BuildConfig.APPLICATION_ID
	           intent.data = "package:$applicationId".toUri()
	           startActivity(intent)
	       }
	   }
	}
五、PDFView 常用方法
- fromFile(File file):从文件路径加载 PDF 文件。
	val filePath = "$filesDir/example.pdf"
	pdfView.fromFile(new File(filePath ))
	      .load();
- fromAsset(String assetName):从 assets 目录(res/main/assets/example.pdf)加载 PDF 文件。
	pdfView.fromAsset("example.pdf")
      .load();
-  fromUri(Uri uri):从 URI 加载 PDF 文件。 
-  defaultPage(int page):设置默认显示的页面。 
-  enableSwipe(boolean swipe):启用或禁用滑动翻页。 
-  enableDoubletap(boolean doubletap):启用或禁用双击放大。 
-  zoom(float scale):设置 PDFView 的初始缩放比例。 
-  rotate(int degrees):设置 PDFView 的初始旋转角度。 
-  onPageChange(OnPageChangeListener listener):设置页面改变时的监听器。 
-  onError(OnErrorListener listener):设置错误监听器。 
-  enableAnnotationRendering(boolean enabled):启用注释渲染。 
-  setOffscreenPageLimit(int limit):设置预加载页数,避免 OOM 问题。 



















