升級到 Android Studio 3.0 + Gradle 4.1 遇到的一些坑及解決方案


問題一:

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=commonDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl. Open File
  • 1

解決方案一:

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

I used this code

 applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { //輸出apk名稱為:應用名.apk def fileName = "xxx.apk" output.outputFile = new File(outputFile.parent, fileName) } } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

**Use all() instead of each() 
Use outputFileName instead of output.outputFile if you change only file name (that is your case)**

Example from the guide:

// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates // through only the objects that already exist during configuration time— // but those object don't exist at configuration time with the new model. // However, all() adapts to the new model by picking up object as they are // added during execution. android.applicationVariants.all { variant -> variant.outputs.all { outputFileName = "xxx.apk" } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

問題二:

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
  • 1

解決方案二:

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

大致是說,Plugin 3.0.0之后有一種自動匹配消耗庫的機制,便於debug variant 自動消耗一個庫,然后就是必須要所有的flavor 都屬於同一個維度。 
為了避免flavor 不同產生誤差的問題,應該在所有的庫模塊都使用同一個foo尺寸。

但是我們從中已經知道解決方案了:

在主 app 的 build.gradle 里面的

defaultConfig {
 targetSdkVersion:*** minSdkVersion :*** versionCode:*** versionName :*** //版本名后面加句話,意思是flavor dimension 它的維度就是該版本號, //這樣維度就是都是統一的了 **flavorDimensions "versionCode"** }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

問題三:

Error:Execution failed for task ':youyoubao:javaPreCompileCommonDebug'. > Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor. Please add them to the annotationProcessor configuration. - butterknife-compiler-8.6.0.jar (com.jakewharton:butterknife-compiler:8.6.0) Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior. Note that this option is deprecated and will be removed in the future. See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.
  • 1
  • 2
  • 3
  • 4
  • 5

解決方案三:

在 app 下的 build.gradle 的 defaultConfig 中加一句話:

defaultConfig {
   ... javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } } }


轉:
https://blog.csdn.net/CHITTY1993/article/details/78667069


 

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated

今天升級了AS3.0以后,在項目編譯的時候發現Gradle中報錯了,錯誤如下:
Error:(60, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=xiaomiRelease, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
<a href="openFile:E:\Studio\MyApplication\CodeBook\build.gradle">Open File</a>
經過一番折騰,網上找大牛的解讀,弄明白了output.outputFile變成了只讀屬性,不能再往里面寫東西了,以下是3.0之前的配置:
applicationVariants.all { variant ->    //批量修改Apk名字
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith('.apk') && 'release'.equals(variant.buildType.name)) {
            def fileName = outputFile.name.replace("${variant.flavorName}", "V${defaultConfig.versionName}-${variant.flavorName}")
            fileName = fileName.replace('.apk', "-${buildTime()}.apk")
            output.outputFile = new File(outputFile.parent, fileName)
        }
    }
}
下面是經過修改之后3.0里面批量修改APK名字的配置:
applicationVariants.all { variant ->    //批量修改Apk名字
    variant.outputs.all { output ->
        if (!variant.buildType.isDebuggable()) {
            //獲取簽名的名字 variant.signingConfig.name
            //要被替換的源字符串
            def sourceFile = "-${variant.flavorName}-${variant.buildType.name}"
            //替換的字符串
            def replaceFile = "_V${variant.versionName}_${variant.flavorName}_${variant.buildType.name}_${buildTime()}"
            outputFileName = output.outputFile.name.replace(sourceFile, replaceFile);
            //遺留問題:如何獲取當前module的name,如CodeBooke這個名字怎么獲取到
        }
    }
}
問題:對於如何在gradle中獲取module的name,還是沒有找到相關的方法,希望有知道的大神留言交流。
這是一個對AS 3.0變化總結比較全的博客:
 


免責聲明!

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



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