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

來源:互聯網
上載者:User

標籤:during   string   大致   nal   cas   字串   相關   sha   nta   

問題一:
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 } }}


轉:
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,還是沒有找到相關的方法,希望有知道的大神留言交流。gradle API:https://developer.android.google.cn/studio/build/gradle-plugin-3-0-0-migration.html#variant_api這是一個對AS 3.0變化總結比較全的部落格:https://mp.weixin.qq.com/s?__biz=MzAwNzc0NjAxMg==&mid=2653391711&idx=1&sn=821eff3f98ab52fc442c6218794b203e&chksm=80aa53ecb7dddafa522d4ebfdf67af2f473365bb0fa2d50ae6df5d00499926ae0d32a5a448d2&mpshare=1&scene=23&srcid=10308c4ICo7GCwuVc683H6eZ#rd
 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.