Failed to sign APK package.

Whenever i try building my project i receive this :

"Failed to sign APK package.

CommandInvokationFailure: Failed to sign APK package.
C:\Users\FENIX\AppData\Local\Android\Sdk\build-tools\27.0.1\apksigner.bat sign --ks "F:/Farouk/Backupgames/user.keystore" --ks-pass pass:"password" --ks-key-alias "fenix" --key-pass pass:"password" "C:\Windows\Temp\SauceChase\Temp/StagingArea/Package.apk"

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
-Djava.ext.dirs=C:\Users\FENIX\AppData\Local\Android\Sdk\build-tools\27.0.1\lib is not supported.  Use -classpath instead.
exit code: 1
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.Android.PostProcessor.Tasks.TasksCommon.Exec (System.String command, System.String args, System.String workingdir, System.String errorMsg)
UnityEditor.Android.PostProcessor.Tasks.BuildAPK.SignPackage (UnityEditor.Android.PostProcessor.PostProcessorContext context)
UnityEditor.Android.PostProcessor.Tasks.BuildAPK.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context)

When i switch my build system to “Gradle(new)” the APK is signed successfully, however the game is full of bugs / ads aren’t showing etc and my google ads plugins get in conflict “Found conflicting Android library play-services-ads-license”.

using jdk1.8.0_151 and latest SDK version available.

List of things i have tried :

Downgrading JDK to version 8.

Set _JAVA_OPTIONS to Xmx512M / Xmx1G.

Signing the APK manually using apksigner.bat + Shell.

I’ts been already 2 days since i finished my first unity game but couldn’t fix this issue so i can upload it on app store.

I finally fixed the issue by doing this :

  • Removing my current google mobile ads version completely from my unity assets folder.
  • Downloading this latest google mobile ad release from Github.
  • I Ran gradlew.bat located in the downloaded folder “\googleads-mobile-unity-master\gradlew.bat”
  • Make sure you got _JAVA_OPTIONS set to Xmx512M ( or higher ) under global / local variables.
  • Copied this content located in “\googleads-mobile-unity-master\source\plugin\Assets” into my assets project folder.
  • Finally i can sign my APK under “Internal” build system.
  1. remove the app from your phone
  2. make sure you use the right keystore (in PlayerSettings-> Publishing settings)