Android - Android Studio 3.0 问题总览记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LABLENET/article/details/78391457

1.更新后Gradle Plugin 3.0 遇到的问题

问题

Error:(101, 0) Cannot set the value of read-only property 'outputFile' for A ...

解决

升级为 gradle plugin 3.0 后,打包的文件输出就出了问题:

原始为:

    //打包命名
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (variant.buildType.name == 'release') {
                variant.mergedFlavor.versionCode = getVersionCode(false)
                variant.mergedFlavor.versionName = getVersionName(false)
                // release
                def apkName = "${project.getName()}_${variant.flavorName}_${buildType.name}_v${variant.versionCode}.apk"
                       output.outputFile = new File(output.outputFile.parent, apkName)
            } else {
                variant.mergedFlavor.versionCode = getVersionCode(true)
                variant.mergedFlavor.versionName = getVersionName(true)
                // debug
               def apkName = "${project.getName()}_${buildType.name}.apk"
                      output.outputFile = new File(output.outputFile.parent, apkName)
            }
        }
    }

更新为:要输出的文件名称必须命名为 outputFileName

    //打包命名
    applicationVariants.all { variant ->
        variant.outputs.all {
            if (variant.buildType.name == 'release') {
                variant.mergedFlavor.versionCode = getVersionCode(false)
                variant.mergedFlavor.versionName = getVersionName(false)
                // release:输出文件名必须命名为 outputFileName
                outputFileName = "${project.getName()}_${variant.flavorName}_${buildType.name}_v${variant.versionCode}.apk"
//                        output.outputFile = new File(output.outputFile.parent, apkName)
            } else {
                variant.mergedFlavor.versionCode = getVersionCode(true)
                variant.mergedFlavor.versionName = getVersionName(true)
                // debug 输出文件名必须命名为 outputFileName
                outputFileName = "${project.getName()}_${buildType.name}.apk"
//                        output.outputFile = new File(output.outputFile.parent, apkName)
            }
        }
    }

2. 出现 AAPT2 错误

Error:(1875) resource color/blue (aka com.smartahc.android.insurance.beta:color/blue) not found.
Error:(1880) resource color/white (aka com.smartahc.android.insurance.beta:color/white) not found.
Error:(1882) resource color/white (aka com.smartahc.android.insurance.beta:color/white) not found.
Error:(1885) resource color/white (aka com.smartahc.android.insurance.beta:color/white) not found.
Error:failed linking references.
Error:java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:Execution failed for task ':eartag_main:processBetaReleaseResources'.
> Failed to execute aapt

解决

gradle.properties 中添加下面内容

#Tue Oct 17 10:08:09 CST 2017
android.enableAapt2=false

3. flavor dimension 配置问题

Error:A problem occurred configuring project ':eartag_main'.
> All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

解决

android{
  ...
  defaultconfig{
     ...
     ...
     flavorDimensions "tier"
  }
  ...

    productFlavors {
        beta {
            dimension "tier"
            ...
            ]
        }
        production {
            dimension "tier"
             ....
            ]
        }
    }


}

4. Unable to resolve dependency for compileClasspath

类似于下面错误


Unable to resolve dependency for ':eartag_main@betaProductionPyger/compileClasspath': Could not resolve project :eartag_base. Open File Show Details

解决

library 库依赖的时候,需要注意 build variant 使用方式:

这里写图片描述

  • 当 library 为 debug 的时候,使用 debugImplementation 引入;
  • 当 library 为 release 的时候,使用 implementation 引入;

5.依赖改变

  • implementation : 依赖 libs 和 第三方库
  • debugImplementation : 依赖 debug 版本
  • testImplementation : 依赖测试库
  • androidTestImplementation : 依赖 andrioid 测试库
  • api : 以前的 complie 用法;
    implementation project(':eartag_base')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

重点

看看 api 与 implementation 的使用对比,不然在项目中依赖 module 后,怎么都找不到 libs 中依赖的内容:

(来源与网上,侵权删)
这里写图片描述

最后保持 build variants 一直就行了

这里写图片描述

5.kotlin 配置

项目使用 databinding 了,然后更新后就不能用了,使用 kapt 进行引入 databinding

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
...

dependencies {
    // kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation libs.pyger
    kapt libs.databinding
    //    implementation smartahc.coreui
    implementation project(':eartag_base')
}

6.aapt 依赖库重复问题


Error:org.gradle.process.internal.ExecException: Process 'command '/Users/yuan/Library/Android/sdk/build-tools/26.0.2/aapt'' finished with non-zero exit value 1

如果自己配置 android 相关的库,就会出现重复依赖的情况,看这个是否重复依赖了相关库,如果自己使用第三方的已经配置了,那么久不需要自己进行配置了,或者自己配置成相同的版本;

这里写图片描述

7.更多配置设置见官网说明

gradle-plugin-3.0 migration

猜你喜欢

转载自blog.csdn.net/LABLENET/article/details/78391457