AndroidStudio2.2.1之后升级到AndroidStudio3.0.1版本 问题

之前用的是AndroidStudio2.2.1之后升级到AndroidStudio3.0.1版本 
遇到如下问题: 
1,Declaring custom ‘clean’ task when using

Error:(48, 1) A problem occurred evaluating root project 'NEWAPP'.
> Declaring custom 'clean' task when using the standard Gradle lifecycle plugins is not allowed.
  • 1
  • 2

解决方法: 
来自:http://blog.csdn.net/poorkick/article/details/72599929 
把build.gradle中的clean task 无情地注释掉。

//task clean(type: Delete) {
//   delete rootProject.buildDir
//}
  • 1
  • 2
  • 3

2,No service of type Factory

Error:No service of type Factory<LoggingManagerInternal> available in ProjectScopeServices.
  • 1

clean项目。出现问题3 
3,Could not create plugin of type ‘AndroidMavenPlugin’.

Warning:The specified Android SDK Build Tools version (24.0.2) is ignored, as it is below the minimum supported version (26.0.2) for Android Gradle Plugin 3.0.1.
Android SDK Build Tools 26.0.2 will be used.
To suppress this warning, remove "buildToolsVersion '24.0.2'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
Information:BUILD FAILED in 2s
Error:FAILURE: Build failed with an exception.

* Where:
Script 'https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.0.0/gradle-android-javadocs.gradle' line: 17

* What went wrong:
A problem occurred evaluating script.
> Failed to apply plugin [id 'com.github.dcendents.android-maven']
   > Could not create plugin of type 'AndroidMavenPlugin'.
      > No service of type Factory<LoggingManagerInternal> available in ProjectScopeServices.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

解决办法: 
来自:http://blog.csdn.net/suwenlai/article/details/55046644 

dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath group: 'com.zeroc.gradle.ice-builder', name: 'slice', version: '1.3.14'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4'
    }
  • 1
  • 2
  • 3
  • 4
  • 5

之前classpath ‘com.github.dcendents:android-maven-gradle-plugin:1.4’是1.3版本,换成1.4 
4,换buildToolsVersion “26.0.2”

build.gradle里的buildToolsVersion在gradle3.0.1中对应的是26.0.2
buildToolsVersion "26.0.2"

这个问题是Android studio升级到3.0之后,运行的时候会提示gradle要升级到3.5版本才能编译。于是我把我的gradle升级到了
 gradle-4.1-milestone-1 版本,是2017年7月份最新版本了。
于是我把主程序的build.gradle中的gradle版本改成了这个,具体指定哪个版本我也不知道,于是就写了个3.0+
 dependencies {        classpath 'com.android.tools.build:gradle: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 includes a new dependency mechanism that automatically matches variants when consuming a library. This means an app's debug variant automatically consumes a library's debug variant, and so on. It also works when using flavors—an app's redDebug variant will consume a library's redDebug variant. To make this work, the plugin now requires that all flavors belong to a named flavor dimension —even if you intend to use only a single dimension. Otherwise, you will get the following build error:
[cpp]  view plain  copy
  1. Error:All flavors must now belong to a named flavor dimension.  
  2. The flavor 'flavor_name' is not assigned to a flavor dimension.  
 To resolve this error, assign each flavor to a named dimension, as shown in the sample below. Because dependency matching is now taken care of by the plugin, you should name your flavor dimensions carefully. For example, if all your app and library modules use the foo dimension, you'll have less control over which flavors are matched by the plugin. 
[cpp]  view plain  copy
  1. // Specifies a flavor dimension.  
  2. flavorDimensions "color"  
  3.   
  4. productFlavors {  
  5.      red {  
  6.       // Assigns this product flavor to the 'color' flavor dimension.  
  7.       // This step is optional if you are using only one dimension.  
  8.       dimension "color"  
  9.       ...  
  10.     }  
  11.   
  12.     blue {  
  13.       dimension "color"  
  14.       ...  
  15.     }  
  16. }  
大致是说,Plugin 3.0.0之后有一种自动匹配消耗库的机制,便于debug variant 自动消耗一个库,然后就是必须要所有的flavor 都属于同一个维度。
为了避免flavor 不同产生误差的问题,应该在所有的库模块都使用同一个foo尺寸。
= 。=还是懵逼。说一堆依然不是很理解。
但是我们从中已经知道解决方案了:
在主app的build.gradle里面的
 defaultConfig {
 targetSdkVersion:***minSdkVersion :***
versionCode:***
 versionName :***
//版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
flavorDimensions "versionCode"
}
就直接解决这个问题。然后app 就可以happy的运行起来了。

5.体验最新版AndroidStudio3.0 Canary 8的时候,发现之前项目的butter knife报错,用到注解的应该都会报错
Error:Execution failed for task ':app:javaPreCompileDebug'.
> 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-7.0.1.jar
  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.

appbuildandroid {
    ...
    defaultConfig {
        ...
        //添加如下配置就OKjavaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
    }
    ...
}

6.详细报错:

Error:(45, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=xiaomiDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
<a href="openFile:H:\CloudMus\app\build.gradle">Open File</a>
  • 1
  • 2

位置:

 // 自定义输出配置
//            applicationVariants.all { variant ->
//                variant.outputs.each { output ->
//                    def outputFile = output.outputFile
//                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
//                        // 输出apk名称为great_v1.0_wandoujia.apk
//                        def fileName = "great${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
//                        output.outputFile = new File(outputFile.parent, fileName)
//                    }
//                }
//            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Android plugin 3.0 建议使用

Use all() instead of each()
Use outputFileName instead of output.outputFile if you change only file name (that is your case)

例子:

// 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 = "${variant.name}-${variant.versionName}.apk"
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

故而修改上面的写法:

applicationVariants.all { variant ->
                variant.outputs.all {
                        // 输出apk名称为great_v1.0_wandoujia.apk
                        def fileName = "great${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
                        outputFileName = fileName

                }
            }
7.Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForAlibabaDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

解决:Multidex enable true或者clean rebuild 项目

猜你喜欢

转载自blog.csdn.net/ltp_zy/article/details/79482722