1 library中 buildTypes和main app中不一致
项目拆分出几个Module后,编译release版本没有问题,编译其他版本会报如下错误:
主要是Required com.android.build.api.attributes.BuildTypeAttr ‘mock’ and found incompatible value 'debug’类似错误。
主要原因是:library中的 buildTypes和main app中不一致
> Could not resolve all task dependencies for configuration ':app:mockCompileClasspath'.
> Could not resolve project :Modules:basecomponent.
Required by:
project :app
> Unable to find a matching configuration of project :Modules:basecomponent:
- Configuration 'debugApiElements':
- Required com.android.build.api.attributes.BuildTypeAttr 'mock' and found incompatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration 'debugRuntimeElements':
- Required com.android.build.api.attributes.BuildTypeAttr 'mock' and found incompatible value 'debug'.
- Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found incompatible value 'java-runtime'.
- Configuration 'releaseApiElements':
- Required com.android.build.api.attributes.BuildTypeAttr 'mock' and found incompatible value 'release'.
- Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
- Configuration 'releaseRuntimeElements':
- Required com.android.build.api.attributes.BuildTypeAttr 'mock' and found incompatible value 'release'.
- Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
- Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
- Required org.gradle.usage 'java-api' and found incompatible value 'java-runtime'.
因为主项目app中build.gradle中BuildTypes配置为:
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
qa {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
mock {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
而模块中配置为:
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
此时编译release没有问题,编译其他类型app就会报上面的问题。
2 compile,api,implementation
参考:https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration#variant_dependencies
Gradle 3.4 引入了新的 Java 库插件配置,允许您控制到编译和运行时类路径的发布(适用于模块间依赖项)。 Android 插件 3.0.0 正在迁移到这些新依赖项配置。 要迁移您的项目,只需更新您的依赖项以使用新配置,而非已弃用配置,如下表中所列。
最近拆分项目,把有的功能提取到单独的Module中,其中一个Module引用了jar库文件,配置dependencies使用的都是implementation,但在主app中引用这个Module却报找不到jar包中的类。
一般情况下compile,api,implementation效果好像是一样的,但其实他们还是有区别的,而且这些区别有时可能导致错误出现。它们三个重要的作用就是将库引入到当前项目中,compile是比较老的引入方式,api和implementation则是AndroidStudio插件推荐的新的引入方式,具体点api和compile的功能相同,在引入库的同时还会将库(jar,aar)编译到自己的生成库中,所以在别的项目中使用时不需要重新引入这些库(jar,aar),implementation则不会把引用的库存储到自己的项目中。所以api或compile关键字引用的库对于其他module是可见的,可使用,而implementation关键字引用的库对于其他module来说是不可见的。
3 手动删除AndroidStudio引用的库
如果从libs中加载了一个jar或者aar文件:
如果手动在dependencies中删除了aar文件的引用也要去上面的图片所示位置删除一下,否则可能会出问题,但也可能不会出问题,所以当手动删除了dependencies中对于本地库的引用时,如果编译出错可以到上图位置看看是否还存在引用。