19

Why do we get this error in Android Studio 3.0 RC1?

 com.android.dx.cf.code.SimException: 
default or static interface method used without --min-sdk-version >= 24

According to the android docs, the feature "Default and static interface methods" is compatible with Any min-sdk version.

I tracked this down to a java-library that calls Comparator.naturalOrder() - which has been added in API level 24.
So I would not expect any error-message at all for this code in a java-library.

When I use the code in my own android-app or lib java code, I see the correct lint message: "Call requires API level 24)"

Is the error-message wrong or am I missing something?

TmTron
  • 17,012
  • 10
  • 94
  • 142
  • 1
    "Call requires API level 24" messages appear for source code, not libraries. – CommonsWare Oct 15 '17 at 11:10
  • @CommonsWare You are right - I have clarified the question – TmTron Oct 15 '17 at 11:14
  • Perhaps the implementation of `naturalOrder()` happens to use the Java 8 features you're getting the error about. That's just a guess. You said that you tried using it in your own code -- if you skip over the Lint error and do a build, do you get the same error message that your library is giving you? – CommonsWare Oct 15 '17 at 11:18
  • 1
    @CommonsWare Yes, `naturalOrder) is a static interface method. But I should not get an error, because the android build tools [claim](https://developer.android.com/studio/write/java8-support.html#supported_features) to be able to handle static interface methods for any min-sdk. When I ignore the lint-message and compile my own code, I get the same error-message. – TmTron Oct 15 '17 at 11:23
  • 1
    "But I should not get an error, because the android build tools claim to be able to handle static interface methods for any min-sdk" -- my guess is that it is a documentation error. – CommonsWare Oct 15 '17 at 11:41

4 Answers4

28

I just found out that it works as expected when I activate the D8 dexer which is planned to be the default for Android Studio 3.1

In the project gradle.properties, add:

android.enableD8=true

Now the code compiles as expected and I still get the expected linter messages.

TmTron
  • 17,012
  • 10
  • 94
  • 142
  • 1
    Although be careful. According to Google Dev Blog: "There is currently a known issue around 64-bit devices running Lollipop. Give D8 a try but please don't use it to deploy your app to Google Play. We would love to hear your feedback. You can file a bug report using this link." – Sergey Emeliyanov Nov 03 '17 at 13:55
  • 2
    that [known issue](https://issuetracker.google.com/issues/64740479) is already fixed – TmTron Nov 03 '17 at 14:50
  • Does anyone know why changing the dexer fixes the original issue? If the problem was really that a dependency was requiring API >= 24 what does the dexer have to do with it? – PICyourBrain Dec 28 '17 at 14:19
  • 1
    1) The original problem was, that the compilation failed with an error-message because a library contained a static interface method: this is strange, because (according to the Android docs) static interface methods should just work: so using the new Dexer fixes this issue. 2) The other thing is that the function `naturalOrder()` was added in API version 24: So if you compile for a lower API, you should get a Linter message, but no compile error (which works in both Dexer settings) – TmTron Dec 28 '17 at 14:44
  • This fixes the original problem, then I get r8.errors.CompilationError: Cannot fit requested classes in a single dex file. – Hector Jan 19 '18 at 08:59
4

If the java-library that you're talking about was guava, you can try to upgrade it to the latest android-specific build

implementation 'com.google.guava:guava:23.0-android'

This fixed for me

Diego Plentz
  • 6,760
  • 3
  • 30
  • 31
0

From guava v24 we have two alternative versions: Android or JRE. So, in this case you need to include the dependency as:

compile 'com.google.guava:guava:24.1-android'

Find all the details within the repo: https://github.com/google/guava

Rafa0809
  • 1,733
  • 21
  • 24
0

If this error occurs due to Guava, then the solution, as per the official documentation from Google is: https://github.com/google/guava

Change the dependency to (version is current as of this writing):

api 'com.google.guava:guava:27.0-android'

This fixed the issue for me.

Sam
  • 1,009
  • 10
  • 13