我有一个具有以下设置的应用程序项目:
android {
compileSdkVersion 21
buildToolsVersion '19.1.0'
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
}
buildTypes {
release {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
我有一个带有 try-with 资源的 block ,该 block 不使用检查 Build.SDK 是否低于 API 19。 我没有从 IDE 收到任何关于它的错误。 我在带有目标 SDK Android 4.1.2 的模拟器上运行了一个程序,一切正常,还在带有 Android 4.2.2 的设备上进行了检查。程序调用此代码,并使用调试器进行检查。 一切都顺利吗?我预计 IDE 可能存在兼容性错误,但事实并非如此。
如果我尝试在 IDE 中使用相同的 minVersionSdk 10 创建新项目,我会从 IDE 收到有关兼容性的错误。 但在我的工作项目中我不明白。 我不知道,除了build.gradle和AndroidManifest.xml之外,是否还有其他兼容性设置?为什么它可以在 API <19 上运行?
更新: 您需要检查 Android Studio 中的 Lint 设置。 编辑 - 检查 Android Lint,在旧版本上调用新方法
最佳答案
Why is it working on API < 19 ?
据此:https://code.google.com/p/android/issues/detail?id=73483它在 API 15 中“大部分工作”,并且您在 API 16 模拟器 (4.1.2) 上进行了测试。
问题提出者指出:
Since it was unhidden in API level 19, try-with-resources is backwards compatible down to API level 15.
Google 成员(member)回复(已编辑):
AutoCloseable
was in ics. and it's just an interface; it's javac that emits code to actually do the closing. iirc there are fewer classes that areAutoCloseable
in ics than we actually unhidso "backwards compatible" is a bit misleading. "mostly works" is closer to the truth.
由于不完全兼容,警告来自 API 19。
至于为什么您没有看到警告,我认为这取决于您非常旧的 buildToolsVersion
,它可以追溯到 2013 年 12 月。https://developer.android.com/tools/revisions/build-tools.html
You should always keep your Build Tools component updated by downloading the latest version using the Android SDK Manager
关于Android studio 检查与 API < 19 的兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32881671/