Android Studio 3.0 的一些小變化

前言

一大早還在北京擁擠的地鐵里,我的CTO閆哥在微信里給我發(fā)了一條信息:Android Studio 3.0發(fā)布了。

為什么會這么關(guān)注Android Studio 3.0 的版本發(fā)布呢?主要是因為公司即將開發(fā)的新app準(zhǔn)備使用Kotlin語言,而Android Studio 3.0 已經(jīng)把Kotlin的語言支持內(nèi)置進(jìn)去了,這樣就省去了很多的麻煩,如果你還沒接觸過Kotlin語言,可以去百度一下 他們的官網(wǎng),如果你現(xiàn)在使用的Java語言,那么你真是太幸運了,因為Kotlin對于你來說,將會非常簡單,例如像我這樣的,兩三天就可以幾乎應(yīng)付大部分的開發(fā)了。

這里就不對Kotlin語言做過多的描述了,今天的重點,是我升級到Android Studio 3.0 以后的故事。

正文

來到公司打開電腦,升級Android Studio到3.0版本,編譯目前的工程。哎呀呀我擦擦,為什么報了好多的錯?別著急,我們慢慢解決這些問題。

這里寫圖片描述

Android Studio的自帶Gradle版本是4.1,插件版本是3.0.0,所以如果你使用的是老版本,就會出現(xià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是只讀屬性,不可以對他進(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,然后通過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屬性應(yīng)當(dāng)屬于同一個命名空間

看著問題似乎有點深奧,其實就是需要我們?yōu)閒lavors設(shè)置一個版本,統(tǒng)一使用相同版本的flavors。

defaultConfig {
   targetSdkVersion:***
   minSdkVersion :***
   versionCode:***
   versionName :***
   //為flavor設(shè)置一個版本,命名是隨意的
   flavorDimensions "versionCode"
}

問題3

這里寫圖片描述

有些庫不能被正常引用,例如我使用的multidex,在上面的截圖中已經(jīng)提示我們?nèi)绾谓鉀Q這個問題

buildscript {
    repositories {
        ...
        // 添加google庫的依賴
        google()
    }
    dependencies {
        ...
    }
}

問題4

Error:(2638) error: style attribute '@android:attr/windowEnterAnimation' not found.

提示我們找不到@android:attr/windowEnterAnimation,因為已經(jīng)不支持@開頭使用android自帶的屬性,我們只要把@符號刪掉就可以了。

修改前:

<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>

問題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.

好多的錯誤日志啊,其實最關(guān)鍵的只有前兩行:

使用注解編譯庫,需要顯示的聲明,而我正在使用的butterknife是含有注解編譯功能的,但是并沒有聲明。

解決辦法:

android {

    defaultConfig {
    // 聲明需要使用注解功能
        javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
        ...
    }
}

其他的變化

通過剛才的修改,我的工程已經(jīng)運行起來了,但是發(fā)現(xiàn)了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'

從警告上看,希望我移除這個插件,于是我到官網(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,編輯器會使用Java8支持,替換這個工具。

因為我使用me.tatarka.retrolambda第三方框架,所以就出現(xiàn)了這個,我們只要刪除相關(guān)的配置就可以了。

變化2

提示有更高版本你的第三方框架:


這里寫圖片描述

上面的截圖顯示,gson有更高的版本2.8.3,提示我升級gson。這就省去了我們?nèi)ithub上查看是否版本更新的時間,非常的方便。

總結(jié)

這就是我今天遇到的問題及解決方案,如果之前有更多問題再補(bǔ)充。

祝大家周末愉快~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容