Android Studio 升級3.0 ,填坑記錄
Android Studio的自帶Gradle版本是4.1,外掛版本是3.0.0,所以如果你使用的是老版本,就會出現一些小的相容問題,我們看看報了哪些錯誤呢:
問題1
Error:(72, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=appDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
outputFile是隻讀屬性,不可以對他進行修改
看一下我的gradle裡面的程式碼:
?1234567891011121314151617181920212223242526 | // 定義生成的apk的名稱 def apkName; buildTypes { release { ... // 定義release版本生成的apk的名字 apkName = "xxx" + VERSION_NAME + "_release.apk"; } debug { ... // 定義debug版本生成的apk的名字 apkName = "ugirls_" + VERSION_NAME + "_debug.apk"; } } // 修改apk build的名字 android.applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { //這裡使用之前定義apk檔名稱 output.outputFile = new File(outputFile.parent, apkName) } } } |
這段程式碼的功能是修改Build命令生成的apk的名稱,因為outputFile變成只讀屬性,所以報錯。
修改後:
?12345678910 | // 之前程式碼保持不變 // 修改apk build的名字 android.applicationVariants.all { variant -> variant.outputs.all { if (outputFileName.endsWith('.apk')) { //這裡使用之前定義apk檔名稱 outputFileName = apkName } } } |
把each修改為all,然後通過outputFileName修改生成的apk的名稱。
如果你提示沒有找到all方法或者是未找到outputFileName,你可以先把這個功能註釋掉,等其他問題都解決了,再開啟就可以解決這個問題了。
問題2
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
所有的flavor屬性應當屬於同一個名稱空間
看著問題似乎有點深奧,其實就是需要我們為flavors設定一個版本,統一使用相同版本的flavors。
?12345678 | defaultConfig { targetSdkVersion:*** minSdkVersion :*** versionCode:*** versionName :*** //為flavor設定一個版本,命名是隨意的 flavorDimensions "versionCode" } |
問題3
有些庫不能被正常引用,例如我使用的multidex,在上面的截圖中已經提示我們如何解決這個問題
?12345678910 | buildscript { repositories { ... // 新增google庫的依賴 google() } dependencies { ... } } |
問題4
Error:(2638) error: style attribute '@android:attr/windowEnterAnimation' not found.
提示我們找不到@android:attr/windowEnterAnimation,因為已經不支援@開頭使用android自帶的屬性,我們只要把@符號刪掉就可以了。
修改前:
?1234 | < style name = "ToastStyle" parent = "android:Animation" > < item name = "@android:windowEnterAnimation" >@anim/push_fade_in</ item > < item name = "@android:windowExitAnimation" >@anim/push_fade_out</ item > </ style > |
修改後:
?1234 | < style name = "ToastStyle" parent = "android:Animation" > < item name = "android:windowEnterAnimation" >@anim/push_fade_in</ item > < item name = "android:windowExitAnimation" >@anim/push_fade_out</ item > </ style > |
問題5
Error:Execution failed for task ':app:javaPreCompileAppDebug'.
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-6.1.0.jar (com.jakewharton:butterknife:6.1.0)
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.
好多的錯誤日誌啊,其實最關鍵的只有前兩行:
使用註解編譯庫,需要顯示的宣告,而我正在使用的butterknife是含有註解編譯功能的,但是並沒有宣告。
解決辦法:
?12345678 | android { defaultConfig { // 宣告需要使用註解功能 javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } } ... } } |
其他的變化
通過剛才的修改,我的工程已經執行起來了,但是發現了Android Studio 3.0 的幾個小變化。
變化1
Warning:One of the plugins you are using supports Java 8 language features. To try the support built into the Android plugin, remove the following from your build.gradle:
apply plugin: 'me.tatarka.retrolambda'
從警告上看,希望我移除這個外掛,於是我到官網上查看了一下資訊:
If Android Studio detects that your project is using Jack, Retrolambda, or DexGuard, the IDE uses Java 8 support provided by those tools instead.
如果Android Studio發現你的工程中使用Jack ,Retrolambda 或DexGuard,編輯器會使用Java8支援,替換這個工具。
因為我使用me.tatarka.retrolambda第三方框架,所以就出現了這個,我們只要刪除相關的配置就可以了。
變化2
提示有更高版本你的第三方框架:
上面的截圖顯示,gson有更高的版本2.8.3,提示我升級gson。這就省去了我們去github上檢視是否版本更新的時間,非常的方便。
問題X:
Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error:
原創 2017年10月06日 17:13:07Error:Execution failed for task ':app:mergeDebugResources'.
> Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
- 1
- 2
- 1
- 2
在專案的gradle.properties中:
android.enableAapt2=false
- 1
Error: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException:錯誤
把eclipse專案匯入AndroidStudio,結果報錯了。
錯誤:Error:Execution failed for task ':app:mergeDebugResources'. > Error: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException:
- android {
- ......
- aaptOptions.cruncherEnabled = false
- aaptOptions.useNewCruncher = false
- ......
- }
禁止Gradle檢查PNG的合法性
問題XXX:android studio 3.0版本升級問題修改:
===》 問題一
- Error:Cannot choose between the following configurations of project :pickerview:
- - debugApiElements
- - debugRuntimeElements
- - releaseApiElements
- - releaseRuntimeElements
- All 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.
- Error:Cannot choose between the following configurations of project :pickerview:
- - debugApiElements
- - debugRuntimeElements
- - releaseApiElements
- - releaseRuntimeElements
- All 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.
===》解決方法(from:http://blog.csdn.net/pjingying/article/details/71975805?utm_source=itdadao&utm_medium=referral):
Warning:android-apt plugin is incompatible with future version of Android Gradle plugin. Please use ‘annotationProcessor’ configuration instead.
原因:更新Android studio 原來專案出現問題。
分析: 尤其是採用butterknife工具的,採用新的Android Studio都會出現這樣的問題,本人根據提示最後猜測原因可能是Android studio更新,然後gradle更新了,這樣的話可能使原來的android-apt 工具跟不上節奏了,所以讓採用annotationProcessor工具。
解決: 把project下的build.gradle 當中的依賴
修改成如下:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath ‘com.android.tools.build:gradle:2.4.0-alpha7’ //classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’ //註釋掉
}
}
然後再把module下的build.gradle :
修改如下:
dependencies {
compile project(‘:roadvance-sdk’)
compile ‘com.google.dagger:dagger:2.10’
//apt ‘com.google.dagger:dagger-compiler:2.10’
annotationProcessor ‘com.google.dagger:dagger-compiler:2.10’
compile ‘com.android.support:appcompat-v7:25.3.1’
compile ‘com.jakewharton:butterknife:8.5.1’
//apt ‘com.jakewharton:butterknife-compiler:8.5.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.5.1’
}
再把 apply plugin: ‘com.neenbedankt.android-apt ’ 這個引用給刪除。
重新reBuild的一下
===》 問題二
- Error:Execution failed for task ':wigetlib: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 (com.jakewharton:butterknife:7.0.1)
- 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.
- Error:Execution failed for task ':wigetlib: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 (com.jakewharton:butterknife:7.0.1)
- 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
}
}
...
}
...
}
總結
這就是我今天遇到的問題及解決方案,如果之前有更多問題再補充。
歡迎小夥伴們加入 技術交流群: 168786059
加群請備註(技術交流)