title: Lambda compatibility with Android API 21-24
body: <p>I'm developing an Android application that uses a very, very large third-party Java library (netcdf-java). The library makes extensive use of Java 8 features, particularly lambda expressions.</p>
  <p>The application works fine on Oreo/API 25+, but crashes and burns on API 21-24 (21 was the oldest I bothered to try testing) with an error that appears to be a NoClassDefFoundError involving lambdas, after a bunch of failed classloads that I've read basically mean, &quot;This version of Android can't do lambdas&quot;:</p>
  <pre><code>06-14 15:43:05.395 8010-8127/pantherkitty.launchspotter I/art: Rejecting re-init on previously-failed class java.lang.Class&lt;ucar.nc2.-$$Lambda$Group$oysUYWnjlRBOBg2TJn5tet0MooM&gt;
  (... snip ...)
  06-14 15:43:05.397 8010-8127/pantherkitty.launchspotter E/PRETTY_LOGGER: │ caught exception while parsing GriddedData for /data/user/0/pantherkitty.launchspotter/cache/abi-l2-acmc-57529695.nc : java.io.IOException: java.lang.NoClassDefFoundError: ucar.nc2.-$$Lambda$Group$oysUYWnjlRBOBg2TJn5tet0MooM
  06-14 15:43:05.397 8010-8127/pantherkitty.launchspotter E/PRETTY_LOGGER: │  at ucar.nc2.NetcdfFile.open(NetcdfFile.java:401)
  06-14 15:43:05.397 8010-8127/pantherkitty.launchspotter E/PRETTY_LOGGER: │  at ucar.nc2.dataset.NetcdfDataset.openProtocolOrFile(NetcdfDataset.java:831)
  06-14 15:43:05.397 8010-8127/pantherkitty.launchspotter E/PRETTY_LOGGER: │  at ucar.nc2.dataset.NetcdfDataset.openDataset(NetcdfDataset.java:479)
  06-14 15:43:05.397 8010-8127/pantherkitty.launchspotter E/PRETTY_LOGGER: │  at ucar.nc2.dataset.NetcdfDataset.acquireDataset(NetcdfDataset.java:634)
  06-14 15:43:05.397 8010-8127/pantherkitty.launchspotter E/PRETTY_LOGGER: │  at ucar.nc2.dt.grid.GridDataset.open(GridDataset.java:86)
  06-14 15:43:05.397 8010-8127/pantherkitty.launchspotter E/PRETTY_LOGGER: │  at ucar.nc2.dt.grid.GridDataset.open(GridDataset.java:72)
  </code></pre>
  <p>I've read that Google <em>supposedly</em> tweaked Android Studio/Gradle to make Java 8 features backwards-compatible with older devices limited to earlier APIs. Theory aside... I've never seen it actually <em>work</em>, despite using the latest version of Android Studio, upgrading the project to Gradle 3, targeting API=30 (with min API=21), and specifying JavaVersion.VERSION_1_8 for sourceCompatibility and targetCompatibility in build.gradle.</p>
  <p><strong>So... is there something <em>else</em> I need to do to enable unfettered Lambda-usage by Android apps running under API 21-24, or is 100% backwards-compatibility with lambdas and older Android devices with only API 21-24 (Lollipop, Marshmallow, and Nougat) something that <em>still</em> doesn't work... and probably <em>won't ever</em> work?</strong></p>
  <hr />
  <h3>update: I applied Artem Viter's suggestions.</h3>
  <p>I'm no longer getting the &quot;rejecting re-init on previously failed class&quot; or the &quot;NoClassDefFound&quot; errors for classes that appeared to be lambda-related earlier, but now I'm getting some <em>new</em> errors that are even more mystifying.</p>
  <p>I'm not sure whether this is just a new manifestation of my original problem (say, if I'm just seeing an error message arising from desugared code that ultimately boils down to the same thing), or whether this is something completely new and different that ought to have a different question here.</p>
  <p>(note: I'm testing it on a Chuwi Hi12 tablet with Intel Cherry Trail SoC and Android 5.1, which might explain the Intel-related errors)</p>
  <pre><code>06-14 18:56:35.783 17016-17677/pantherkitty.launchspotter W/art: Failed to open zip archive '/system/framework/com.intel.config.jar': I/O Error
  06-14 18:56:35.783 17016-17677/pantherkitty.launchspotter W/art: Failed to open zip archive '/system/framework/com.intel.aware.awareservice.jar': I/O Error
  06-14 18:56:35.900 17016-17677/pantherkitty.launchspotter W/art: Failed to open zip archive '/system/framework/com.intel.config.jar': I/O Error
  06-14 18:56:35.900 17016-17677/pantherkitty.launchspotter W/art: Failed to open zip archive '/system/framework/com.intel.aware.awareservice.jar': I/O Error
  06-14 18:56:35.970 17016-17677/pantherkitty.launchspotter E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
      Process: pantherkitty.launchspotter, PID: 17016
      java.lang.RuntimeException: An error occured while executing doInBackground()
          at android.os.AsyncTask$3.done(AsyncTask.java:304)
          at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
          at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
          at java.util.concurrent.FutureTask.run(FutureTask.java:242)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
          at java.lang.Thread.run(Thread.java:818)
       Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/invoke/MethodHandles;
          at ucar.nc2.dataset.EnhanceScaleMissingUnsignedImpl.&lt;clinit&gt;(EnhanceScaleMissingUnsignedImpl.java:30)
          at ucar.nc2.dataset.VariableDS.&lt;init&gt;(VariableDS.java:761)
          at ucar.nc2.dataset.NetcdfDataset.convertVariable(NetcdfDataset.java:1172)
          at ucar.nc2.dataset.NetcdfDataset.convertGroup(NetcdfDataset.java:1156)
          at ucar.nc2.dataset.NetcdfDataset.&lt;init&gt;(NetcdfDataset.java:1139)
          at ucar.nc2.dataset.NetcdfDataset.openDataset(NetcdfDataset.java:485)
          at ucar.nc2.dataset.NetcdfDataset.acquireDataset(NetcdfDataset.java:634)
          at ucar.nc2.dt.grid.GridDataset.open(GridDataset.java:86)
          at ucar.nc2.dt.grid.GridDataset.open(GridDataset.java:72)
          at pantherkitty.launchspotter.GriddedData.update(GriddedData.java:45)
          at pantherkitty.launchspotter.MapsActivity$GriddedDataParserTask.doInBackground(MapsActivity.java:242)
          at pantherkitty.launchspotter.MapsActivity$GriddedDataParserTask.doInBackground(MapsActivity.java:221)
          at android.os.AsyncTask$2.call(AsyncTask.java:292)
          at java.util.concurrent.FutureTask.run(FutureTask.java:237)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
          at java.lang.Thread.run(Thread.java:818) 
       Caused by: java.lang.ClassNotFoundException: Didn't find class &quot;java.lang.invoke.MethodHandles&quot; on path: DexPathList[[zip file &quot;/data/app/pantherkitty.launchspotter-2/base.apk&quot;],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
          at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
          at ucar.nc2.dataset.EnhanceScaleMissingUnsignedImpl.&lt;clinit&gt;(EnhanceScaleMissingUnsignedImpl.java:30) 
          at ucar.nc2.dataset.VariableDS.&lt;init&gt;(VariableDS.java:761) 
          at ucar.nc2.dataset.NetcdfDataset.convertVariable(NetcdfDataset.java:1172) 
          at ucar.nc2.dataset.NetcdfDataset.convertGroup(NetcdfDataset.java:1156) 
          at ucar.nc2.dataset.NetcdfDataset.&lt;init&gt;(NetcdfDataset.java:1139) 
          at ucar.nc2.dataset.NetcdfDataset.openDataset(NetcdfDataset.java:485) 
          at ucar.nc2.dataset.NetcdfDataset.acquireDataset(NetcdfDataset.java:634) 
          at ucar.nc2.dt.grid.GridDataset.open(GridDataset.java:86) 
          at ucar.nc2.dt.grid.GridDataset.open(GridDataset.java:72) 
          at pantherkitty.launchspotter.GriddedData.update(GriddedData.java:45) 
          at pantherkitty.launchspotter.MapsActivity$GriddedDataParserTask.doInBackground(MapsActivity.java:242) 
          at pantherkitty.launchspotter.MapsActivity$GriddedDataParserTask.doInBackground(MapsActivity.java:221) 
          at android.os.AsyncTask$2.call(AsyncTask.java:292) 
          at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
          at java.lang.Thread.run(Thread.java:818) 
          Suppressed: java.lang.ClassNotFoundException: java.lang.invoke.MethodHandles
          at java.lang.Class.classForName(Native Method)
          at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
          at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                  ... 19 more
       Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
  </code></pre>
  <h3>update 2:</h3>
  <p>I got the same crash on my Galaxy S3 (running AOSP Marshmallow), but without the four W/art errors related to Intel jarfiles I got on the tablet. The details of the crash itself were substantially identical after that.</p>