Android studio 升級3.0后出現的bug解決辦法!

android studio 提示有3.0更新,強迫癥就直接升級了,結果就尷尬了!
出現問題然后就去找解決辦法:看到這篇文章
http://blog.csdn.net/qqcrazyboy/article/details/77900183

內容如下:
1.現象描述
原來項目在Android studio 2.3一切正常,升級3.0之后報如下錯誤:

Error:Cannot choose between the following configurations of project :android_sdk: - debugApiElements - debugRuntimeElements - releaseApiElements - releaseRuntimeElementsAll of them match the consumer attributes: - Configuration 'debugApiElements': - Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required. - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required. - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required. - Found org.gradle.api.attributes.Usage 'java-api' but wasn't required. - Configuration 'debugRuntimeElements': - Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required. - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required. - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required. - Found org.gradle.api.attributes.Usage 'java-runtime' but wasn't required. - Configuration 'releaseApiElements': - Found com.android.build.api.attributes.BuildTypeAttr 'release' but wasn't required. - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required. - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required. - Found org.gradle.api.attributes.Usage 'java-api' but wasn't required. - Configuration 'releaseRuntimeElements': - Found com.android.build.api.attributes.BuildTypeAttr 'release' but wasn't required. - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but wasn't required. - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required. - Found org.gradle.api.attributes.Usage 'java-runtime' but wasn't required.

2.解決辦法:
project的build.gradle
文件中刪除

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

module的build.gradle
文件中刪除

apply plugin: 'com.neenbedankt.android-apt'

module的build.gradle
文件中替換

//apt 'com.jakewharton:butterknife-compiler:8.0.1
'annotationProcessor 'com.jakewharton:butterknife-compiler:8.0.1'

然后又出現了新的問題:

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

然后又百度找解決辦法,真的是急死我了。終于看到這篇文章:
http://blog.csdn.net/syif88/article/details/75009663

內容如下:
這個問題是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

Error:All flavors must now belong to a named flavor dimension.
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

// Specifies a flavor dimension.
flavorDimensions "color"

productFlavors {
red {
// Assigns this product flavor to the 'color' flavor dimension.
// This step is optional if you are using only one dimension.
dimension "color"
...
}

blue {  
  dimension "color"  
  ...  
}  

}

大致是說,Plugin 3.0.0之后有一種自動匹配消耗庫的機制,便于debug variant 自動消耗一個庫,然后就是必須要所有的flavor 都屬于同一個維度。
為了避免flavor 不同產生誤差的問題,應該在所有的庫模塊都使用同一個foo尺寸。
= 。=還是懵逼。說一堆依然不是很理解。
但是我們從中已經知道解決方案了:
在主app的build.gradle里面的

defaultConfig {
targetSdkVersion:minSdkVersion :
versionCode:***
versionName :***

//版本名后面添加一句話,意思就是flavor dimension 它的維度就是該版本號,這樣維度就是都是統一的了
flavorDimensions "versionCode"
}

就直接解決這個問題。然后app 就可以happy的運行起來了。

但是很不幸構建是成功了,但是運行時又出現了這個問題:

Error:Execution failed for task ':data: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.
- auto-value-gson-0.4.2.jar (com.ryanharter.auto.value:auto-value-gson:0.4.2)
- auto-value-1.3-rc1.jar (com.google.auto.value:auto-value:1.3-rc1)
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.

然后還是找解決辦法,終于發現這篇文章,
http://blog.csdn.net/keep_holding_on/article/details/76188657
嘗試一波:
Mr.Smile填坑記——AndroidStudio3.0 Canary 8注解報錯Annotation processors must be explicitly declared now.

體驗最新版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.

在app的build中android { ...
defaultConfig { ...

//添加如下配置就OK了

javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }

} ...
}

最后終于可以運行了!最后的最后,想用一首"涼涼的"送給android studio團隊,你要讓我們android開發者怎么樣???雖說是升級,可是制造這么多麻煩真的好么?希望這篇文章能對各位有所幫助,繼續努力嘍!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容