Android Studio 3.0 的一些小变化

作者: 珠穆朗玛小王子 | 来源:发表于2017-10-27 18:34 被阅读2113次

    前言

    一大早还在北京拥挤的地铁里,我的CTO闫哥在微信里给我发了一条信息:Android Studio 3.0发布了。

    为什么会这么关注Android Studio 3.0 的版本发布呢?主要是因为公司即将开发的新app准备使用Kotlin语言,而Android Studio 3.0 已经把Kotlin的语言支持内置进去了,这样就省去了很多的麻烦,如果你还没接触过Kotlin语言,可以去百度一下 他们的官网,如果你现在使用的Java语言,那么你真是太幸运了,因为Kotlin对于你来说,将会非常简单,例如像我这样的,两三天就可以几乎应付大部分的开发了。

    这里就不对Kotlin语言做过多的描述了,今天的重点,是我升级到Android Studio 3.0 以后的故事。

    正文

    来到公司打开电脑,升级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是只读属性,不可以对他进行修改

    修改后的代码:

    // 修改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属性应当属于同一个命名空间

    看着问题似乎有点深奥,其实就是需要我们为flavors设置一个版本,统一使用相同版本的flavors。

    defaultConfig {
       targetSdkVersion:***
       minSdkVersion :***
       versionCode:***
       versionName :***
       //为flavor设置一个版本,命名是随意的
       flavorDimensions "versionCode"
    }
    

    问题3

    这里写图片描述

    有些库不能被正常引用,例如我使用的multidex,在上面的截图中已经提示我们如何解决这个问题

    buildscript {
        repositories {
            ...
            // 添加google库的依赖
            google()
        }
        dependencies {
            ...
        }
    }
    

    问题4

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

    提示我们找不到@android:attr/windowEnterAnimation,因为已经不支持@开头使用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.

    好多的错误日志啊,其实最关键的只有前两行:

    使用注解编译库,需要显示的声明,而我正在使用的butterknife是含有注解编译功能的,但是并没有声明。

    解决办法:

    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上查看是否版本更新的时间,非常的方便。

    总结

    这就是我今天遇到的问题及解决方案,如果之前有更多问题再补充。

    祝大家周末愉快~

    相关文章

      网友评论

      • 小于小于:楼主,设置apkName,这样写不会每次取的都是最后的apkName吗?比如你release写前面就是release的名字,debug写前面就用的debug设置的名字
        珠穆朗玛小王子:@小于小于 我看了你的代码
        applicationVariants.all { variant ->
        variant.outputs.all { output ->
        if (outputFileName.endsWith('.apk')) {
        if ("app".equalsIgnoreCase(variant.buildType.name)) {
        outputFileName = "app.apk"
        } else {
        outputFileName = "bbb-" + vName + "-debug.apk"
        }
        }
        }
        }
        你这里还做了一个判断"app".equalsIgnoreCase(variant.buildType.name),看上去应该是判断应用的名称,如果名称不相等则会打出bbb开头的apk,你检查一下的appName看看呢,或者看看variant.buildType.name究竟是什么值
        小于小于:@珠穆朗玛小王子 我就是这样试的,他会执行后面设置的那个,我也很好奇,最后没办法这样设置的,是不是我什么地方少设置了呢?
        applicationVariants.all { variant ->
        variant.outputs.all { output ->
        if (outputFileName.endsWith('.apk')) {
        if ("app".equalsIgnoreCase(variant.buildType.name)) {
        outputFileName = "app.apk"
        } else {
        outputFileName = "bbb-" + vName + "-debug.apk"
        }
        }
        }
        }
        珠穆朗玛小王子:不会 你可以试一下,这个是表示你要打的包是release版还是debug版,这个是android studio自带的功能,在你打包的时候是需要选择的,这样直接把apk的正式包和测试包区别开。
      • 981139cb1310:大神,对于第一个问题,我的gradle里 是这样的,报同样的错,没有output属性,我尝试加入该函数仍然不奏效。
        android.applicationVariants.all { variant ->

        if (variant.getBuildType().name == "debug") {
        task "configDevice2${variant.name.capitalize()}" (type: Exec){
        dependsOn variant.install

        group = 'nameofyourtaskgroup'
        description = 'Describe your task here.'

        def adb = android.getAdbExe().toString()
        def mypermission = 'android.permission.WRITE_EXTERNAL_STORAGE '
        commandLine "$adb shell pm grant ${variant.applicationId} $mypermission".split(' ')
        }
        variant.testVariant.connectedInstrumentTest.dependsOn "configDevice2${variant.name.capitalize()}"

        }
        }
        求助。。。网上都说把 outputs.each 改成outputs.all就好了,可是我没有这个代码啊,粘贴添加进去也还是同样的错:sob: :sob: ,谢谢大神
        珠穆朗玛小王子:@纸盒人阿楞 不好意思 我最近一直在加班 我当时也遇到类似这样的问题 我也是很蛋疼 后来我先把其他错误一起改掉 这个地方才生效 ……我也没弄明白是为什么
      • 跑步的小男孩:太感谢了 问题解决了
      • 翻译不了的声响:applicationVariants.all { variant ->
        variant.outputs.each { output ->
        def fileName = "${variant.versionName}_release.apk"
        if (output.zipAlign != null) {
        output.zipAlign.doLast {
        output.zipAlign.inputFile.delete()(报错)
        }
        }
        Could not get unknown property 'inputFile' for task
      • 酒仙娘子:不错 不错 解决了我的问题
      • 0445981d6022:可以的

      本文标题:Android Studio 3.0 的一些小变化

      本文链接:https://www.haomeiwen.com/subject/deiauxtx.html