简体   繁体   English

Android Studio / Gradle Project中的纯Java 8库模块

[英]Pure Java 8 Library module in Android Studio/Gradle Project

I have an Android Studio project with a pure Java 8 library module. 我有一个带有纯Java 8库模块的Android Studio项目。 Here is the complete gradle.build file for that library: 这是该库的完整gradle.build文件:

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). 我遇到的问题是我已经构建了项目,然后尝试在另一个不使用Java 7(而不是8)的Android项目中使用构建的.jar文件。 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? 我是如何编写Java模块的? 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. 另外,请告诉我如何正确编译,以便我可以在Android Studio中获得编译时错误,表明库是使用较新的Java版本编译的。

For completeness, I am getting the following runtime error when I use my Java 8 library module 为了完整起见,我在使用Java 8库模块时遇到以下运行时错误

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 ). 在API级别26( https://developer.android.com/reference/java/util/Base64.html添加了java.util.Base64 You must use android.util.Base64 ( https://developer.android.com/reference/android/util/Base64 .) 您必须使用android.util.Base64https://developer.android.com/reference/android/util/Base64 。)

For universal library I use Google Guava . 对于通用库,我使用Google Guava

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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