-1

I upgraded butterknife to 10.1.0 which throws this error:

Execution failed for task ':app:mergeDexDebug'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
   > Failed to transform artifact 'butterknife-runtime.aar (com.jakewharton:butterknife-runtime:10.1.0)' to match attributes {artifactType=android-dex, dexing-is-debuggable=true, dexing-min-sdk=19}
      > Execution failed for DexingTransform: /Users/<REMOVED>/.gradle/caches/transforms-2/files-2.1/65b6816001722128222b6880d16907c6/jars/classes.jar.
         > Error while dexing.

So I added to build.gradle:

compileOptions {
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
}

However, the minSdkVersion is 19, but Java 8 requires API >= 26 if I am not mistaking.

Just for test, when I try to use an Java 8 feature like stream the compiler shows an error as expected:

Call requires API level 24 (current min is 19): java.util.Collection#stream

The projects compiles without error, but will butterknife just crash the app during runtime on a low Android version trying to use Java 8 language features?

Manuel
  • 14,274
  • 6
  • 57
  • 130
  • What does that mean regarding my question? – Manuel Jul 01 '19 at 16:19
  • So if I am not using Jack there won't be any issues? – Manuel Jul 01 '19 at 16:32
  • Yes, that I understand, but what if a library uses stream, will Android detect that and throw a compile time error? – Manuel Jul 01 '19 at 16:42
  • Derek, I am not using stream. But what if a lib uses it, will Lint detect that? – Manuel Jul 01 '19 at 17:07
  • Still don't get it, `butterknife` uses Java 8 features (apparent by the error message), but when I enable Java 8 features through `sourceCompatibility`, there is no warning when `minSdkVersion` is 19? How can `butterknife` use Java 8 features but not require a `minSdkVersion` > 19? – Manuel Jul 01 '19 at 23:26
  • It should be the same, what you wrote below is a constant of value 1.8. – Manuel Jul 02 '19 at 10:17
  • Derek, I am not sure where you are you going with this; setting the compatibility in Android Studio > File > Project Structure to "Java 8" adds the entries in my post. While the enum value causes an `Unresolved` error in the dialog. How does this all answer my original question? – Manuel Jul 02 '19 at 13:34
  • hi manuel, so can you find a way to solve the lint problems? i want to find the newapi lint errors while i added the sourceCompatibility with java8 – Colibrow Aug 01 '19 at 13:10
  • @Colibrow I don't see this Lint warning anymore, unfortunately I don't recall what exactly helped. I am setting `compileSdkVersion 28` and everything works. – Manuel Aug 01 '19 at 15:04

1 Answers1

-1

However, the minSdkVersion is 19, but Java 8 requires API >= 26 if I am not mistaking.

Android Studio 3.0+ natively supports Java 8 and Jack was used to provide Java 8 support to earlier Android studio releases.

To start supporting Java 8 in your project, You should add following in your build.gradle (I assume you are already using Android Studio 3.0+).

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
 }

Note: According to Android Java 8 support documentation

If Android Studio detects that your project is using Jack, Retrolambda, or DexGuard, the IDE uses Java 8 support provided by those tools instead. However, consider migrating to the default toolchain.

Now, as you say you are using stream api, it would be supported on Android 24+ only.

The full support list for Java 8 features and apis as follows:

  • Lambda expressions: Any. Note that Android does not support the serialization of lambda expressions.
  • Method references: Any.
  • Type annotations: Any. However, type annotation information is available at compile time, but not at runtime. Also, the platform supports TYPE in API level 24 and below, but not ElementType.TYPE_USE or ElementType.TYPE_PARAMETER.
  • Default and static interface methods: Any.
  • Repeating annotations: Any.
  • java.lang.annotation.Repeatable: API level 24 or higher.
  • AnnotatedElement.getAnnotationsByType(Class): API level 24 or higher.
  • java.util.stream: API level 24 or higher.
  • java.lang.FunctionalInterface: API level 24 or higher.
  • java.lang.reflect.Method.isDefault(): API level 24 or higher.
  • java.util.function: API level 24 or higher.

What happens if minSdkVersion is below sourceCompatibility?

If you deliberately use any one of the above APIs in min-sdk versions lower than they supported, (or in your case, minSdkVersion is below sourceCompatibility) it will give you lint error "NewApi". It indicates something in your code isn't optimal or may crash. This answers your app crash on low Android version devices.

Deˣ
  • 4,191
  • 15
  • 24