Unity 游戏提升 Android TargetVersion 相关记录
原因:googleplay 要求。
要构建指定 TargetVersion 的App,
必须使用所需版本的 JDK、Android SDK、Android NDK、Gradle。
只将Unity PlayerSettings/Android/OtherSettings 中,TargetAPILevel 修改为目标版本,然后构建 是不行的!
一般而言,如果 googleplay 有提升要求,Unity会跟进,出对应的 Unity小版本。
直接更换 Unity小版本是成本最小的办法。
但注意,官方可能并不是及时的。可能得半个月左右才会稳定。要是着急必然要趟一些坑。
若要自己在老Unity的基础上替换新的 JDK、Android SDK、Android NDK、Gradle,成本可能是不小的。
----------------------------------------------------------------------------------------------------
这里只记录更换Unity版本的过程。
应注意:
若项目中存在由老版本生成的任何 template.gradle、Manifest、ProguardFile 等文件,则可能需要重新生成/修改。
由于目标gradle版本的差异,这些文件的结构甚至会产生很大变化。
请务必对比这几个新老文件,按需合并,以确保:
其中的数据结构、内容值,与最终所需相符。
最好是由新版本的Unity重新生成,再补充之前需要的内容。
最后,应观察一下导出的安卓工程中的三个gradle 中的 内容是否与所需相符。
尤其注意compileSdkVersion、buildToolsVersion、JavaVersion。
对比 Unity2021.3.4f1c1 和 Unity2021.3.41f1c1生成的部分模板的差异如下:
1、根目录的build.gradle:
2、unityLibrary 中的 build.gradle:
打包报错:
1、* What went wrong:
A problem occurred configuring root project 'XXXXXX'.
> Could not resolve all files for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:7.4.2.
Required by:
project : > com.android.application:com.android.application.gradle.plugin:7.4.2
project : > com.android.library:com.android.library.gradle.plugin:7.4.2
> No matching variant of com.android.tools.build:gradle:7.4.2 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5.1' but:
- Variant 'apiElements' capability com.android.tools.build:gradle:7.4.2 declares a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1')
- Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally:
- Incompatible because this component declares documentation and the consumer needed a library
- Other compatible attributes:
- Doesn't say anything about its target Java version (required compatibility with Java 8)
- Doesn't say anything about its elements (required them packaged as a jar)
- Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1')
- Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1')
- Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally:
- Incompatible because this component declares documentation and the consumer needed a library
- Other compatible attributes:
- Doesn't say anything about its target Java version (required compatibility with Java 8)
- Doesn't say anything about its elements (required them packaged as a jar)
- Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1')
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 6s
原因:gradle 执行时读取的 JAVA_HOME 中的 JAVA 版本不是需要的 11,而是 8。
在检查确定 build.gradle 中的 JavaVersion 无误后,最终确定是 Jenkins 环境变量未修改。
当然,最好直接将 JAVA_HOME 配到系统环境变量中。
2、* What went wrong:
Execution failed for task ':launcher:checkDebugAarMetadata'.
> Could not resolve all files for configuration ':launcher:debugRuntimeClasspath'.
> Could not find com.bytedance.applog:RangersAppLog-Lite-cn:6.14.3.
Required by:
project :launcher > project :unityLibrary
> Could not find com.google.firebase:firebase-analytics-unity:11.6.0.
Required by:
project :launcher > project :unityLibrary
> Could not find com.google.firebase:firebase-app-unity:11.6.0.
Required by:
project :launcher > project :unityLibrary
> Could not find com.google.firebase:firebase-crashlytics-unity:11.6.0.
Required by:
project :launcher > project :unityLibrary
原因:gradle 7 之后将仓库声明调整至 setting.gradle 中,
而 Unity 2021.3.41f1c1 中没有将 setting.gradle 暴露出来,EDM4U 还是将 仓库声明生成到了 unityLibray 项目的 build.gradle 中。
https://github.com/googlesamples/unity-jar-resolver/issues/699
解决:提升Unity 版本到 Unity 2021.3.42f1c1,并且更新EDM4U。
另:因Unity版本提升,HybridCLR 也需提升。
3、WARNING: The option setting 'android.enableR8=true' is deprecated.
It will be removed in version 5.0 of the Android Gradle plugin.
You will no longer be able to disable R8
Build-tool 32.0.0 is missing DX at /Applications/Unity/Hub/Editor/2021.3.4f1c1/PlaybackEngines/AndroidPlayer/SDK/build-tools/32.0.0/dx
File /Users/edy/.android/repositories.cfg could not be loaded.
Build-tool 32.0.0 is missing DX at /Applications/Unity/Hub/Editor/2021.3.4f1c1/PlaybackEngines/AndroidPlayer/SDK/build-tools/32.0.0/dx
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':launcher:compileDebugJavaWithJavac'.
> Installed Build Tools revision 32.0.0 is corrupted. Remove and install again using the SDK Manager.
在尝试使用 32.0.0 代替 30.0.2 目录,并将 launcherTemplate 和 mainTemplate 的 buildToolsVersion =》32.0.0 时报错。
原因:Build Tool 31 以后从SDK内删除了dx工具,使用d8工具来替代dx工具。
解决:把build-tools\30.0.0目录下的dx和lib/dx.jar文件,复制到build-tools\32.0.0目录。
提升 Unity版本,涉及改动:
- 安装目标版本Unity,同时安装构建 Android和iOS所需模块。
- 使用此Unity打开老的Unity项目,查看内容变化,确保无报错。
- 提升开发人员Unity版本,(需提交的ProjectSettings变化、资源meta信息变化)
- 确认 gradle-plugin版本是否变化,若变化,是否致使 gradle版本变化。
- 配置打包脚本中指定版本的 unity 和 gradle。
- 重打资源、重打包
如何查看gradle版本是否变化?
查看根目录中的 build.gradle 中的 classpath。
由上方注释地址,查看Unity文档(注意切换到指定Unity版本的文档)
https://docs.unity3d.com/Manual/android-gradle-overview.html
https://docs.unity3d.com/2021.3/Documentation/Manual/android-gradle-overview.html
根据当前要提升的目标Unity版本:
unity 版本:2021.3.41f1c1
gradle-plugin版本:7.4.2
对应的 gradle 版本:7.5.1
在打包机(Mac)上安装指定版本Gradle:
选择对应Gradle版本:Gradle | Releases
按照安装文档:https://docs.gradle.org/7.5.1/userguide/installation.html
环境:需要java 8+
下载 gradle-7.5.1-bin.zip
❯ cd /Users/edy/Downloads //进入下载目录
❯ sudo mkdir /opt/gradle //创建目标目录
❯ sudo unzip -d /opt/gradle gradle-7.5.1-bin.zip //解压到目标目录
❯ ls /opt/gradle/gradle-7.5.1 //验证解压成功