Gradle Wrapper (gradlew) Generator Package

Hi everybody! In my latest work I’ve been dealing with mobile projects that use a version of Gradle that is not the one bundled with the version of Unity we use (I know, this is not officially supported, but we use features that only newer versions have).

So I made this simple, free and open source package for automatically generating the Gradle Wrapper (gradlew) when exporting Android projects:

It is installable using UPM and the only setup needed is a Project Setting with the wanted version of Gradle. The plugin has an OnPostGenerateGradleAndroidProject method that generates the wrapper using the JDK and Gradle already configured in Unity, so there is no need to install or configure anything else.

Cool stuff about having gradlew in exported projects:

  • Android Studio generates a Gradle Wrapper if there is none in the project, usually with one of the newest versions available. Having the wrapper already set up means Android Studio will use the exact version of Gradle that you need, so you don’t need to worry about it picking up a version of Gradle that is incompatible with your projects’ build scripts.
  • If you need a version that is not the one bundled with Unity and you always export projects, that is, don’t use the builtin Android build from Unity, you don’t need to download Gradle independently and set it up in External Tools. Just run Android Studio or gradlew directly and voilà!
  • Using gradlew might mean less environment setup, which is good for CI or when onboarding new developers into your team.
  • The file ProjectSettings/GradleVersion.txt, generated by the plugin’s settings provider, serves as self documentation of which version of Gradle the project needs. Might be useful even if it’s the same as the one bundled with the Unity Android module.

And that’s it! If anybody tries it out, please let me know what you think. Also, feel free to open issues and discussions in the repo or here in this thread. I’d really appreciate any feedback. If it works or not, if it’s easy to install, easy to use, if current documentation is enough…
Thanks!

2 Likes

Will definitely give a try! Thanks for sharing.
Are you open for any pull requests? If so can make it compatible for mac.

Of course, feel free to do it!

I haven’t tested on a mac yet, but I believe it will just work the way it is. Both Linux and Windows worked, they both have the same folder structure in regards to Unity installations, I believe mac will be the same.
@Voxel-Busters I will test this on a mac later and tell you about what I find. If you do test first, let me know if it works or not.

Hey, @gilzoide Thanks for sharing. It works very well!

There was a problem with UNITY_ANDROID_PATH on macOS. It is looking inside the Contents folder but the PlaybackEngines folder is not there.

The problem solved when I changed the path on GradleWrapperGenerator.cs from this:

static readonly string UNITY_ANDROID_PATH = Path.Combine(
            EditorApplication.applicationContentsPath, "PlaybackEngines", "AndroidPlayer"
        );

To this:

static readonly string UNITY_ANDROID_PATH = Path.Combine(
            EditorApplication.applicationPath, "../", "PlaybackEngines", "AndroidPlayer"
        );
1 Like

@gokhanyahyatorba Thanks for the insight!

I’ve made a new version 1.0.2 that should fix this. It’s using the path with the Data folder in Windows/Linux, but without it in macOS. It’s also using AndroidExternalToolsSettings.gradlePath when available (Unity 2019.3+), so it will be even more rare for the plugin to need to “guess” the file paths.

Thanks a lot for the help, I hope it works now xD

2 Likes