当前位置:   article > 正文

Unity使用Gradle出apk包报错_a problem occurred configuring root project 'gradl

a problem occurred configuring root project 'gradle'. > could not resolve al

问题记录:A problem occurred configuring root project ‘Gradle‘.

unity2022.3.8f1c1 使用 Gradle 出包。(2019以上无法选择internal)

一般表现是打包最后一步会等待超长时间,最后报错,错误信息:

  1. Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
  2. FAILURE: Build failed with an exception.
  3. * What went wrong:
  4. A problem occurred configuring root project 'Gradle'.
  5. > Could not resolve all artifacts for configuration ':classpath'.
  6. > Could not download fastutil-8.4.0.jar (it.unimi.dsi:fastutil:8.4.0)
  7. > Could not get resource 'https://plugins.gradle.org/m2/it/unimi/dsi/fastutil/8.4.0/fastutil-8.4.0.jar'.
  8. > Premature end of Content-Length delimited message body (expected: 19,052,190; received: 6,553,552)
  9. * Try:
  10. 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.
  11. * Get more help at https://help.gradle.org

最开始有遇到问题:Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for detailsd,这个据说是RAM不足会影响的,简单清理以后就好了,之后遇到的问题如下:

*What went wrong:
A problem occurred configuring root project 'Gradle'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not download fastutil-8.4.0.jar (it.unimi.dsi:fastutil:8.4.0)
      > Could not get resource 'https://plugins.gradle.org/m2/it/unimi/dsi/fastutil/8.4.0/fastutil-8.4.0.jar'.
         > Premature end of Content-Length delimited message body (expected: 19,052,190; received: 12,844,960)

发现自己的Gradlle配置可能存在问题,故进行了如下操作:

我的是在:E:\Unity 3D\Unity\Installs\2022.3.8f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates,这个文件夹下所有文件中的repositories {**ARTIFACTORYREPOSITORY**这句话的后面都加上maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}(我的只有settingTemplate中有),但又有如下错误:

Error resolving plugin [id: 'com.android.application', version: '7.1.2', apply: false]
> Could not resolve all dependencies for configuration 'detachedConfiguration1'.
   > Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven(http://maven.aliyun.com/nexus/content/groups/public/)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.2/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details. 

出现了新的问题,证明改动的位置有效。

接下来的解决办法是:在settingsTemplate.gradle的文件中,在repositories的闭包中添加:

maven { url "https://jitpack.io" }
maven { url 'https://maven.aliyun.com/repository/releases' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url 'https://maven.aliyun.com/repository/public' }
该方法参考:http://t.csdnimg.cn/3BI8n

可以去掉这些“**ARTIFACTORYREPOSITORY**”,

最终settingTemplate.gradle内容如下:

pluginManagement {
    repositories {
        maven { url "https://jitpack.io" }
        maven { url 'https://maven.aliyun.com/repository/releases' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/central' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
 
include ':launcher', ':unityLibrary'
**INCLUDES**
 
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        maven { url "https://jitpack.io" }
        maven { url 'https://maven.aliyun.com/repository/releases' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/central' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        google()
        mavenCentral()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}

最终apk很快输出成功,问题解决!

解决方案:

在 Build Settings 左下角打开 Player Settings,在 Android 平台图标选项卡下找到 Publishing Settings 区域

勾选 Custom Gradle Settings Template

根据下面提示的路径打开 settingsTemplate.gradle 文件,修改其内容:

  1. pluginManagement {
  2. repositories {
  3. **ARTIFACTORYREPOSITORY**
  4. maven {
  5. url 'https://mirrors.huaweicloud.com/repository/maven/'
  6. }
  7. //gradlePluginPortal()
  8. //google()
  9. //mavenCentral()
  10. }
  11. }
  12. include ':launcher', ':unityLibrary'
  13. **INCLUDES**
  14. dependencyResolutionManagement {
  15. repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
  16. repositories {
  17. **ARTIFACTORYREPOSITORY**
  18. maven {
  19. url 'https://mirrors.huaweicloud.com/repository/maven/'
  20. }
  21. //google()
  22. //mavenCentral()
  23. flatDir {
  24. dirs "${project(':unityLibrary').projectDir}/libs"
  25. }
  26. }
  27. }

网上多是使用的阿里云镜像,但试了一下找不到相关包,故此改为华为云,如果华为云的也不能使用了,以下提供更多源备选:

阿里云:http://maven.aliyun.com/
中央仓库:https://repo1.maven.org/maven2/
网易:http://maven.netease.com/repository/public/
华为云:https://repo.huaweicloud.com/repository/maven/
腾讯云:https://mirrors.cloud.tencent.com/repository/maven/
中国科技大学:http://mirrors.ustc.edu.cn/maven/maven2/
南京大学:http://maven.nju.edu.cn/repository/
清华大学:https://repo.maven.apache.org/maven2/
北京理工大学:http://mirror.bit.edu.cn/maven/
东软信息学院:https://mirrors.neusoft.edu.cn/maven2/
中国科学院开源协会:http://maven.opencas.cn/maven/
北京交通大学:http://maven.bjtu.edu.cn/maven2/

————————————————
版权声明:本文为CSDN博主「--北极兔--」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/m0_62592894/article/details/133818038

如果想要全局配置镜像源地址,可以找到模板文件

# 参考路径,请根据自己实际安装的 Unity 版本和路径进行查找
C:\Program Files\Unity\Hub\Editor\2022.3.8f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates\settingsTemplate.gradle

修改该路径的文件内容即可。

另外如果通过这步以后,还提示以下错误:

  1. Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
  2. D:\Develop\Unity\HRVTest\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\src\main\java\com\unity\androidnotifications\UnityNotificationManager.java:164: 错误: 找不到符号
  3. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
  4. ^
  5. 符号: 变量 TIRAMISU
  6. 位置: 类 VERSION_CODES
  7. D:\Develop\Unity\HRVTest\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\src\main\java\com\unity\androidnotifications\UnityNotificationManager.java:165: 错误: 找不到符号
  8. permissionGranted = mContext.checkCallingOrSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED;
  9. ^
  10. 符号: 变量 POST_NOTIFICATIONS
  11. 位置: 类 permission
  12. 注: D:\Develop\Unity\HRVTest\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\src\main\java\com\unity3d\player\UnityPlayerActivity.java使用或覆盖了已过时的 API。
  13. 注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
  14. 注: 某些输入文件使用了未经检查或不安全的操作。
  15. 注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
  16. 2 个错误
  17. FAILURE: Build failed with an exception.
  18. * What went wrong:
  19. Execution failed for task ':unityLibrary:compileReleaseJavaWithJavac'.
  20. > Compilation failed; see the compiler error output for details.
  21. * Try:
  22. 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.
  23. * Get more help at https://help.gradle.org

TIRAMISU 是 Android 13 的代号 提拉米苏,应该是本地的 SDK 版本较低,请升级到相应的最新版本即可解决此错误

  1. # 参考命令(也可使用管理员身份运行 Unity,在安卓平台的 Target API Level 配置中选择 33 进行 Build 时,会自动提示需要升级 SDK,以下命令就提取自其错误提示)
  2. # 进入 sdk 安装目录
  3. cd "C:\Program Files\Unity\Hub\Editor\2022.3.8f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\cmdline-tools\6.0\bin"
  4. # 执行升级指令(Android 13 对应 sdk 版本为 33)
  5. sdkmanager.bat "platforms;android-33"

参考:https://blog.csdn.net/linxinfa/article/details/118553713

https://blog.csdn.net/qq_38217990/article/details/129257106

https://www.cnblogs.com/xwgli/p/17789215.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/104943
推荐阅读
相关标签
  

闽ICP备14008679号