【问题标题】:Android - Date in API Level 21 [closed]Android - API 级别 21 中的日期 [关闭]
【发布时间】:2020-06-26 11:50:18
【问题描述】:

您好,我对 Android 还是有点陌生​​,

目前我对本地日期(或我尝试过的任何其他日期格式)有一个大问题

LocalDate currentDate = LocalDate().now();

此行产生以下错误:

Call requires API Level 26 (current min is 21)

我完全理解这意味着什么,但我无法更改项目的 API 级别(因为我与老师签订了一份合同,我和一个合作伙伴将按照我们的方式完成我们的学校项目计划好了。)

如何在 API 级别 21 中使用任何类型的日期?

分级:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.fragment"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "0.5"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

        }
    }
    android {
        compileOptions {
            coreLibraryDesugaringEnabled true
        }
    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-database:19.2.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'com.firebase:firebase-client-android:2.5.2'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
    implementation 'me.dm7.barcodescanner:zxing:1.9'
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:22.0.0'
    implementation 'com.google.code.gson:gson:2.8.6'

}

【问题讨论】:

标签: android api date


【解决方案1】:

将此添加到您的 build.gradle 文件中:

android {
  compileOptions {
    coreLibraryDesugaringEnabled true
  }
}

较新版本的 Android Gradle 插件(从 4.0 开始)能够使用脱糖功能将新 API 反向移植到较旧版本的 Android。你可以在这里阅读更多信息:https://jakewharton.com/d8-library-desugaring/

请注意,您必须使用 Android Studio 的 Beta 版才能访问这些功能:https://developer.android.com/studio/preview

【讨论】:

  • 我和其他人一起工作,现在每个人都需要这个测试版吗?
  • 我相信是的。另一种选择是使用流行的ThreeTenABP 库。这不会是 Java 8 版本的库,但 API 基本完全相同。
  • 谢谢我把它标记为正确的答案我想我试试 ThreeTenABP,因为如果我说他们必须安装不同的版本,团队中的其他人会杀了我。谢谢你本P
猜你喜欢
  • 2021-06-01
  • 2021-01-03
  • 2021-07-19
  • 2019-11-11
  • 1970-01-01
  • 2015-04-16
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多