關(guān)于Android Studio3.0升級

升級3.0之后打開項目報錯,這里記錄一下解決的過程

首先更新了gradle版本

2017-10-27-屏幕快照 2017-10-27 15.48.27.png

之后報錯

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)同步到我的個人博客:傳送門

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市乔煞,隨后出現(xiàn)的幾起案子吁朦,更是在濱河造成了極大的恐慌,老刑警劉巖渡贾,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件逗宜,死亡現(xiàn)場離奇詭異,居然都是意外死亡空骚,警方通過查閱死者的電腦和手機纺讲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來囤屹,“玉大人熬甚,你說我怎么就攤上這事±呒幔” “怎么了乡括?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長智厌。 經(jīng)常有香客問我粟判,道長,這世上最難降的妖魔是什么峦剔? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮角钩,結(jié)果婚禮上吝沫,老公的妹妹穿的比我還像新娘呻澜。我一直安慰自己,他們只是感情好惨险,可當我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布羹幸。 她就那樣靜靜地躺著,像睡著了一般辫愉。 火紅的嫁衣襯著肌膚如雪栅受。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天恭朗,我揣著相機與錄音屏镊,去河邊找鬼。 笑死痰腮,一個胖子當著我的面吹牛而芥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播膀值,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼棍丐,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了沧踏?” 一聲冷哼從身側(cè)響起歌逢,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎翘狱,沒想到半個月后秘案,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡盒蟆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年踏烙,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片历等。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡讨惩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出寒屯,到底是詐尸還是另有隱情荐捻,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布寡夹,位于F島的核電站处面,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏菩掏。R本人自食惡果不足惜魂角,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望智绸。 院中可真熱鬧野揪,春花似錦访忿、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至挣惰,卻和暖如春卧斟,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背憎茂。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留唇辨,地道東北人廊酣。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓赏枚,卻偏偏與公主長得像,于是被迫代替她去往敵國和親饿幅。 傳聞我的和親對象是個殘疾皇子凡辱,可洞房花燭夜當晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容