How to fix build error with UnityAds after upgrading to Unity 5




Is your Unity project using UnityAds? Did you just update to Unity 5? Are you now seeing an error when trying to build for Android? The read on…

Unity 5 now includes in-built support for UnityAds, without requiring the UnityAds plugin from the Asset Store. As such, if you’ve previously imported the plugin, Unity might complain. It may simply be fixable by choosing Assets > Reimport All (which might make Unity realise it should ‘ignore imported version’ of UnityAds).

But, if that doesn’t work, you’ll probably see something like the following console output when trying to build an Android APK:

java.lang.
IllegalArgumentException
already added: Lcom/unity3d/ads/android/BuildConfig;

java.lang.IllegalArgumentException: already added: Lcom/unity3d/ads/android/BuildConfig;
    at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122)
    at com.android.dx.dex.file.DexFile.add(DexFile.java:161)
    at com.android.dx.command.dexer.Main.processClass(Main.java:732)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)
    at com.android.dx.command.dexer.Main.access$300(Main.java:83)
    at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)
    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:632)
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:510)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:280)
    at com.android.dx.command.dexer.Main.run(Main.java:246)
    at com.android.dx.command.dexer.Main.main(Main.java:215)
    at com.android.dx.command.Main.main(Main.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at SDKMain.main(SDKMain.java:129)

If you’re seeing this, it simply means you’re project is still trying to include both the in-built and imported versions of the UnityAds plugin. To fix it, follow these steps:

  • Close Unity Editor
  • Backup your project
  • Remove any folder or meta file called UnityAds from your project’s Assets/Libs folder
  • Remove any folder or meta file called UnityAds from your project’s Assets/Plugins/Android folder
  • Start Unity
  • Build your project for Android

If this doesn’t work, try doing an Assets > Reimport All, then building again. Hopefully, future version of Unity will solve this problem automatically. Good luck out there!

Leave a Reply