11

My project uses these plugins:

  • OneSignal
  • GooglePlayServices
  • Adjust
  • Google Analytics
  • Unity IAP
  • Facebook SDK

Those plugins are used in almost every project.

But I am over field references count

This is my .aar file list List

What should I do to decrease reference count?

doppelgreener
  • 4,809
  • 10
  • 46
  • 63
Kerem Bekman
  • 1,281
  • 2
  • 13
  • 24
  • when your project's files is increased so, android studio give that type of error, and then android studio required some plugin and using that plugin to android studio provide you able to more files store in your project. and android project provide you only store 65536 files without this plugin. click to see your solution : https://stackoverflow.com/questions/42582850/too-many-field-references-70613-max-is-65536/51054678#51054678 – Prince Dholakiya Jun 28 '18 at 08:59

2 Answers2

31

So What should i do to decrease reference count ?

You have so many plugins with many functions/fields. There is a limit when building this from Unity's Editor and you have reached that limit.

To decrease reference count, you have to delete some these plugins but I am sure that you need them and deletion may not be the appropriate solution in this case.

The only way to actually get around this and build for Android at this moment is to export the Project as Android Project then build it with Android Studio. This removes the reference limit imposed by Unity's Editor.


EDIT

I forgot to mention that you have to enable multidex after exporting it out. Since many people go through this problem daily, I decided to add a thorough instruction on how to fix this problem by exporting it out and also how to fix it without exporting it.

FIX BY EXPORTING THE PROJECT

1A.Export the Unity Project as Android Project.

enter image description here

1B.Import into Android Studio:

enter image description here

If you get grade error when importing into Android Studio like the one below:

Error:org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection

enter image description here

In the "dependencies" block which is in the "buildscript" block in the build.gradle file, change:

classpath 'com.android.tools.build.gradle.2.1.0'

to

classpath 'com.android.tools.build:gradle:2.2.3'

enter image description here


If you export the Project and still get the-same error, you need to manually enable multidex. Below is a simplified step to follow from Google's doc that will be shown to you in the error:

2.Add android:name="android.support.multidex.MultiDexApplication" > to the application tag in the AndroidManifest.xml file.

enter image description here


3.Add compile 'com.android.support:multidex:1.0.1' to the "dependencies" block in the build.gradle file.

enter image description here


4.Add multiDexEnabled true to the "defaultConfig" block which is in the "android" block in the build.gradle file.

enter image description here

Build APK and see if it works. If the reference count error is gone, stop here.


5.Getting a GC overhead exception like the one below?

java.lang.OutOfMemoryError: GC overhead limit exceeded

Increase the heap size that will be used when performing dex operation. From this solution, add the following to the "android" block in the build.gradle file:

dexOptions {
    javaMaxHeapSize "4g"
}

enter image description here

FIX WITHOUT EXPORTING THE PROJECT

Must have Unity 5.5 and above to do this:

1.Go to <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates, Copy the mainTemplate.gradle file to your <ProjectName>Assets\Plugins\Android folder.


2.Go to <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk, Copy the AndroidManifest.xml file to your <ProjectName>Assets\Plugins\Android


3.Open both the mainTemplate.gradle and AndroidManifest.xml file you just copied with Visual Studio then do the modification from the FIX BY EXPORTING THE PROJECT instruction above. Skip/Ignore step #1A and #1B. Just do steps from #2 to #5. That's it.

This is what the final mainTemplate.gradle should look like and this is what the final AndroidManifest.xml should look like. This is only for reference purposes. I suggest you don't use mine but instead follow the copy steps above to create yours because future Unity versions can come with different files. You want to make sure you use the latest one or you may have problems building it.


4.Build APK and see if it works:

enter image description here

--

If the reference count error is gone, stop here:

5.Getting an error like the one below?

Build Failure Release builds have to be signed when using Gradle

Just sign the apk from the Publishing Settings in the Build Settings. You can create new keystore or use an existing one.

enter image description here


6.Another error like below?

Error: Avoid hardcoding the debug mode; leaving it out allows debug and release builds to automatically assign one [HardcodedDebugMode]

Remove android:debuggable="true"> from the AndroidManifest.xml file.


If this non exporting solution did not work for you then you have to use the exporting solution.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I have to use every plugin and I got same error when i tried exporting Project as Android Project Should i do change anyting after export in Android Project ? – Kerem Bekman Mar 05 '17 at 23:59
  • 1
    hi, thanks for your answer, when i done this, when i install apk file, by touching back button, application minimized and do keydown.escape simultaneously. do you know why it minimized my game? – BlackMB Oct 13 '17 at 12:13
  • Thanks for the detailed answer! I upvoted a few days ago :) It was really easy to follow. But I have two questions now. 1. Can I put a different installed version of Gradle other than default 2.1.0 in Unity's mainTemplate.gradle just like in Android Studio? 2. How can I update google-services plugin? I already set the sdk folder properly in Unity, I updated to the latest version in Android Studio but I get the version conflict error when building with the plugin setup in mainTemplate.gradle.. – Jenix Nov 15 '17 at 11:36
  • 1
    1.I don't know but give it a try. Note that Unity determines which gradle version they support. If they don't support others, it won't work or you will run into issues building your project. 2.If you get conflict, check out [unity-jar-resolver](https://github.com/googlesamples/unity-jar-resolver). Any further conflict, create a new question. I am sure many people out there can help you – Programmer Nov 15 '17 at 11:54
  • I tried your solution for still building within Unity and I'm getting this: A problem occurred configuring root project 'gradleOut'. > Build-in class shrinker and multidex are not supported yet I'm using Unity 2017.2.0f3. Help? – Sabre Runner Feb 01 '18 at 15:38
  • @SabreRunner There are two solutions in my answer: Export and fix or don't export an fix. I don't know which one doesn't work for you but try the other one if the first one doesn't work for you. Note that if you follow these steps carefully, it should work for you. I suggest starting over again. – Programmer Feb 01 '18 at 16:00
  • I said I tried through Unity. I tried now through Android Studio and I'm getting this: Error:Build-in class shrinker and multidex are not supported yet. – Sabre Runner Feb 03 '18 at 12:02
  • what to do if I have iMac system and I don't want to export Android Studio project? – Siddharth Sep 03 '18 at 15:16
  • @Siddharth IT shows you what to do. See the **FIX WITHOUT EXPORTING THE PROJECT** section. You have to replace the paths in that section with paths on your iMac – Programmer Sep 03 '18 at 15:36
  • @Programmer I can't able to find mainTemplate.gradle file. Above path belong to Windows System for mac where I found? – Siddharth Sep 03 '18 at 15:41
  • 1
    Follow the instruction. It's at `\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates`. You have to **replace** `UnityInstallationDirecory` with your Unity installation path on your path. You have to Google that since I'm not using Mac. The rest of the path should be there. It's likely `/Applications/Unity` on Mac. – Programmer Sep 03 '18 at 15:49
  • @Programmer, thanks for your answer, it works for me, but i'm having a problem with now my apk installing but won't open on my device anymore? Do you think you can help? I created a question here: https://stackoverflow.com/questions/60902303/unity3d-android-apk-installed-but-wont-open – wasicool2 Mar 28 '20 at 14:58
5

IMO, The accepted answer isn't correct (or only partly correct). Exporting an Android studio will not "magically" fix this issue. Also, from my experience, the exported Android studio project does not always build right away and you have to fix lots of things before you can actually build it.

You are using lots of plugins and so you faced an Android limitation of not being able to reference more than 64k methods.

There are a few things you can try to solve this:

  1. Delete some of these plugins, if possible (as already suggested in the accepted answer).
  2. Use multidex. In the past this was only possible by exporting the project to Android studio and building it there. Starting with Unity 5.5 you can build your project using Gradle right from Unity. This means you can create a custom gradle file that configures your game to use Multidex. This doesn't reduce the method ref count, but it works around it by splitting your native Java count into multiple dex files.
  3. Use ProGuard - since you can use Gradle from Unity, you can also define ProGuard to run and remove any code that is not used.
lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
  • Thank you so much! You're right! If I can use Gradle from Unity, I can do whatever I want!! Wow, applying ProGuard right in Unity, I never thought of it. Your answer gives me some fancy ideas. But problem is, I don't know how to use Gradle in Unity. Is there any tutorial or guide on this? Especially I want to know how to reference directly Android Studio's dependencies such as appcompat library and Google Play services.. This is going to be a game changer to me!! – Jenix Nov 13 '17 at 18:18
  • Starting with Unity 2017.1 (i think), you can add a custom gradle build file from Player Settings. In this file, under "dependencies" you can add any dependency you like (such as appcompat, etc). i believe that should work fine. – lysergic-acid Nov 13 '17 at 21:26
  • Thanks again for making me realize Unity Gradle's potential :) – Jenix Nov 13 '17 at 22:18
  • How do I use ProGuard within Unity? What is the setup procedure? – Sabre Runner Feb 04 '18 at 13:39