Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 390|回复: 0

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

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-5-20 13:50:10 | 显示全部楼层 |阅读模式

    问题一:

    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变化总结比较全的博客:
     
    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-19 16:02 , Processed in 0.073269 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表