升级Gradle 3.0遇到的坑

来源:互联网 发布:淘宝客贷款哪里进 编辑:程序博客网 时间:2024/06/13 11:23

近来因为项目需要升级到了gradle 3.0,升级之后编译速度提升了很多,这里总结一下升级过程遇到过的坑。

Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'. Possible causes for this unexpected error include:Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network)The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem. Stop Gradle build processes (requires restart)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.In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
  • 报错信息:找不到方法com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;
  • 解决方法:butterknife的gradle插件还未支持gradle3.0,回退到butterknife 8.4.0版本可以规避该问题,使用中发现8.4.0版本还有有些严重的坑的,尤其是对library的支持上。12月7号更新:现在butterknife的9.0.0-SNAPSHOT已经支持了gradle3.0,可以升级到此版本,不急着升级的可以等9.0.0 release之后再升级。9.0.0-SNAPSHOT对应的maven地址maven { url “https://oss.sonatype.org/content/repositories/snapshots” }
 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.    - xxx.jar (xxx)    - auto-service-1.0-rc3.jar (com.google.auto.service:auto-service:1.0-rc3)  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.
  • 报错信息:Annotation processors如果有传递依赖的话,现在必须要显式声明,或者配置includeCompileClasspath为true
  • 解决方法:显式声明即可,不推荐配置includeCompileClasspath的方法,因为后续版本有可能会删掉这个配置项
- Configuration 'debugApiElements':         - Required com.android.build.api.attributes.BuildTypeAttr 'predeploy' and found incompatible value 'debug'.         - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.         - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.         - Required org.gradle.api.attributes.Usage 'java-api' and found compatible value 'java-api'.
  • 报错信息:library中缺少BuildTypeAttr配置
  • 解决方法:gradle 3.0会检查library的配置,之前不会,如果再application中添加了自定义的buildType,那么在library中,也要添加相同的buildType,不过配置项可以为空,如下
    buildTypes {
    predeploy {
    }
    }
Error:Execution failed for task ':ppdBiz-common:mockableAndroidJar'.> Output file [/Users/fengrui/code/Lender-Android-mobi/libs_ppd/ppdBiz-common/build/generated/mockable-android-26.v3.jar] already exists.
  • 报错信息:mockable-android-26.v3.jar已存在
  • 解决方法:原因未知。在同步和编译中有时候会报这个错误,clean project后再次同步/编译即可
Execution failed for task ':app:transformClassesWithDexBuilderForDebug'.> Unexpected scopes found in folder '/Users/fengrui/code/Lender-Android-mobi/app/build/intermediates/transforms/AspectTransform/debug'. Required: PROJECT, SUB_PROJECTS, EXTERNAL_LIBRARIES. Found: EXTERNAL_LIBRARIES, PROJECT, PROJECT_LOCAL_DEPS, SUB_PROJECTS, SUB_PROJECTS_LOCAL_DEPS
  • 报错信息:transform scopes类型不对
  • 解决方法:gradle 3.0的transfor api修改了QualifiedContent.Scope的定义,需要修改transform的实现进行兼容。如果是自己的gradle插件的话,可以参考下面的代码进行修改。如果是依赖的第三方的插件的话,需要将插件升级到支持gradle 3.0的版本。我用到的第三方插件是神策埋点,将神策的插件升级到1.2.0即可
// transform兼容gradle 3.0@OverrideSet<QualifiedContent.Scope> getScopes() {    def name = QualifiedContent.Scope.PROJECT_LOCAL_DEPS.name()    def deprecated = QualifiedContent.Scope.PROJECT_LOCAL_DEPS.getClass()            .getField(name).getAnnotation(Deprecated.class)    if (deprecated == null) {        println "cannot find QualifiedContent.Scope.PROJECT_LOCAL_DEPS Deprecated.class "        return ImmutableSet.<QualifiedContent.Scope> of(QualifiedContent.Scope.PROJECT                , QualifiedContent.Scope.PROJECT_LOCAL_DEPS                , QualifiedContent.Scope.EXTERNAL_LIBRARIES                , QualifiedContent.Scope.SUB_PROJECTS                , QualifiedContent.Scope.SUB_PROJECTS_LOCAL_DEPS)    } else {        println "find QualifiedContent.Scope.PROJECT_LOCAL_DEPS Deprecated.class "        return ImmutableSet.<QualifiedContent.Scope> of(QualifiedContent.Scope.PROJECT                , QualifiedContent.Scope.EXTERNAL_LIBRARIES                , QualifiedContent.Scope.SUB_PROJECTS)    }}
原创粉丝点击