Android Studio 3.0 使用的一些小坑错误(持续更新)

  Android Studio 更新到3.0以后,gradle也升级到了4.1,程序员得适应这种变化,但是也带来一些问题,需要自己注意,这里记录下,出现的错误会持续更新的。

《Android Studio 3.0 使用的一些小坑错误(持续更新)》

1. 错误一:

《Android Studio 3.0 使用的一些小坑错误(持续更新)》

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

这个错误的意思:所有的flavors都必须属于同一个风格。然后科学上网一下,看了解释还是有点懵逼。
大致意思是Plugin 3.0.0之后有一种自动匹配消耗库的机制,便于debug variant 自动消耗一个库,然后就是必须要所有的flavor 都属于同一个维度。

为了避免flavor 不同产生误差的问题,应该在所有的库模块都使用同一个foo尺寸。
解决办法:在你的主App的Builder.gradler文件中

defaultConfig {
targetSdkVersion:***
minSdkVersion :***
versionCode:***
versionName :***
//版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
flavorDimensions “versionCode”
}
例如:

 defaultConfig {
        applicationId "com.demo.xxxx"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        flavorDimensions "versionCode"
    }

2. 错误二:

《Android Studio 3.0 使用的一些小坑错误(持续更新)》

Error:Removing unused resources requires unused code shrinking to be turned on. See http://d.android.com/r/tools/shrink-resources.html for more information.

这个错误的意思是当你想将项目里面的无用的资源移除的时候,资源压缩只与代码压缩协同工作。

代码压缩器移除所有未使用的代码后,资源压缩器便可确定应用仍然使用的资源。这在您添加包含资源的代码库时体现得尤为明显 – 您必须移除未使用的库代码,使库资源变为未引用资源,才能通过资源压缩器将它们移除。

要启用资源压缩,请在 build.gradle 文件中将 shrinkResources 属性设置为 true(在用于代码压缩的 minifyEnabled 旁边)。例如:

 buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }

测试了一下,要压缩资源的时候,minifyEnabled 和shrinkResources 都必须为true。

3. 错误三:

《Android Studio 3.0 使用的一些小坑错误(持续更新)》

Error:org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
<a href="syncProject">Re-download dependencies and sync project (requires network)</a></li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
<a href="stopGradleDaemons">Stop Gradle build processes (requires restart)</a></li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

下载了一个项目到本地运行的时候发现这么一个错误,然后检查发现是gradle的问题
解决办法:
下载的项目的:
project:build.gradle :

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

自己本地的gradle是:
project:build.gradle:

   classpath 'com.android.tools.build:gradle:2.3.3'

拷贝过去,替换一下,编译,运行,没问题了

    原文作者:忆念成风
    原文地址: https://www.jianshu.com/p/1b4e53fc9cda
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞