简体   繁体   中英

Pure Java 8 Library module in Android Studio/Gradle Project

I have an Android Studio project with a pure Java 8 library module. Here is the complete gradle.build file for that library:

apply plugin: 'java-library'

dependencies {
    testImplementation 'junit:junit:4.12'
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"

The code is tested and works fine.

The problem I am having is that I have built the project, and then tried to use the built .jar file in another Android project that doesn't uses Java 7 (not 8). Obviously, the code does not work. However, I am surprised that there are no compile-time errors indicating that the library is not compatible. Instead, I only first notice the incompatibility when a runtime function is called.

Am I doing something wrong in how I am compiling the Java module? Or is it expected behavior that you will only get runtime errors that the library is not compatible? I expected compile-time errors.

Also, please tell me how to compile properly so that I can get compile-time errors in Android Studio indicating that a library was compiled with a newer Java version.

For completeness, I am getting the following runtime error when I use my Java 8 library module

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/util/Base64;

java.util.Base64 added in API level 26 ( https://developer.android.com/reference/java/util/Base64.html ). You must use android.util.Base64 ( https://developer.android.com/reference/android/util/Base64 .)

For universal library I use Google Guava .

<dependency>
   <artifactId>guava</artifactId>
   <groupId>com.google.guava</groupId>
   <type>jar</type>
   <version>14.0.1</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM