4

Hello I'm still kinda new to Android,

at the moment I have a big problem with Local Date(or any other Date Format I tried)

LocalDate currentDate = LocalDate().now();

This line produces following error:

Call requires API Level 26 (current min is 21)

I perfectly understands what this means, but I can't change the API Level of the project (due to the fact that I singed a contract with my teacher that me and a partner will make our school project in excact that way we planned it.)

How can I use any kind of Date with API Level 21?

Gradle :

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'

}
CodeChris B
  • 139
  • 1
  • 10
  • Does this answer your question? [cannot resolve symbol 'java.time.LocalDate' error in android studio](https://stackoverflow.com/questions/28745205/cannot-resolve-symbol-java-time-localdate-error-in-android-studio) – Ole V.V. Mar 15 '20 at 07:15
  • Or [this](https://stackoverflow.com/questions/49348769/offsetdatetime-now-requires-api-26) or [this](https://stackoverflow.com/questions/54828832/zoneddatetime-to-date-before-java-8-in-early-android) or [this](https://stackoverflow.com/questions/58556621/what-method-can-be-used-to-get-the-accurate-date-and-time-in-the-region)? – Ole V.V. Mar 15 '20 at 07:18

1 Answers1

20

Add this to your build.gradle file:

android {
  compileOptions {
    coreLibraryDesugaringEnabled true
  }
}

Newer versions of the Android Gradle plugin (starting with 4.0) are able to use desugaring to backport new APIs to older versions of Android. You can read more about it here: https://jakewharton.com/d8-library-desugaring/

Note that you'll have to use the Beta release of Android Studio to access these features: https://developer.android.com/studio/preview

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • I work in a team with other people does everyone of them now needs this beta version? – CodeChris B Mar 14 '20 at 22:47
  • 1
    I believe so, yes. Another option is to use the popular [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) library. That won't literally be the Java 8 version of the library, but the API is basically exactly the same. – Ben P. Mar 14 '20 at 22:48
  • Thank you I marked this as the correct anwser I guess I try the ThreeTenABP, because the other persons in the team would kill me if I would say they have to install a diffrent version. Thank you Ben P – CodeChris B Mar 14 '20 at 22:51