为什么在现代Android开发中要使用sourceCompatibility和targetCompatibility?

huangapple go评论63阅读模式
英文:

Why use sourceCompatibility and targetCompatibility in modern Android development?

问题

我正在尝试我的 build.gradle.kts 文件。看起来,当 isCoreLibraryDesugaringEnabled = true 时,sourceCompatibility 和 targetCompatibility 似乎不会影响任何内容。代码在 Android 5.1 和 13 上都可以编译和运行良好。我可以删除 sourceCompatibility 和 targetCompatibility 并忘记使用它们吗?

英文:

I'm experimenting with my build.gradle.kts file. It looks like sourceCompatibility and targetCompatibility don't affect anything when isCoreLibraryDesugaringEnabled = true. The code compiles and runs fine on both Android 5.1 and 13. Can I remove sourceCompatibility and targetCompatibility and forget about using them?

答案1

得分: 0

在现代Android开发中,sourceCompatibilitytargetCompatibility设置用于Android项目的build.gradle文件,指定了Java语言版本的兼容性。

  • sourceCompatibility:此设置指定了项目源代码的Java版本兼容性。它确定了您的代码中可以使用的Java语言特性的版本。它确保您的代码与指定的Java版本兼容,并防止使用该版本不支持的语言特性。

  • targetCompatibility:此设置指定了Java编译器生成的字节码的Java版本兼容性。它确定了您编译的代码可以在哪个Java运行时环境(JRE)上运行的版本。它允许您指定比源代码的兼容性级别更低的Java版本,以实现与较旧的JRE的向后兼容性。

这些设置有助于确保您的代码与所需的Java版本一起工作,从而在保持与较旧的Android设备和运行时的兼容性的同时,可以使用新功能。

从Android Gradle插件7.0.0开始,在大多数情况下不再需要sourceCompatibilitytargetCompatibility设置。插件会根据Android Gradle插件版本和项目配置中指定的Android API级别自动确定适当的兼容性。但是,如果需要的话,仍然可以显式设置这些值,例如,如果您希望强制执行与特定Java版本的兼容性,或者如果遇到任何兼容性问题。

英文:

In modern Android development, the sourceCompatibility and targetCompatibility settings are used in the build.gradle file of an Android project specifies the Java language version compatibility.

  • sourceCompatibility: This setting specifies the Java version compatibility for the source code of your project. It determines the version of the Java language features you can use in your code. It ensures that your code is compatible with the specified Java version and prevents the use of language features that are not supported by that version.

  • targetCompatibility: This setting specifies the Java version compatibility for the bytecode generated by the Java compiler. It determines the version of the Java runtime environment (JRE) that your compiled code can run on. It allows you to specify a lower Java version than your source code's compatibility level, enabling backward compatibility with older JREs.

These settings help ensure your code works with the desired Java version, enabling the use of new features while maintaining compatibility with older Android devices and runtimes.

Starting with Android Gradle Plugin 7.0.0, the sourceCompatibility and targetCompatibility settings are not required in most cases. The plugin automatically determines the appropriate compatibility based on the Android Gradle Plugin version and the Android API level specified in your project configuration. However, you can still explicitly set these values if needed, for example, if you want to enforce compatibility with a specific Java version or if you encounter any compatibility issues.

huangapple
  • 本文由 发表于 2023年6月5日 04:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定