1. 版本不同错误
Error:Execution failed for task ':webviewdemo:preDebugAndroidTestBuild'.
Conflict with dependency 'com.android.support:support-annotations' in project ':webviewdemo'.
Resolved versions for app (26.1.0) and test app (27.1.1) differ.
See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.
解决方案:
//把此依赖引改用configurations.all{}包裹起来
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
//如下
configurations.all {
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
}
2. Android Stuido 多个DEX错误问题
Error:com.android.dex.DexException: Multiple dex files
解决方案(两种):
- clear project
- Project Structure--->app--->Properties中有一个选项叫:Incremental Dex 设置值为True
3. 指定配置错误
Error:com.android.builder.merge.DuplicateRelativeFileException:
More than one file was found with OS independent path 'META-INF/DEPENDENCIES'
解决方案:
在报错的module下的build.gradle文件中加入如下配置:
android{
//处理所有报META-INF/*'的错误
packagingOptions {
pickFirst 'META-INF/*'
}
}
4. 使用productFlavors多配置报错
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
解决方案:
出现这种异常一般情况下是在升级到Gradle4.x以上才会,只要在报错module下的build.gradle文件加入如下配置:
android{
flavorDimensions "default"
}
其中flavorDimensions的可以任意赋值
5. lib中使用productFlavors指定多任务引用出错,找不到方法
使用方法点击打开链接
Error:(38, 0) Could not find method commonCompile()
for arguments [DefaultProjectDependency{dependencyProject='project ':picmodule'',
configuration='commonRelease'}] on object of type org.gradle.api.internal.artifacts.dsl.
dependencies.DefaultDependencyHandler.
可能出错原因:
configurations{}配置放在了dependencies{}后面,可能会出现上面错误。
解决方案:
把configurations{}相关自定义内容放在dependencies{}前面,如:
configurations {
commonCompile
redCompile
blueCompile
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
commonCompile project(path: ':picmodule', configuration: 'commonRelease')
redCompile project(path: ':picmodule', configuration: 'redRelease')
blueCompile project(path: ':picmodule', configuration: 'blueRelease')
}
注意:不管是Application module,还是一个library module,只要需要依赖lib.module,都需要在自身的build.gradle中配置同样的productFlavors。也就是说app和library的productFlavors配置名必须相同
6. lib中使用productFlavors指定多任务引用出错2
在升级到Gradle4.x时,如果继续使用如下方式引用依赖lib
commonCompile project(path: ':picmodule', configuration: 'commonRelease')
redCompile project(path: ':picmodule', configuration: 'redRelease')
blueCompile project(path: ':picmodule', configuration: 'blueRelease')
可能会报找不到依赖包错误:
如果出现以上方式,可以尝试使用如下方式引用
implementation project(":picmodule")
7. 布局文件报空指针异常
在项目运行时,布局文件突然报个NullPointerException,后来检查原来View写成了view,如下:
<view
android:layout_width="match_parent"
android:layout_height="@dimen/dp_10" />
把view改成View即可。