文章

04.AGP避坑

04.AGP避坑

dependencyResolutionManagement 和 allprojects 的 repositories 同时配置了仓库

  • 错误
1
2
A problem occurred evaluating root project 'android-architecture'.
> Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'
  • 分析
    root build.gradlesettings.gradle 同时添加了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// root build.gradle
allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}
// settings.gradle
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        jcenter() // Warning: this repository is going to shut down soon
    }
}
  • 解决
    去除其中一个就行了,推荐去除 app/build.gradle 中的配置

buildType 和 productFlavor 问题

Library Module 多个 buildType 问题

问题描述

  • 问题 1:Library Module buildType 如果有多个,默认 app 只会用 Library 的 release buildType。

library module 的 gradle 配置里面有这样两个默认配置:

1
2
3
4
android {
    defaultPublishConfig "release"
    publishNonDefault false
}

这个配置的作用是: 无论主 module 以什么 build type 来构建, 我 (library) 都将以 release 模式来构建。也就是在 Library 中,用了 BuildConfig.DEBUG,那将都返回 true。

publishNonDefault:是否关闭默认发布配置 (true 关闭, false 开启)
defaultPublishConfig:默认发布配置项,这个需要 library 中必须存在,否则编译不过

  • 问题 2:AGP3.x+,如果 App 有的 buildType 而 Library 没有会报错

通过 matchingFallbacks 机制

解决

  1. 通过分支名决定构建的版本类型
    https://juejin.im/post/5a3a70d46fb9a044fe46855d
  2. defaultPublishConfig 配置默认的 buildType

Ref

设置 defaultPublishConfig 和 app 的保持 buildType 一致

Gradle buildType 和 productFlavors 不能相同

相同的话,报错

1
BuildType names cannot collide with ProductFlavor names

lib 和 app 中的 productFlavor 和 buildType 不匹配

  1. lib 和 app productFlavor 不匹配
  2. lib 和 app buildType 不匹配
1
2
3
Required by:
 project :modules:common
> The consumer was configured to find an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'beta', attribute 'app' with value 'huawei', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'. However we cannot choose between the following variants of project :baseui:
  • 解决 1:app 和 lib 都配置一样的 buildType 和 flavor
  • 解决 2:利用 matchingFallbacks 进行 buildType 的降级;lib 中没有对应的 flavor 时,好像不需要配置对应的 flavor

productFlavor 配置成大写, xxxImplementation 编译不过

解决:productFlavor 的名称改成小写

Gradle merger task

查看 Gradle merge 后的结果:
app/build/outputs/logs/manifest-merger-debug-report.txt

merge 后的 AndroidManifest.xml 文件路径:
app/build/intermediates/manifests/full/debug/AndroidManifest.xml

本文由作者按照 CC BY 4.0 进行授权