这个问题比较玄学,经常会卡在以下情况:
1.Building Gradle Project...
2.Calling IPostGenerateGradleAndroidProject callbacks
等等各种都是卡在和gradle有关的地方。尝试了各种操作,个人觉得应该是以下几个方法奏效,从而可以成功打包。
0.Admob版本:Google Mobile Ads Unity Plugin v9.1.0
0.Unity版本:2021.3.23f1
1.当你按照Admob指南Resolve后,找到Assets\Plugins\Android\mainTemplate.gradle文件,找到下面的部分,将maven的url换成阿里云的,其他地方不用改。
([rootProject] + (rootProject.subprojects as List)).each { project ->
project.repositories {
def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/" // Assets/GoogleMobileAds/Editor/GoogleMobileAdsDependencies.xml:7, Assets/GoogleMobileAds/Editor/GoogleMobileAdsDependencies.xml:12, Assets/GoogleMobileAds/Editor/GoogleUmpDependencies.xml:7
}
mavenLocal()
mavenCentral()
}
}
2.为了保险起见,我在_UnityEditor\2021.3.23f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates\baseProjectTemplate.gradle中也进行了类似的设置(如果你没导入Admob,就只改这里)
allprojects {
buildscript {
repositories {**ARTIFACTORYREPOSITORY**
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
google()
jcenter()
}
dependencies {
// If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity
// See which Gradle version is preinstalled with Unity here https://docs.unity3d.com/Manual/android-gradle-overview.html
// See official Gradle and Android Gradle Plugin compatibility table here https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
// To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version
classpath 'com.android.tools.build:gradle:4.0.1'
**BUILD_SCRIPT_DEPS**
}
}
repositories {**ARTIFACTORYREPOSITORY**
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
google()
jcenter()
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
3.设置一下Android Resolver,在Unity中,菜单栏点击Assets>External Dependency Manager>Android Resolver>Settings,禁用Enable Auto Resolution和Enable Resolution On Build(为了防止打包的时候它自动Resolve然后会把我们在mainTemplate.gradle配置的地址又替换成谷歌的)
4.删除掉C:\Users\Administrator\.gradle\caches目录
5.重启Unity
6.一般来说可以正常打包了。我的玄学情况是,虽然偶尔还是会卡住,但在卡住的时候开启上网工具似乎就不会再卡住,如果你已经开启了上网工具,就在卡住的时候关闭它,然后就正常了。