Reputation: 29571
I'm learning Android development, and I noticed the docs for findViewById() say that casting its returned object is unnecessary:
<Android API 26 Platform>
... resulting view is automatically cast to the target class type...
I'd prefer to not cast if I don't have to (less code to write and read!), but this seems to be a Java 8 feature. Yet I'd like my app to work on platforms as old as 19 (Kitkat).
The page https://developer.android.com/studio/write/java8-support.html does not seem to cover automated type casting available in Java 8, and various other questions/answers on SO indicate that some features of Java 8 are available all the way to SDK 9.
Firstly, how can I tell if a Java language feature is in my range of target SDK? Secondly, how is it possible that new language features are supported in platform API that are several years old?
Upvotes: 1
Views: 118
Reputation: 29571
Posts indicate that "this has nothing to do with Java 8". This seems to be only partially correct, in a way that does not invalidate my 2 questions. So here are my own conclusions, based on additional reading I did since my OP, and I'll fix it based on responses.
The official docs are clear: Android supports ... a subset of Java 8 language features that vary by platform version.
Note the terms "language feature", not "API". The latter usually means the constants, classes, and class members (and for methods, their signatures) defined in a particular SDK and accessable/callable from your code; whereas "language features" are things like operators, precedence rules, try-with-resource, etc. So whereas "SDK" and "API" are often used interchangeably, they are probably not the same on Android: the SDK is likely API + tools (like compiler, transpiler, scripts for error checking, etc).
The quote implies that as the SDK level increases, more and more language features of Java 8 spec become supported. The key word is "supported": if language feature A is supported at SDK 27 but not at 26, and you compile your program with compileSdk: 27
, and it runs correctly on an Android device with platform 26 (this assumes your program does not use any of the new API 27, it just uses the new language features like a new operator etc), what magic is at play?
The only explanation I have (and I'm hoping the answer to second question in my OP) is the additional compilation that I didn't know about when I posted: the Java bytecode (.class file) generated from the Java compiler is transpiled to DEX format. This additional step involves a tool, which is not part of the API, but is probably part of the SDK. This DEX compiler is where language features get implemented: if the DEX compiler does not know how to transpile a Java bytecode, then that operator is not supported by Android. This suggests that when possible, new Java bytecodes are probably not converted to new DEX bytecodes; instead, they get converted to an equivalent sequence of DEX bytecodes that perform the operation in the older platform (which has an older Dalvik VM). When such equivalence is too onerous to determine, is when the minSdk is affected (there is then simply no way an older Dalvik VM can run the DEX files).
For my first question, each SDK probably includes "inspection rules" that check for those Java JDK features that are not yet supported by the DEX compiler, thus the way to tell if a JAVA language feature is supported in a given SDK range is to lint, and assume that inspection rules used by the linter flag all issues correctly (no false positives or negatives).
Any corrections welcome!
Upvotes: 0
Reputation: 12953
You donot have to cast object returned by findViewById()
if your compileSdkVersion is >= 26
i.e Android O
. Its not related to java 8.
Android Studio will show error or warning depending on the API if a particular API is not supported or deprecated.
Upvotes: 1