build.gradle 各種錯誤解決總結


前言:自從用上Android studio 之后,遇到各種gradle 的問題,前一段時間,把我經常遇到的問題總結了一下 ,大部分問題是Google 查到了,親測可用之后,總結分享出來。也感謝這些前輩們處理完這些問題留下的寶貴經驗總結。

1.Gradle DSL method not found runProguard()

從字面就能看出來,出現這個問題的原因是找不到runProguard()這個方法了(調用這個方法的地方在每個Module對應的build.gradle文件中)。
這是因為,當Android Studio升級時,也自動的將項目下的build.gradle文件(不是Module對應的build.gradle!)下的內容修改成了:


   dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0-rc4'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

而升級之前是這樣的(以我的電腦為例,應該是類似的):

    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

顯然,最簡單的解決方法是將gradle的版本改回去,此方法可能的確有效。
但是,這種做法顯然不是最好的方法。查閱官方文檔之后(http://tools.android.com/tech-docs/new-build-system)之后發現,在新版本的gradle中,runProguard這個方法已經廢棄了,並且改為新的方法了:minifyEnabled. 因此,正確的解決方法不是修改gradle的版本號,而是將項目中每個Module對應的build.gradle文件中的runProguard方法名改為minifyEnabled,即:

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

問題完美解決。


2.Error Plugin with id android-apt not found.note

刪除 Plugin with id ‘android-apt’ not found


3.Could not find property outputFile on com android build gradle.note

Android studio從1.0 RC 4升級到1.0(其實就是打了一個8M的patch)后,這個時候相應的gradle的版本也會直接使用“com.android.tools.build:gradle:1.0.0”,如果這時你在gradle文件中又用到outputFile就會出現上述的問題。好吧,其實這也是gradle團隊搞的問題,有時候我們多希望gradle能像android一樣,對舊版本有一個非常好的兼容性。
廢話不多說,直接說怎么解決這個問題吧,這個問題的原因是outputFile這個函數被換地方了。
old:

applicationVariants.all { variant -> ......
            variant.outputFile = new File(variant.outputFile.parent, name);
        ......
            }

}

new:

applicationVariants.all { variant -> ......
            variant.outputs.each { output -> output.outputFile = new File(output.outputFile.parent, name);
        ......
            }

}

按上述方式改就ok了。

4.Gradle DSL method not found android().note

解決方法:
刪掉最外層的build.gradle中的

這里寫圖片描述

android {
    compileSdkVersion 19
    buildToolsVersion '21.1.1'
}
后續問題總結將繼續更新

版權聲明:本文為博主原創文章,未經博主允許不得轉載。(轉載請注明出自 AllenCoder)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM