将旧项目迁移到AndroidStudio3.0

简介: 1.aapt2编译不过去Error:Execution failed for task ':app:mergeDebugResources'.> Error: java.

1.aapt2编译不过去

Error:Execution failed for task ':app:mergeDebugResources'.
> Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details

原来的项目用到了kotlin以及kapt,升级到了androidstudio 3.0报了上面的错误,需要在项目的gradle.properties中添加:

android.enableAapt2=false

关闭aapt2。

2.All flavors must now belong to a named flavor dimension

All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

官方文档:

Plugin 3.0.0 includes a new dependency mechanism that automatically matches variants when consuming a library. This means an app's debug variant automatically consumes a library's debug variant, and so on. It also works when using flavors—an app's redDebug variant will consume a library's redDebug variant. To make this work, the plugin now requires that all flavors belong to a named flavor dimension —even if you intend to use only a single dimension. Otherwise, you will get the following build error:

Android Plugin3.0的依赖机制:在使用library时会自动匹配variant(debug, release),就是说app的debug会自动匹配library的debug,相信大多数人也像我一样,当library多了,不会手动选择每个Library的variant。现在好了,它会自动匹配了。同样如果使用flavor的时候,比如app的redDebug同样会自动匹配library的readDebug。虽然有这样的优势,但是在使用flavor时,必须定义flavor dimension,否则会提示错误:

Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.

现在使用flavor,必须像下面一样配置:

// Specifies a flavor dimension.
flavorDimensions "color"

productFlavors {
     red {
      // Assigns this product flavor to the 'color' flavor dimension.
      // This step is optional if you are using only one dimension.
      dimension "color"
      ...
    }

    blue {
      dimension "color"
      ...
    }
}
  • 注意:如果library有两个dimensions:color,shape,但是app只有color,那么会如下的编译错误:
Error:Could not resolve all dependencies for configuration ':bar:redDebugCompileClasspath'.
Cannot choose between the following configurations on project :foo:
  - redCircleDebugApiElements
  - redSquareDebugApiElements
  ...
  • 在APP使用flavorSelection选定使用某个flavor dimension,注意如下配置:
android {
  ...
  // The flavorSelection property uses the following format:
  // flavorSelection 'dimension_name', 'flavor_name'

  // Chooses the 'square' flavor from libraries that specify a 'shape'
  // dimension.
  flavorSelection 'shape', 'square'
}

参考文章

3.Cannot set the value of read-only property 'outputFile’….

Android plugin 3.0 migration guide 建议:

  • 使用 all()代替 each()
  • 使用 outputFileName代替 output.outputFile,如果你只想更改文件名称
// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.
android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

参考如下:

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        def newApkName = applicationId + "-" + variant.versionName + "(" + variant.versionCode + ")" + ".apk";
        outputFileName = new File("${project.projectDir}/../outputs/apks/" + variant.name, newApkName);
    }
}

参考文章

3.You should manually set the same version via DependencyResolution

参考文章

另一种姿势:

configurations.all {  
    resolutionStrategy.force 'rubygems:rb-inotify:0.9.5'  
} 
目录
相关文章
|
11月前
|
JavaScript IDE 前端开发
如何在大型代码仓库中删掉废弃的文件和 exports?
本文是我最近在公司内部写的废弃代码删除工具的一篇思考总结。
|
存储 iOS开发 Windows
利用Dism修复系统步骤,以及dism找不到源文件解决方案
利用Dism修复系统步骤,以及dism找不到源文件解决方案
4214 0
利用Dism修复系统步骤,以及dism找不到源文件解决方案
|
4月前
|
定位技术 C++
Visual Studio复制、拷贝C++项目与第三方库配置信息到新的项目中
Visual Studio复制、拷贝C++项目与第三方库配置信息到新的项目中
|
7月前
|
Java Maven
IDEA Maven 源修改为国内阿里云镜像的正确方式, 2023 年更新,亲测可用
IDEA Maven 源修改为国内阿里云镜像的正确方式, 2023 年更新,亲测可用
23004 1
|
Java 应用服务中间件 Maven
离线下IDEA打开拷贝的完整工程,解决工程代码大量报错的问题
离线下IDEA打开拷贝的完整工程,解决工程代码大量报错的问题
216 2
离线下IDEA打开拷贝的完整工程,解决工程代码大量报错的问题
|
C# 容器
旧项目集成
旧项目集成
107 0
旧项目集成
CLion中创建多个.c文件不能运行问题及报错问题
CLion中创建多个.c文件不能运行问题及报错问题
CLion中创建多个.c文件不能运行问题及报错问题
ohmyzsh 新功能解决文件全路径拷贝痛点
Alfred 插件 在终端打开 Finder ,或在 Finder 跳转到终端 首先要有 Alfred, 这个一般 mac 用户都装过,然后安装插件 :https://github.com/LeEnno/alfred-terminalfinder 你可以在终端和文件夹自由切换了
ohmyzsh 新功能解决文件全路径拷贝痛点
|
XML Java Android开发
AndroidStudio不自动添加新创建的文件到VCS的解决办法
AndroidStudio不自动添加新创建的文件到VCS的解决办法
AndroidStudio不自动添加新创建的文件到VCS的解决办法

热门文章

最新文章