天天看點

Unity 之 解決2019之後的版本打apk卡到Building Gradle project問題

# 一,遇到問題

安裝環境:安裝Android Build Support;【需要将下拉菜單的兩項一并安裝】

Unity 之 解決2019之後的版本打apk卡到Building Gradle project問題

問題:

Unity2019 版本打包apk,卡到Building Gradle project這一直不動,長時間等待後打包失敗

Unity 之 解決2019之後的版本打apk卡到Building Gradle project問題

# 二,分析問題

失敗原因:是在下載下傳的對應版本Gradle時,需要通路外網下載下傳速度慢或者網絡不穩定導緻失敗

# 三,解決問題

方法一:修改配置檔案

1,找到Untiy安裝目錄下的AndroidPlayer\Tools\GradleTemplates檔案夾下的 baseProjectTemplate.gradle 檔案

參考我的目錄:

D:\Program Files\Unity\2019.4.19f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates

Unity 之 解決2019之後的版本打apk卡到Building Gradle project問題
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

allprojects {
    buildscript {
        repositories {**ARTIFACTORYREPOSITORY**
            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:3.4.0'
            **BUILD_SCRIPT_DEPS**
        }
    }

    repositories {**ARTIFACTORYREPOSITORY**
        google()
        jcenter()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
           

修改為:

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

allprojects {
    buildscript {
        repositories {**ARTIFACTORYREPOSITORY**
            //google()
            //jcenter()
           maven{ url 'http://maven.aliyun.com/repository/google'}           
           maven{ url 'http://maven.aliyun.com/repository/gradle-plugin'}
           maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
           maven{ url 'http://maven.aliyun.com/repository/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:3.4.0'
            **BUILD_SCRIPT_DEPS**
        }
    }

    repositories {**ARTIFACTORYREPOSITORY**
        //google()
        //jcenter()
        maven{ url 'http://maven.aliyun.com/repository/google'}           
        maven{ url 'http://maven.aliyun.com/repository/gradle-plugin'}
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        maven{ url 'http://maven.aliyun.com/repository/jcenter'}

        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
           

然後重新打包即可。

方法二:修改Preferences屬性

在官網上提前下載下傳好需要版本gradle:

https://gradle.org/releases/

然後在Preferences面闆上,将Gradle Installed...選項前面√取消勾選,然後點選“Browse”,找到自己下載下傳好的gradle,然後重新打包就可以了。

Unity 之 解決2019之後的版本打apk卡到Building Gradle project問題

繼續閱讀