美文网首页Android知识程序员Android开发
Android Studio 2.3 到 3.0 (正式版)的项

Android Studio 2.3 到 3.0 (正式版)的项

作者: Bleoo | 来源:发表于2017-10-30 11:32 被阅读0次

    前言

    要迁移 看官网迁移策略
    https://developer.android.google.cn/studio/build/gradle-plugin-3-0-0-migration.html

    以下只是我个人的迁移思路和碰到的一些问题及解决方法,仅供参考。

    升级Gradle版本

    这个在Android Studio 3.0 打开时会自动提醒下载,点击下载升级就好,Gradle android tools 也会自动升级到3.0.0,当然还要新增 google 库。

    buildscript {
        repositories {
            ...
            // You need to add the following repository to download the
            // new plugin.
            google()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:3.0.0'
        }
    }
    

    apt、compile更换

    所有的 apt 要改写成 annotationProcessor,例如:

    annotationProcessor ‘com.jakewharton:butterknife-compiler:8.4.0’
    

    compile 官方已废弃,推荐改为 api 及 implementation。这个 implementation 我还未理解。我个人只是将库的依赖改为 api,例如:

    api 'com.facebook.fresco:fresco:1.5.0'
    

    定义 flavor dimensions

    这是新增的一个所谓 “维度” 的东西。
    当你定义了 productFlavors 时,你必须定义 flavor dimensions。flavorDimensions 填的是若干个 String 型。类似以下:

    flavorDimensions "apple", "banana"
    
    productFlavors {
         qq {
          dimension "apple"
          ...
        }
    
        oppo {
          dimension "banana"
          ...
        }
    }
    
    

    “apple” 和 “banana” 就分别是两个 “维度” 。此时你的gradle命令中会出现assembleQqOppo类似的命令。

    感觉不对吧?我要么打qq这个包要么打oppo这个包,怎么会出现QqOppo杂交的包呢?

    这就是 “维度” 的特性。就是交叉组合。例如:A B C,会组合成:A、 B、C、 AB、 AC、 BC、 ABC 这些。

    这显然不是我想要的,所以我只定义了一个维度。类似以下:

    flavorDimensions "1.0"
    
    productFlavors {
         qq {
          dimension "1.0"
          ...
        }
    
        oppo {
          dimension "1.0"
          ...
        }
        
        ...
    }
    
    

    解决一些错误

    这时候同步gradle,可能会出现类似以下的错误:

    Error:Unable to resolve dependency for ':app@debug/compileClasspath':
      Could not resolve project :library.
    Error:Unable to resolve dependency for ':app@release/compileClasspath':
      Could not resolve project :library.
    

    这是因为你在 app 主模块的 gradle 中定义了 buildTypes。类似以下:

    buildTypes {
        debug {
            ...
        }
        alpha {
            ...
        }
        release {
            ...
        }
    }
    

    这时候你必需保证编译时所有的子 library 模块能够指定 buildType。也就是说如果你要编译 alpha 版本时,所有的子模块也必须有 alpha 版本。

    这就很尴尬了。我所有的 library 都没有定义 buildTypes。

    我的解决方法很 ~ (如果有更好的解决方法麻烦告知,谢谢~)。在所有的 library 子模块都加上 buildTypes, 如下:

    buildTypes {
        debug {}
        alpha {}
        release {}
    }
    

    再次同步gradle。可能出现以下错误:

    Error:Execution failed for task ':TerminalLibrary:transformResourcesWithMergeJavaResForDebug'.
    > More than one file was found with OS independent path 'META-INF/LICENSE'
    Information:BUILD FAILED
    

    在 app 主模块下的 build.gradle 中加入如下配置项,排除掉中间生成的META-INF/xxx文件

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    

    你可能会出现类似以下的错误信息:

    Error: style attribute '@android:attr/windowEnterAnimation' not found
    

    解决方法也很简单,找到定义该style的地方,把 @ 删掉。


    你可能会碰到如下 Warning :

    Warning:The android.dexOptions.incremental property is deprecated and it has no effect on the build process. 
    

    解决:

    dexOptions { 
    javaMaxHeapSize “4g” 
    incremental true // 将这句删除就好
    } 
    

    我只写出我碰到的问题及我个人解决方法。更多的问题解决请参考本文顶部的官网,还望已官网为准~


    Java8 的支持

    参考官方
    https://developer.android.google.cn/studio/write/java8-support.html

    在你的 build.gradle 中配置如下:

    android {
      ...
      // Configure only for each module that uses Java 8
      // language features (either in its source code or
      // through dependencies).
      compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
      }
    }
    

    如果你是从 Jack 迁移过来,请删除以下代码块:

    // Remove this block.
    jackOptions {
        enabled true
        ...
    }
    

    如果你的使用了 retrolambda 插件。请删除:

    // Remove the following plugin.
    apply plugin: 'me.tatarka.retrolambda'
    ...
    // Remove this block after migrating useful configurations.
    retrolambda {
        ...
        // If you have arguments for the Java VM you want to keep,
        // move them to your project's gradle.properties file.
        jvmArgs '-Xmx2048m'
    }
    
    

    如果你不想使用Java8 的特性,可以在 gradle.properties 中将其关闭:

    android.enableDesugar=false
    

    以上是我花半天时间的所做的项目迁移,希望能帮到一些人。

    相关文章

      网友评论

        本文标题:Android Studio 2.3 到 3.0 (正式版)的项

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