Gradle build Failed

Ver.
Unity 2017.4.3f1

Problem describtion
Needs for multiple plugins integrating lead us to use gradle build (because of 64k limit). But unfortunatly we faced with impossibility to make this build via gradle. Google researches gave us a clue to ad to mainTemplate.gradle lines such as:

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

But it didnt help us. What should we do?

Error code

CommandInvokationFailure: Gradle build failed.
C:/Program Files/Java/jdk1.8.0_172\bin\java.exe -classpath "C:\Program Files\Unity2017_4\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-4.0.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx2048m" "assembleDebug"

stderr[
Dex: Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
    UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Exception parsing classes
    at com.android.dx.command.dexer.Main.processClass(Main.java:781)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:747)
    at com.android.dx.command.dexer.Main.access$1200(Main.java:88)
    at com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1689)
    at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
    at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
    at com.android.dx.command.dexer.Main.processOne(Main.java:695)
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:592)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:321)
    at com.android.dx.command.dexer.Main.run(Main.java:292)
    at com.android.builder.internal.compiler.DexWrapper.run(DexWrapper.java:54)
    at com.android.builder.core.DexByteCodeConverter.lambda$dexInProcess$0(DexByteCodeConverter.java:174)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
    at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:476)
    at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
    at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
    at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
    at com.android.dx.command.dexer.Main.parseClass(Main.java:793)
    at com.android.dx.command.dexer.Main.access$1600(Main.java:88)
    at com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1728)
    at com.android.dx.command.dexer.Main.processClass(Main.java:779)
    ... 16 more

1 error; aborting

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: Return code 1 for dex process

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED in 4s
]
stdout[
The setTestClassesDir(File) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the setTestClassesDirs(FileCollection) method instead.
The getTestClassesDir() method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the getTestClassesDirs() method instead.
The ConfigurableReport.setDestination(Object) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the method ConfigurableReport.setDestination(File) instead.
:preBuild UP-TO-DATE
:preDebugBuild UP-TO-DATE
:checkDebugManifest
:preReleaseBuild UP-TO-DATE
:prepareAndroidArchLifecycleRuntime100Library
:prepareComAndroidSupportSupportCompat2610Library
:prepareComAndroidSupportSupportCoreUi2610Library
:prepareComAndroidSupportSupportCoreUtils2610Library
:prepareComAndroidSupportSupportFragment2610Library
:prepareComAndroidSupportSupportMediaCompat2610Library
:prepareComAndroidSupportSupportV42610Library
:prepareComGoogleAndroidGmsPlayServicesAdsIdentifier1501Library
:prepareComGoogleAndroidGmsPlayServicesAnalytics1601Library
:prepareComGoogleAndroidGmsPlayServicesAnalyticsImpl1601Library
:prepareComGoogleAndroidGmsPlayServicesAuth1501Library
:prepareComGoogleAndroidGmsPlayServicesAuthApiPhone1501Library
:prepareComGoogleAndroidGmsPlayServicesAuthBase1501Library
:prepareComGoogleAndroidGmsPlayServicesBase1501Library
:prepareComGoogleAndroidGmsPlayServicesBasement1501Library
:prepareComGoogleAndroidGmsPlayServicesDrive1501Library
:prepareComGoogleAndroidGmsPlayServicesGames1501Library
:prepareComGoogleAndroidGmsPlayServicesMeasurementBase1600Library
:prepareComGoogleAndroidGmsPlayServicesNearby1501Library
:prepareComGoogleAndroidGmsPlayServicesStats1501Library
:prepareComGoogleAndroidGmsPlayServicesTagmanagerV4Impl1601Library
:prepareComGoogleAndroidGmsPlayServicesTasks1501Library
:prepareComGoogleFirebaseFirebaseAnalytics1601Library
:prepareComGoogleFirebaseFirebaseAnalyticsImpl1611Library
:prepareComGoogleFirebaseFirebaseAppUnity511Library
:prepareComGoogleFirebaseFirebaseCommon1600Library
:prepareComGoogleFirebaseFirebaseCore1601Library
:prepareComGoogleFirebaseFirebaseIid1620Library
:prepareComGoogleFirebaseFirebaseIidInterop1600Library
:prepareComGoogleFirebaseFirebaseMeasurementConnector1600Library
:prepareComGoogleFirebaseFirebaseMeasurementConnectorImpl1601Library
:prepareComGoogleFirebaseFirebaseMessaging1710Library
:prepareComGoogleFirebaseFirebaseMessagingUnity511Library
:prepareComGoogleGamesGpgsPluginSupport0950Library
:prepareCommonLibrary
:prepareGoogleAIDLLibrary
:prepareGooglePlayLibrary
:prepareUnityAdsLibrary
:Firebase:preBuild UP-TO-DATE
:Firebase:preReleaseBuild UP-TO-DATE
:Firebase:checkReleaseManifest
:Firebase:prepareReleaseDependencies
:Firebase:compileReleaseAidl
:Firebase:compileReleaseNdk NO-SOURCE
:Firebase:compileLint
:Firebase:copyReleaseLint NO-SOURCE
:Firebase:mergeReleaseShaders
:Firebase:compileReleaseShaders
:Firebase:generateReleaseAssets
:Firebase:mergeReleaseAssets
:Firebase:mergeReleaseProguardFiles UP-TO-DATE
:Firebase:packageReleaseRenderscript NO-SOURCE
:Firebase:compileReleaseRenderscript
:Firebase:generateReleaseResValues
:Firebase:generateReleaseResources
:Firebase:packageReleaseResources
:Firebase:processReleaseManifest
:Firebase:generateReleaseBuildConfig
:Firebase:processReleaseResources
:Firebase:generateReleaseSources
:Firebase:incrementalReleaseJavaCompilationSafeguard
:Firebase:javaPreCompileRelease
:Firebase:compileReleaseJavaWithJavac
:Firebase:processReleaseJavaRes NO-SOURCE
:Firebase:transformResourcesWithMergeJavaResForRelease
:Firebase:transformClassesAndResourcesWithSyncLibJarsForRelease
:Firebase:mergeReleaseJniLibFolders
:Firebase:transformNativeLibsWithMergeJniLibsForRelease
:Firebase:transformNativeLibsWithStripDebugSymbolForRelease
:Firebase:transformNativeLibsWithSyncJniLibsForRelease
:Firebase:bundleRelease
:GooglePlayGamesManifest.plugin:preBuild UP-TO-DATE
:GooglePlayGamesManifest.plugin:preReleaseBuild UP-TO-DATE
:GooglePlayGamesManifest.plugin:checkReleaseManifest
:GooglePlayGamesManifest.plugin:prepareReleaseDependencies
:GooglePlayGamesManifest.plugin:compileReleaseAidl
:GooglePlayGamesManifest.plugin:compileReleaseNdk NO-SOURCE
:GooglePlayGamesManifest.plugin:compileLint
:GooglePlayGamesManifest.plugin:copyReleaseLint NO-SOURCE
:GooglePlayGamesManifest.plugin:mergeReleaseShaders
:GooglePlayGamesManifest.plugin:compileReleaseShaders
:GooglePlayGamesManifest.plugin:generateReleaseAssets
:GooglePlayGamesManifest.plugin:mergeReleaseAssets
:GooglePlayGamesManifest.plugin:mergeReleaseProguardFiles UP-TO-DATE
:GooglePlayGamesManifest.plugin:packageReleaseRenderscript NO-SOURCE
:GooglePlayGamesManifest.plugin:compileReleaseRenderscript
:GooglePlayGamesManifest.plugin:generateReleaseResValues
:GooglePlayGamesManifest.plugin:generateReleaseResources
:GooglePlayGamesManifest.plugin:packageReleaseResources
:GooglePlayGamesManifest.plugin:processReleaseManifest
:GooglePlayGamesManifest.plugin:generateReleaseBuildConfig
:GooglePlayGamesManifest.plugin:processReleaseResources
:GooglePlayGamesManifest.plugin:generateReleaseSources
:GooglePlayGamesManifest.plugin:incrementalReleaseJavaCompilationSafeguard
:GooglePlayGamesManifest.plugin:javaPreCompileRelease
:GooglePlayGamesManifest.plugin:compileReleaseJavaWithJavac
:GooglePlayGamesManifest.plugin:processReleaseJavaRes NO-SOURCE
:GooglePlayGamesManifest.plugin:transformResourcesWithMergeJavaResForRelease
:GooglePlayGamesManifest.plugin:transformClassesAndResourcesWithSyncLibJarsForRelease
:GooglePlayGamesManifest.plugin:mergeReleaseJniLibFolders
:GooglePlayGamesManifest.plugin:transformNativeLibsWithMergeJniLibsForRelease
:GooglePlayGamesManifest.plugin:transformNativeLibsWithStripDebugSymbolForRelease
:GooglePlayGamesManifest.plugin:transformNativeLibsWithSyncJniLibsForRelease
:GooglePlayGamesManifest.plugin:bundleRelease
:UnityOBBDownloader:preBuild UP-TO-DATE
:UnityOBBDownloader:preReleaseBuild UP-TO-DATE
:UnityOBBDownloader:checkReleaseManifest
:UnityOBBDownloader:prepareReleaseDependencies
:UnityOBBDownloader:compileReleaseAidl
:UnityOBBDownloader:compileReleaseNdk NO-SOURCE
:UnityOBBDownloader:compileLint
:UnityOBBDownloader:copyReleaseLint NO-SOURCE
:UnityOBBDownloader:mergeReleaseShaders
:UnityOBBDownloader:compileReleaseShaders
:UnityOBBDownloader:generateReleaseAssets
:UnityOBBDownloader:mergeReleaseAssets
:UnityOBBDownloader:mergeReleaseProguardFiles UP-TO-DATE
:UnityOBBDownloader:packageReleaseRenderscript NO-SOURCE
:UnityOBBDownloader:compileReleaseRenderscript
:UnityOBBDownloader:generateReleaseResValues
:UnityOBBDownloader:generateReleaseResources
:UnityOBBDownloader:packageReleaseResources
:UnityOBBDownloader:processReleaseManifest
:UnityOBBDownloader:generateReleaseBuildConfig
:UnityOBBDownloader:processReleaseResources
:UnityOBBDownloader:generateReleaseSources
:UnityOBBDownloader:incrementalReleaseJavaCompilationSafeguard
:UnityOBBDownloader:javaPreCompileRelease
:UnityOBBDownloader:compileReleaseJavaWithJavac
:UnityOBBDownloader:processReleaseJavaRes NO-SOURCE
:UnityOBBDownloader:transformResourcesWithMergeJavaResForRelease
:UnityOBBDownloader:transformClassesAndResourcesWithSyncLibJarsForRelease
:UnityOBBDownloader:mergeReleaseJniLibFolders
:UnityOBBDownloader:transformNativeLibsWithMergeJniLibsForRelease
:UnityOBBDownloader:transformNativeLibsWithStripDebugSymbolForRelease
:UnityOBBDownloader:transformNativeLibsWithSyncJniLibsForRelease
:UnityOBBDownloader:bundleRelease
:prepareDebugDependencies
:compileDebugAidl UP-TO-DATE
:compileDebugRenderscript UP-TO-DATE
:generateDebugBuildConfig UP-TO-DATE
:generateDebugResValues UP-TO-DATE
:generateDebugResources UP-TO-DATE
:mergeDebugResources UP-TO-DATE
:processDebugManifest
:processDebugResources
:generateDebugSources
:incrementalDebugJavaCompilationSafeguard UP-TO-DATE
:javaPreCompileDebug
:compileDebugJavaWithJavac UP-TO-DATE
:compileDebugNdk NO-SOURCE
:compileDebugSources UP-TO-DATE
:mergeDebugShaders UP-TO-DATE
:compileDebugShaders UP-TO-DATE
:generateDebugAssets UP-TO-DATE
:mergeDebugAssets
:transformClassesWithDexForDebug FAILED
126 actionable tasks: 114 executed, 12 up-to-date
]
exit code: 1
UnityEditor.Android.Command.WaitForProgramToRun (UnityEditor.Utils.Program p, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.Android.AndroidJavaTools.RunJava (System.String args, System.String workingdir, System.Action`1 progress, System.String error)
UnityEditor.Android.GradleWrapper.Run (System.String workingdir, System.String task, System.Action`1 progress)
Rethrow as GradleInvokationException: Gradle build failed
UnityEditor.Android.GradleWrapper.Run (System.String workingdir, System.String task, System.Action`1 progress)
UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

Why are you adding those lines to your Gradle file? not sure they’re needed. in case you’re not adding that, what kind of error are you seeing ?

Dex: Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.

This is the error. I added those lines just to find out will it help or not. Anyway nothing has changed after adding those lines - build is failing each time ;(

Still looking for decision