Hi, some libraries I use have dependency on "com.android.support:support-v4" When I bild and run my game, I get runtime error: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat; I found, that Play Services Resolver is pretty agressive in replacing old android libraries with androidx ones. In fact, it looks it is not possible to stop it from actively switching on Jetifier and if you try it (answering no), then rosolving fails. com.android.support:support-v4 is one of the old libraries, that are replaced. It took me whole day to find, that it is not enough to have androidx libraries in <Project>\Temp\gradleOut\build.gradle file. You should also have these two additional lines in gradle.properties: android.useAndroidX = true android.enableJetifier = true Unfortunately, gradle.properties is generated by Unity and is overwritten every time. Then I found this answer from person, that deserves gold medal for it. Using his script...: Code (CSharp): using System.IO; using UnityEditor.Android; using UnityEngine; public class AndroidPostBuildProcessor : IPostGenerateGradleAndroidProject { public int callbackOrder { get { return 999; } } void IPostGenerateGradleAndroidProject.OnPostGenerateGradleAndroidProject(string path) { Debug.Log("Bulid path : " + path); string gradlePropertiesFile = path + "/gradle.properties"; if (File.Exists(gradlePropertiesFile)) { File.Delete(gradlePropertiesFile); } StreamWriter writer = File.CreateText(gradlePropertiesFile); writer.WriteLine("org.gradle.jvmargs=-Xmx4096M"); writer.WriteLine("android.useAndroidX=true"); writer.WriteLine("android.enableJetifier=true"); writer.Flush(); writer.Close(); } } ... adds required lines into gradle.properties and game stops crashing. So, working with androidx libraries seems to be only half-way done. It is pushed by Play Services Resolver, but missing in gradle.properties.
Hello, I tried to use this script in my project. But the moment I start build the project it failed and show some error. Code (CSharp): Assets\AndroidPostBuildProcessor.cs(2,19): error CS0234: The type or namespace name 'Android' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?) Assets\AndroidPostBuildProcessor.cs(5,42): error CS0246: The type or namespace name 'IPostGenerateGradleAndroidProject' could not be found (are you missing a using directive or an assembly reference?) Assets\AndroidPostBuildProcessor.cs(15,10): error CS0246: The type or namespace name 'IPostGenerateGradleAndroidProject' could not be found (are you missing a using directive or an assembly reference?)
To everyone who will get confused: You need unity 2018.2 to have access to the IPostGenerateGradleAndroidProject interface. I tried to use it in Unity 2017.4 and didn't understand why it doesn't work
I am a newbie Unity user and i was getting mad with gradle failure because of play resolver. I thank you sincerely for your solution to the problem as I could not find or invent it myself. Works fine in Unity 2018.4.
I encountered this problem today and was able to know what was happening thanks to it! So Thanks! Also, with new versions of Unity play services resolver will add those lines for you. First you need to enable "Custom Gradle Template" in player settings. Second, enable "Path mainTemplate.gradle" in play services resolver android settings. Then resolve or force resolve again, those lines will be added to the mainTemplate.gradle file and your build will not crash.
You can disable whole Resolver, and use pure Gradle things like what i do it for now, never encounter any conflict from this.
Hey guys, So it was like 24h for me to figure this damm thing so i wan't to share what I did since I use Mac I started from Updating Unity's gradle from 5.1.1 to 5.4.1 by downloading new version exporting and replacing Lib folder for graddle. I added script AndroidPostBuildProcessor > to Editor folder I downloaded and Updated Play services to 1.2.135.0 Unity I am using is 2018.4.12f1 I disabled Instalation inside of resolver settings for Android I deleted all all ready libraries in Android folder from google play.services.support ..... what ever was there from google I created main template I updated build from 3.4.0 to classpath 'com.android.tools.build:gradle:3.5.2' I added this in dependencies since I am using IronSource mediation and I needed to make Admob to work implementation 'com.google.android.gmslay-services-ads:18.2.0' implementation group: 'com.google.android.gms', name: 'play-services-basement', version: '17.1.1' implementation group: 'com.google.android.gms', name: 'play-services-ads-identifier', version: '17.0.0' This way I am downloading explicitly this libraries for mediation In Play services settings I ticked ON path to mainTemplate I clicked force resolve but seamed like nothing hapened maybe it did no idea.. didn't check mainTemplate I went for build and run And finally it managed to build!!! After build this is my mainTemapate Code (CSharp): // GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN buildscript { repositories { mavenCentral() google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.5.2' **BUILD_SCRIPT_DEPS**} } allprojects { repositories { mavenCentral() google() jcenter() flatDir { dirs 'libs' } } } // Android Resolver Repos Start ([rootProject] + (rootProject.subprojects as List)).each { project -> project.repositories { def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/") maven { url "https://maven.google.com" } mavenLocal() jcenter() mavenCentral() } } // Android Resolver Repos End apply plugin: 'com.android.application' **APPLY_PLUGINS** dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.google.android.gms:play-services-ads:18.2.0' implementation group: 'com.google.android.gms', name: 'play-services-basement', version: '17.1.1' implementation group: 'com.google.android.gms', name: 'play-services-ads-identifier', version: '17.0.0' // Android Resolver Dependencies Start implementation 'com.android.support:appcompat-v7:25.3.1' // Facebook.Unity.Editor.AndroidSupportLibraryResolver.addSupportLibraryDependency implementation 'com.android.support:cardview-v7:25.3.1' // Facebook.Unity.Editor.AndroidSupportLibraryResolver.addSupportLibraryDependency implementation 'com.android.support:customtabs:25.3.1' // Facebook.Unity.Editor.AndroidSupportLibraryResolver.addSupportLibraryDependency implementation 'com.android.support:support-v4:25.3.1' // Facebook.Unity.Editor.AndroidSupportLibraryResolver.addSupportLibraryDependency implementation 'com.facebook.android:facebook-applinks:[5,6)' // Assets/FacebookSDK/Plugins/Editor/Dependencies.xml:6 implementation 'com.facebook.android:facebook-core:[5,6)' // Assets/FacebookSDK/Plugins/Editor/Dependencies.xml:5 implementation 'com.facebook.android:facebook-login:[5,6)' // Assets/FacebookSDK/Plugins/Editor/Dependencies.xml:7 implementation 'com.facebook.android:facebook-share:[5,6)' // Assets/FacebookSDK/Plugins/Editor/Dependencies.xml:8 implementation 'com.parse.bolts:bolts-android:1.4.0' // Assets/FacebookSDK/Plugins/Editor/Dependencies.xml:4 // Android Resolver Dependencies End **DEPS**} // Android Resolver Exclusions Start android { packagingOptions { exclude ('/lib/armeabi/*' + '*') exclude ('/lib/mips/*' + '*') exclude ('/lib/mips64/*' + '*') exclude ('/lib/x86/*' + '*') exclude ('/lib/x86_64/*' + '*') } } // Android Resolver Exclusions End android { compileSdkVersion **APIVERSION** buildToolsVersion '**BUILDTOOLS**' compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } defaultConfig { minSdkVersion **MINSDKVERSION** targetSdkVersion **TARGETSDKVERSION** applicationId '**APPLICATIONID**' ndk { abiFilters **ABIFILTERS** } versionCode **VERSIONCODE** versionName '**VERSIONNAME**' } lintOptions { abortOnError false } aaptOptions { noCompress = ['.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**] }**SIGN** buildTypes { debug { minifyEnabled **MINIFY_DEBUG** useProguard **PROGUARD_DEBUG** proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD** jniDebuggable true } release { minifyEnabled **MINIFY_RELEASE** useProguard **PROGUARD_RELEASE** proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD****SIGNCONFIG** } }**PACKAGING_OPTIONS****SPLITS** **BUILT_APK_LOCATION** bundle { language { enableSplit = false } density { enableSplit = false } abi { enableSplit = true } } }**SPLITS_VERSION_CODE****REPOSITORIES****SOURCE_BUILD_SETUP**
Thank you @tummygames! your fix to **DIR_UNITYPROJECT** issue on windows (slash instead of backslash in path) is perfect
to me, I delete all libs in Asset/Plugin/Android after that, Open android setting by menu Assets/Play Service Resolver/ Android Resolver/ Setting Click Restore default and enable Jetifier, Click OK It will Resolve all android X lib (see the image), and build OK hope useful
On Unity 2019 , you must modify Atom's code to build success: Code (CSharp): void IPostGenerateGradleAndroidProject.OnPostGenerateGradleAndroidProject(string path) { Debug.Log("Bulid path : " + path); // This line need modify, to replace new path of gradle string gradlePropertiesFile = path.Replace("unityLibrary", "") + " /gradle.properties"; if (File.Exists(gradlePropertiesFile)) { File.Delete(gradlePropertiesFile); } StreamWriter writer = File.CreateText(gradlePropertiesFile); writer.WriteLine("org.gradle.jvmargs=-Xmx4096M"); writer.WriteLine("android.useAndroidX=true"); writer.WriteLine("android.enableJetifier=true"); writer.Flush(); writer.Close(); }
I did the same. I used the "enhanced" version as edited by Jumeuan. I had a bug where the path of Gradle's output had a single blank space right before /gradle.properties (I had not edited that line so I was puzzled as to why this happened). I use the last version as of today, or at least a very recent one. 2019.2.15f1 Ok, did that too. We're talking about "install android packages", right? I may need some clarification on this. I use several ad SDKs that have put stuff in this folder. There are several "libs" folders, either right after /Android/ or one folder deeper. Are these folders to be deleted, including the libs folders that are inside those named after the advertising companies? I'm not sure they'll be able to reimport the libs after this even if I try to resolve anything. Yes, that file appears when we ask for a custom gradle build. Unticking the box does not delete the file, merely appends a DISABLED extension to its name (not a problem, just pointing this out for those who might read this and be wondering what goes on). Now that's something I don't understand. What does that mean and how did you do that? How did you know that you had to add these specifically? I have several SDKs that may need to be told to do something similar, and it's possible some other plugins may need a similar guidance to allow a gradle-build to be done properly. How do you define what needs to be added? And you added them to the mainTemplate.gradle file, correct? I suppose it's a typo and you meant 'patch mainTemplate'? (Today, now we use External Dependencies Manager which provides the same options and more.) I hope I'll experience the same positive outcome. I obviously can't copy/paste your mainTemplate either but I'll compare mine and yours, if all of this works.
Necroing this, but just wanted to say thanks! I'd been banging my head against the wall trying to work this junk out (Unity 2019.4.40f1). The Play Resolver actually has a tickbox for using Jetifier now and I noticed it added this to my mainTemplate.gradle: which I thought would fix the problem, but nothing actually changed! Once I used the post-build script above everything worked. My Frankenstein's monster-esque hacked together "one workaround after another to get an Android build working" project lives to fight another day! Cheers
If the old support library is used by a specific plugin, reach out the publisher of it and ask them to update to androidx. v24 support libraries are pretty old and actually migrating to androidx isn't a big issue for the publisher. The main reason for this recommendation is it got introduced in 2018 and now its 2022 which has valid timeline for migration.
That's the ultimate solution. It works! I tried everything before that, including copying the appCompat lib to my plugins (which most said is the solution, it's not)
But isn't ... wait... Jetifier supposed to bridge the old and new, notably to properly resolve dependencies that rely on older stuff? Is Jetifier largely useless or what?
True. If you have both old support libraries and androidx libraries within your project, jetifier is supposed to transform old support libs to androidx during build time. This needs setup in unity(enable useAndroidX, jetifier flags)in gradle properties file. However, it's not a tough one to migrate to androidx for the owner of the plugin.