Java 8 Source Compatibility

PSPDFKit library started using Java 8 language features in version 5.

Java 8 has been supported natively since Android SDK 26 and Android Studio 3.0. If your minimal SDK version is lower than 26, .class files produced by the javac compiler need to be converted to bytecode that is supported by these SDK versions. This process is called desugaring. You’ll still need to update to Android Studio 3.0 or later for full Java 8 support in your IDE.

PSPDFKit, similar to other third-party libraries, ships with .class files that are not yet desugared. The desugaring process has been supported since Android Gradle plugin 3.0.0 and runs only when your application also uses Java 8. This means that in order to integrate PSPDFKit 5 into your application that is using a minimum SDK version lower than 26, you’ll need to perform the following steps:

  1. Update your Android Gradle plugin to version 3.0.0 or higher:
Copy
build.gradle
1
2
3
4
5
6
7
8
9
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
    }
}
  1. Change Source Compatibility and Target Compatibility for each module that uses PSPDFKit to 1.8:
build.gradle
1
2
3
4
5
6
7
android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Note: Desugaring is enabled by default when using Android Gradle plugin version 3.0.0 or higher. However, it can be disabled with the following property in your gradle.properties file:

1
android.enableDesugar=false

Please make sure to remove this property or set it to true to enable desugaring.

Dexing Error Messages

You should look for the following error messages after updating to PSPDFKit 5 or above. All of these can be fixed by enabling desugaring for PSPDFKit via the steps outlined above:

1
D8: Invoke-customs are only supported starting with Android O (--min-api 26)
Copy
1
2
3
4
5
6
7
8
9
Caused by: com.android.builder.dexing.DexArchiveBuilderException: Error while dexing.
The dependency contains Java 8 bytecode. Please enable desugaring by adding the following to build.gradle
android {
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}
See https://developer.android.com/studio/write/java8-support.html for details. Alternatively, increase the minSdkVersion to 26 or above.