更新Android Studio 3.0碰到的問題


更新完后試下運行正在維護的舊項目,出現各種錯誤,因為后來發現問題不在這,所以沒記完整,大概如下:

A larger heap for the Gradle daemon is recommended for running jack.

It currently has 512 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to at least 1536 MB.
To do this set org.gradle.jvmargs=-Xmx1536M in the project gradle.properties.

然后查看gradle.properties我的org.gradle.jvmargs已經是2048了,另外還有個Error:UNEXPECTED TOP-LEVEL ERROR:錯誤

搜索了一下並沒找到要點,於是Build一下項目,提示

Error:Failed to complete Gradle execution.

Cause:
The version of Gradle you are using (3.3) does not support the forTasks() method on BuildActionExecuter. Support for this is available in Gradle 3.5 and all later versions.

於是更新使用Gradle最新版,新建一個項目看看默認用的什么版本,修改以下文件

gradle-wrapper.properties里

distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

 

APP的build.gradle里

dependencies {
  classpath 'com.android.tools.build:gradle:3.0.0'
}

 

 

編譯后又有錯

Cannot set the value of read-only property 'outputFile'....

因為使用了編譯輸出文件名的代碼

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        def fileName
        if (outputFile != null && outputFile.name.endsWith('.apk')) {
            if (variant.buildType.name.equals('release')) {
                // 輸出apk名稱為Test_v_1.0_15-09-15 11:12:32_official_release.apk
                fileName = "Test_v_${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}_release.apk"
            } else if (variant.buildType.name.equals('debug')) {
                // 輸出apk名稱為Test_v_1.0_15-09-15 11:12:32_official_debug.apk
                fileName = "Test_v_${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}_debug.apk"
            }
            output.outputFile = new File(outputFile.parent, fileName)
        }
    }
}

出錯在最后一行,應該是新版Gradle的問題,最后在Stackover Flow找到解決辦法,參考

https://stackoverflow.com/questions/44239235/android-gradle-3-0-0-alpha2-plugin-cannot-set-the-value-of-read-only-property

把 variant.outputs.each 改成

     variant.outputs.all

最后一行改成

outputFileName = fileName

 

 

繼續編譯繼續出錯

Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

百度找到以下文章,修改后就可以正常編譯運行了

http://blog.csdn.net/syif88/article/details/75009663

看了幾個文章也搞不清楚這個是什么來的,照着改項目的build.gradle

defaultConfig {
    targetSdkVersion:***
    minSdkVersion :***
    versionCode:***
    versionName :***
    //版本名后面添加一句話,意思就是flavor dimension 它的維度就是該版本號,這樣維度就是都是統一的了
    flavorDimensions "versionCode"
}

 

項目的其它代碼不用改,文章開頭的錯誤全沒了,還好沒浪費太多時間

 


免責聲明!

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



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