升級3.0之后打開項目報錯,這里記錄一下解決的過程
首先更新了gradle版本
之后報錯
Gradle sync failed: 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.
Google了一下,是因為項目Gradle下使用的部分代碼語法變了
這是項目中原本自定義構(gòu)建APK的代碼
applicationVariants.all { variant ->
variant.outputs.each { output ->
def newName
def timeNow
def outputFile = output.outputFile
if (Boolean.valueOf(IS_JENKINS)) {
timeNow = JENKINS_TIME
newName = APP_PACKAGE_NAME + "-android-V${defaultConfig.versionName}-" + variant.productFlavors[0].name + "-" + timeNow + "-" + variant.buildType.name + '.apk'
} else {
timeNow = getDate()
if ('debug' == variant.buildType.name) {
newName = APP_PACKAGE_NAME + "-android-V${defaultConfig.versionName}-debug.apk"
} else {
newName = APP_PACKAGE_NAME + "-android-V${defaultConfig.versionName}-" + variant.productFlavors[0].name + "-" + timeNow + "-" + variant.buildType.name + '.apk'
}
}
output.outputFile = new File(outDirectory, newName)
}
}
其中的這句 variant.outputs.each { output ->
需要把each
替換成all
獲取輸出文件的代碼output.outputFile
已經(jīng)變?yōu)橹蛔x,現(xiàn)在直接使用outputFileName
賦值即可修改輸出的名稱
修改后的代碼
applicationVariants.all { variant ->
variant.outputs.all { output ->
def newName
def timeNow
if (Boolean.valueOf(IS_JENKINS)) {
timeNow = JENKINS_TIME
newName = APP_PACKAGE_NAME + "-android-V${defaultConfig.versionName}-" + variant.productFlavors[0].name + "-" + timeNow + "-" + variant.buildType.name + '.apk'
} else {
timeNow = getDate()
if ('debug' == variant.buildType.name) {
newName = APP_PACKAGE_NAME + "-android-V${defaultConfig.versionName}-debug.apk"
} else {
newName = APP_PACKAGE_NAME + "-android-V${defaultConfig.versionName}-" + variant.productFlavors[0].name + "-" + timeNow + "-" + variant.buildType.name + '.apk'
}
}
outputFileName = newName
}
}
修改后 Try Again 繼續(xù)報錯
Gradle sync failed: Cannot choose between the following configurations of project :androidSDK:
- 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... (show balloon)
看報錯說明是因為依賴的一個model:androidSDK
,Google說明新版中依賴項目的語法改變了,依賴包使用舊的語法沒有報錯,
舊版compile project(':androidSDK')
新版implementation project(':androidSDK')
貌似現(xiàn)在依賴包也可以使用implementation
這里還提示一點,gradle文件中:
compile
替換為 implementation
apt
替換為 annotationProcessor
testCompile
替換為 androidTestImplementation
releaseCompile
替換為 releaseImplementation
這里注意一點:compile fileTree(dir: 'libs', include: ['*.jar'])
不需要替換compile
,看樣子只有依賴包的時候才需要替換
還有一點,有的依賴包替換成implementation
提示找不到,換回compile
又可以了
繼續(xù)Tran Again提示需要更新Android SDK Build Tools version 至 26.0.2
之后報錯
Error:android-apt plugin is incompatible with the Android Gradle plugin. Please use 'annotationProcessor' configuration instead.
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
繼續(xù)Google,新版去掉了android-apt,所以刪除項目Gradle中apply plugin: 'android-apt'
,只保留apply plugin: 'com.android.application'
把apt
替換為annotationProcessor
Try Again:
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
根據(jù)官網(wǎng)說法:
you need to first declare one or more dimensions using the flavorDimensions property. After that, assign each flavor to one of the dimensions you declared
必須指定一個或多個 flavor dimensions
flavorDimensions 后面可以添加多個不同類型的參數(shù)例如:flavorDimensions "tier", "minApi"
官方示例:
// Specifies two flavor dimensions.
flavorDimensions "tier", "minApi"
productFlavors {
free {
// Assigns this product flavor to the "tier" flavor dimension. Specifying
// this property is optional if you are using only one dimension.
dimension "tier"
...
}
paid {
dimension "tier"
...
}
minApi23 {
dimension "minApi"
...
}
minApi18 {
dimension "minApi"
...
}
}
項目中原來的代碼:
productFlavors {
_360 {}
xiaomi {}
}
修改后的代碼:
flavorDimensions "default"
productFlavors {
_360 {
dimension "default"
}
xiaomi {
dimension "default"
}
}
繼續(xù)Tran Again:
Error:found unexpected optical bounds (red pixel) on top border at x=102.
Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:Execution failed for task ':ygj_new:merge_360DebugResources'.
> Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
是因為項目.9圖片中含有紅色邊線,刪除即可
繼續(xù)Tran Again:
Error:Execution failed for task ':ygj_new:transformClassesWithAndroidGradleClassShrinkerFor_360Debug'.
> java.lang.ArrayIndexOutOfBoundsException (no error message)
對于這個錯誤真是百思不得其解,研究了兩天在一篇博文的啟示下把Instant Run關(guān)掉,之后就成功了!
這里附上這篇博文:
記錄使用Instant Run的一個坑
本文已經(jīng)同步到我的個人博客:傳送門