Search Unity

Too many method references; max is 65536

Discussion in 'Android' started by Qbit86, May 20, 2015.

  1. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    Yes the UI is in 2017.1. May look something like this

     
  2. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    You could also see the expected custom template paths. :)
     
  3. r-pedra

    r-pedra

    Joined:
    Dec 4, 2015
    Posts:
    104
    Hey Unity guys, did you tried to import Google Play Games plugin?

    When you build with Gradle, it says that some values (google play services version) are missing in plugins xml file but these values are present in some .aar from Google Play Services.
    Anyway, I tried both export and direct build in unity and android studio is saying the same that unity. I don't know why gradle can't see the value. If you open the .aar file (If I remember it's located in basement library), you can find the value in the res folder.
    I tried anything but nothing works.
    Maybe it's some sort of project architecture created by unity to compile with gradle
     
  4. Honikou

    Honikou

    Joined:
    Feb 18, 2013
    Posts:
    92
    Same problem for us
     
  5. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    If Android Studio is saying the same as Unity - report a bug to Google!
     
  6. r-pedra

    r-pedra

    Joined:
    Dec 4, 2015
    Posts:
    104
  7. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    56
    Sorry to say this but this problem is not fixed yet. Simply exporting it won't fix it.

    Using Unity 5.6.0f3


    After that exporting it, you have to:

    1.Add
    Code (csharp):
    1. android:name="android.support.multidex.MultiDexApplication" >
    to the
    application tag in the AndroidManifest file.

    2.Add
    Code (csharp):
    1. compile 'com.android.support:multidex:1.0.1'
    to the "dependencies" block in the build.gradle file.

    3.Add
    Code (csharp):
    1. multiDexEnabled true
    to the "defaultConfig" block in the build.gradle file.

    Why is this? This problem has been here for over a year now. Why not just automate this?
     
    Last edited: Jul 21, 2017
    Qbit86 likes this.
  8. alan_motionlab

    alan_motionlab

    Joined:
    Nov 27, 2014
    Posts:
    99
    You can add that into the template that Unity uses. You've just got to remember to change it each time you install a new version. In Unity 2017.1, you can add in your own custom build.gradle, so don't have to do this.
     
    Qbit86 likes this.
  9. alan_motionlab

    alan_motionlab

    Joined:
    Nov 27, 2014
    Posts:
    99
    In Windows, go to {unitypath}\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates and change mainTemplate.gradle with the additions you added.
     
  10. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    56
    Thanks. FYI you also have to modify the Manifest file. I am not new to Unity but this is a hard problem for new Unity users.

    I still think that Unity should detect that error when building apk then ask user if this problem should be fixed. If they click yes, Unity should then automatically perform all those required tasks to fix this.

    That what a proper fix is in this case.
     
  11. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I don't think this issue can be automatically fixed. It's also not magically fixed for Android app developers.

    Instead, you now have the tools (gradle) to fix it yourself by defining what you want to do (proguard for minifying the binary size and the number of method reference counts, or multidex).
     
  12. alan_motionlab

    alan_motionlab

    Joined:
    Nov 27, 2014
    Posts:
    99
    @hangemhigh - by making these changes to the mainTemplate, all my builds work now with Gradle set as the build system. I haven't had to make any changes to my manifest. It works for all of my games, some use custom manifests, some use the default, but its been building fine since. I just can't use Cloud build for it at the moment, as it requires a this gradle template change, and I've not yet updated my projects to 2017.1 to supply my own.
     
  13. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    56

    I had to modify the Manifest to get mine working. Maybe that's because I am using old API. Not sure why but I can't remember the error I got without doing that. Also, I still use 5.6 not 2017 version so that might be the problem.
     
  14. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    56
    This problem can be automatically detected and fixed!

    I understand that you can't spoon-feed people but this issue waste time of new Unity users. It is true that Android Studio users go through it too but they can easily find a fix by Googling this issue. This Thread is about 3 pages long at the time of writing this but it had no clear instruction on how to do this. When I fixed this issue, it wasn't by reading this thread. It was by reading this Google Guide which many people will not be able to find. Hopefully my post above will help people.

    When you find yourself doing the-same thing over and over again when new Project is created, it's time to automate this.

    Just because it's an Android problem does not mean it shouldn't be fixed. I did automate but don't have permission to post the code here.

    If anyone or Unity Engr care about automatic fix with Editor plugin, here are the steps:

    1.When users builds for android, wait for the build to finish.

    2.Read the log and search for "Too many method references" string in the log.

    3.If not present, assume that this Max error stuff did not occur. If this string is present, pop-up a Window and ask user if Unity should automatically fix this.

    4.If user clicks yes,

    Automate the following via code:

    4A.Read the
    Code (csharp):
    1. <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk\AndroidManifest.xml
    file into memory.

    Inject
    Code (csharp):
    1. android:name="android.support.multidex.MultiDexApplication" >
    into it right after
    Code (csharp):
    1. <application
    .

    Save the modified data in memory as "AndroidManifest.xml" to your
    Code (csharp):
    1.  <ProjectName>Assets\Plugins\Android
    folder.

    4B.Read the
    Code (csharp):
    1. <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates\mainTemplate.gradle
    file into memory.

    Inject
    Code (csharp):
    1. compile 'com.android.support:multidex:1.0.1'
    right after the
    Code (csharp):
    1. dependencies {

    4C. Also inject
    Code (csharp):
    1. multiDexEnabled true
    right after
    Code (csharp):
    1. defaultConfig {
    Save the modified data in memory as "mainTemplate.gradle" file to your
    Code (csharp):
    1. <ProjectName>Assets\Plugins\Android
    folder

    5.Automatically rebuild the project now or tell the user that the issue is fixed and ask them to build the project again.

    That's it. Since I got this working, the failure rate is 0. I have tried this on many projects with this error and it fixed each one within a sec. It can be done from command line too so no human interaction is ever needed.
     
    Last edited: Aug 1, 2017
    liuxuan likes this.
  15. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    While this might work, it is not an ideal solution.
    I recall @bitter advising against it (although it was a long time ago). See this thread: https://forum.unity3d.com/threads/multidex-support-on-android.325429/

    Specifically, he linked the Android page discussing the limitation of multidex: https://developer.android.com/studio/build/multidex.html#limitations

    Also, some people still don't use Gradle when exporting from Unity (although the "ADT" option is going away and will soon be deprecated).

    The real solution to this issue, as i answer on every possible thread i see can be summed up as 3 different options, as i see it (quoting my answer from this stack overflow question):
    1. Delete some of the plugins you're using, if possible. Many people just import tons of stuff they don't need. This results in a bloated .apk with lots of unneeded code.
    2. Use ProGuard - since you can use Gradle from Unity, you can also define ProGuard to run and remove any code that is not used. This will fix the method ref count, and also shrink the .apk a bit by removing unneeded code.
    3. Use multidex. In the past this was only possible by exporting the project to Android studio and building it there. Since 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.
     
    Voxel-Busters, Qbit86 and Yury-Habets like this.
  16. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    @hangemhigh your assumptions and recommendations are incorrect and will make the life worse for thousands of people. Please note the link posted by @liortal.

    - your app may encounter "Application not responding" on startup
    - you have to add the HUGE support library in order to work on devices before Android 5.0
    - you are not addressing the issue itself. It's like fixing an app being too large by buying a new SSD.

    It's not as bad now as @bitter mentioned it before, because ART in Android (5.0+) is now in majority of the phones, but the recommendation to remove, strip and proguard libraries before enabling multidex is still actual.

    Once again, read https://developer.android.com/studio/build/multidex.html carefully before proceeding!
     
    Qbit86 and liortal like this.
  17. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    @liortal Thanks for the great information.

    To aid the step you mentioned in step 1, We recently made a script to find out the method counts. We would like to share the same as it might help to find from where the majority references are from.
    You can find it here.

    Just run it from terminal by providing a folder to search for jar files.
    Thanks,
    VB Team
     
  18. Amirraza

    Amirraza

    Joined:
    Jul 13, 2018
    Posts:
    17
    if i am using gradle build there is no further issue but when I install my apk on mobile it just crushes
     
  19. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    sounds like it might be a different issue. maybe you can open a new thread and post the logcat logs with your crash?