title: How to handle different JAVA version in Android?
body: <p>I was testing Android Studio 3.0 preview with some native java 8 features(such as lambda)</p>
  
  <p>I got two modules (app, mylibrary)</p>
  
  <p>In my app <code>build.gradle</code> file, I includes the library module. </p>
  
  <pre><code>implementation project(':mylibrary')
  </code></pre>
  
  <p>In the mylibrary <code>build.gradle</code> file, I specify the java version in order to test lambda expression. </p>
  
  <pre><code>compileOptions {
      android.compileOptions.sourceCompatibility 1.8
      android.compileOptions.targetCompatibility 1.8
  }
  </code></pre>
  
  <p>It was working fine to me until I upgrade the android studio from canary 3 to canary 5, my approach is broken. </p>
  
  <p>I got the following error message for dynamic</p>
  
  <pre><code>Information:Gradle tasks [:app:assembleDebug]
  /Users/chchi/Documents/PayPal/Android/AS3.0Preview/app/src/main/java/com/example/chchi/myapplication/MainActivity.kt
  Error:Error converting bytecode to dex:
  Cause: invalid opcode ba (invokedynamic requires --min-sdk-version &gt;= 26)
  Error:com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version &gt;= 26)
  ...
  ...
  Error:  ... 5 more
  Error:...at bytecode offset 00000016
  Error:locals[0000]: Lcom/android/tools/fd/runtime/IncrementalChange;
  Error:locals[0001]: &lt;invalid&gt;
  Error:...while working on block 0016
  Error:...while working on method testJava8feature:()V
  Error:...while processing testJava8feature ()V
  Error:...while processing com/example/mylibrary/LamdaTest.class
  Error:Execution failed for task ':app:transformClassesWithDexBuilderForDebug'.
  &gt; com.android.build.api.transform.TransformException: com.android.builder.dexing.DexArchiveBuilderException: com.android.builder.dexing.DexArchiveBuilderException: Unable to convert input to dex archive.
  Information:BUILD FAILED in 8s
  Information:40 errors
  Information:1 warning
  Information:See complete output in console
  </code></pre>
  
  <p>After I follow the instruction to bump the <code>minSdkVersion</code> to <code>26</code>. I am getting following error about converting to dex file. </p>
  
  <pre><code>Information:Gradle tasks [:app:assembleDebug]
  Error:Error converting bytecode to dex:
  Cause: Shouldn't happen
  Error:com.android.dex.util.ExceptionWithContext: Shouldn't happen
  Error:  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  Error:  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  Error:  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  Error:  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  ...
  Error:Caused by: com.android.dex.util.ExceptionWithContext: Shouldn't happen
  Error:  at com.android.dex.util.ExceptionWithContext.withContext(ExceptionWithContext.java:45)
  Error:  at com.android.dx.dex.file.MixedItemSection.placeItems(MixedItemSection.java:320)
  Error:  at com.android.dx.dex.file.DexFile.toDex0(DexFile.java:611)
  Error:  at com.android.dx.dex.file.DexFile.toDex(DexFile.java:242)
  Error:  at com.android.builder.dexing.DxDexArchiveBuilder.lambda$processJobChunk$0(DxDexArchiveBuilder.java:118)
  Error:  ... 5 more
  Error:Caused by: java.lang.RuntimeException: Shouldn't happen
  Error:  at com.android.dx.dex.file.ValueEncoder.constantToValueType(ValueEncoder.java:276)
  Error:  at com.android.dx.dex.file.ValueEncoder.writeConstant(ValueEncoder.java:137)
  Error:  at com.android.dx.dex.file.ValueEncoder.writeArray(ValueEncoder.java:310)
  Error:  at com.android.dx.dex.file.CallSiteItem.place0(CallSiteItem.java:63)
  Error:  at com.android.dx.dex.file.OffsettedItem.place(OffsettedItem.java:242)
  Error:  at com.android.dx.dex.file.MixedItemSection.placeItems(MixedItemSection.java:311)
  Error:  ... 8 more
  Error:...while placing call site{method-handle{invoke-static,method{java.lang.invoke.LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;}}, string{"adder"}, proto{()Lcom/example/mylibrary/PayPalInterface;}, (II)V, method-handle{invoke-static,method{com.example.mylibrary.LamdaTest.lambda$testJava8feature$0:(II)V}}, (II)V}
  Error:...while writing section 12
  Error:Execution failed for task ':app:transformClassesWithDexBuilderForDebug'.
  &gt; com.android.build.api.transform.TransformException: com.android.builder.dexing.DexArchiveBuilderException: com.android.builder.dexing.DexArchiveBuilderException: Unable to convert input to dex archive.
  Information:BUILD FAILED in 1s
  Information:38 errors
  Information:0 warnings
  Information:See complete output in console
  </code></pre>
  
  <p>Eventually, I had to bump and target all my module to <code>1.8</code> (no need to set <code>minSdkVersion</code> to <code>26</code>) in order to let compiler pass. Is there a way to make my library module use <code>1.8</code> only and whoever use my library can still stay at where they are now (<code>1.7</code>)? </p>
  
  <p>If I am only use a certain features that have <a href="https://developer.android.com/studio/write/java8-support.html" rel="nofollow noreferrer">high backward compatibility</a>, then why should I worry about if whoever uses my library target to <code>1.8</code>?</p>