前言
一大早還在北京擁擠的地鐵里,我的CTO閆哥在微信里給我發(fā)了一條信息:Android Studio 3.0發(fā)布了。
為什么會(huì)這么關(guān)注Android Studio 3.0 的版本發(fā)布呢?主要是因?yàn)楣炯磳㈤_(kāi)發(fā)的新app準(zhǔn)備使用Kotlin語(yǔ)言韵吨,而Android Studio 3.0 已經(jīng)把Kotlin的語(yǔ)言支持內(nèi)置進(jìn)去了吮廉,這樣就省去了很多的麻煩,如果你還沒(méi)接觸過(guò)Kotlin語(yǔ)言霹购,可以去百度一下 他們的官網(wǎng),如果你現(xiàn)在使用的Java語(yǔ)言朋腋,那么你真是太幸運(yùn)了厕鹃,因?yàn)镵otlin對(duì)于你來(lái)說(shuō),將會(huì)非常簡(jiǎn)單乍丈,例如像我這樣的剂碴,兩三天就可以幾乎應(yīng)付大部分的開(kāi)發(fā)了。
這里就不對(duì)Kotlin語(yǔ)言做過(guò)多的描述了轻专,今天的重點(diǎn)忆矛,是我升級(jí)到Android Studio 3.0 以后的故事。
正文
來(lái)到公司打開(kāi)電腦请垛,升級(jí)Android Studio到3.0版本催训,編譯目前的工程。哎呀呀我擦擦宗收,為什么報(bào)了好多的錯(cuò)漫拭?別著急,我們慢慢解決這些問(wèn)題混稽。
Android Studio的自帶Gradle版本是4.1采驻,插件版本是3.0.0,所以如果你使用的是老版本匈勋,就會(huì)出現(xiàn)一些小的兼容問(wèn)題礼旅,我們看看報(bào)了哪些錯(cuò)誤呢:
問(wèn)題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是只讀屬性,不可以對(duì)他進(jìn)行修改
修改后的代碼:
// 修改apk build的名字
android.applicationVariants.all { variant ->
variant.outputs.all {
if (outputFileName.endsWith('.apk')) {
if ("debug".equalsIgnoreCase(variant.buildType.name)) {
outputFileName = "app_debug.apk"
} else if("release".equalsIgnoreCase(variant.buildType.name)){
outputFileName = "app_release.apk"
}
else if("lzp".equalsIgnoreCase(variant.buildType.name)){
outputFileName = "app_test.apk"
}
}
}
}
把each修改為all洽洁,然后通過(guò)outputFileName修改生成的apk的名稱痘系。
如果你提示沒(méi)有找到all方法或者是未找到outputFileName,你可以先把這個(gè)功能注釋掉饿自,等其他問(wèn)題都解決了汰翠,再打開(kāi)就可以解決這個(gè)問(wèn)題了。
問(wèn)題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屬性應(yīng)當(dāng)屬于同一個(gè)命名空間
看著問(wèn)題似乎有點(diǎn)深?yuàn)W昭雌,其實(shí)就是需要我們?yōu)閒lavors設(shè)置一個(gè)版本复唤,統(tǒng)一使用相同版本的flavors。
defaultConfig {
targetSdkVersion:***
minSdkVersion :***
versionCode:***
versionName :***
//為flavor設(shè)置一個(gè)版本城豁,命名是隨意的
flavorDimensions "versionCode"
}
問(wèn)題3
有些庫(kù)不能被正常引用苟穆,例如我使用的multidex抄课,在上面的截圖中已經(jīng)提示我們?nèi)绾谓鉀Q這個(gè)問(wèn)題
buildscript {
repositories {
...
// 添加google庫(kù)的依賴
google()
}
dependencies {
...
}
}
問(wèn)題4
Error:(2638) error: style attribute '@android:attr/windowEnterAnimation' not found.
提示我們找不到@android:attr/windowEnterAnimation唱星,因?yàn)橐呀?jīng)不支持@開(kāi)頭使用android自帶的屬性雳旅,我們只要把@符號(hào)刪掉就可以了。
修改前:
<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>
修改后:
<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>
問(wèn)題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.
好多的錯(cuò)誤日志啊间聊,其實(shí)最關(guān)鍵的只有前兩行:
使用注解編譯庫(kù)攒盈,需要顯示的聲明,而我正在使用的butterknife是含有注解編譯功能的哎榴,但是并沒(méi)有聲明型豁。
解決辦法:
android {
defaultConfig {
// 聲明需要使用注解功能
javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
...
}
}
其他的變化
通過(guò)剛才的修改,我的工程已經(jīng)運(yùn)行起來(lái)了尚蝌,但是發(fā)現(xiàn)了Android Studio 3.0 的幾個(gè)小變化迎变。
變化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'
從警告上看,希望我移除這個(gè)插件飘言,于是我到官網(wǎng)上查看了一下信息:
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發(fā)現(xiàn)你的工程中使用Jack 衣形,Retrolambda 或DexGuard,編輯器會(huì)使用Java8支持姿鸿,替換這個(gè)工具谆吴。
因?yàn)槲沂褂胢e.tatarka.retrolambda第三方框架,所以就出現(xiàn)了這個(gè)苛预,我們只要?jiǎng)h除相關(guān)的配置就可以了句狼。
變化2
提示有更高版本你的第三方框架:
上面的截圖顯示,gson有更高的版本2.8.3热某,提示我升級(jí)gson腻菇。這就省去了我們?nèi)ithub上查看是否版本更新的時(shí)間,非常的方便昔馋。
總結(jié)
這就是我今天遇到的問(wèn)題及解決方案芜繁,如果之前有更多問(wèn)題再補(bǔ)充。
祝大家周末愉快~