Gradle 的常见错误

1.在您的应用程序的 build.gradle 添加以下内容:(任意位置,一般放在外层)

 configurations.all {
 resolutionStrategy.force'com.google.code.findbugs:jsr305:1.3.9'
} 
 

 

强制Gradle只编译您为所有依赖关系声明的版本号,而不管依赖关系已声明的版本号。  

 2. 当编译版本出现不一致的时候,打包APK时候build会出现冲突,解决办法:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.0'
            }
        }
    }
}

3. 如果用使用其他Lib,要保证这些Lib没有被preDex,否则可能会抛出下面的异常

UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Library dex files are not supported in multi-dex mode
        at com.android.dx.command.dexer.Main.runMultiDex(Main.java:337)
        at com.android.dx.command.dexer.Main.run(Main.java:243)
        at com.android.dx.command.dexer.Main.main(Main.java:214)
        at com.android.dx.command.Main.main(Main.java:106)

   遇到这个异常,需要在Gradle中修改,让它不要对Lib做preDexing

android {

    dexOptions {
         javaMaxHeapSize "4g" //当调用dx时指定-Xmx值。   
         preDexLibraries = false //是否预先dex库,它可以改善增量的生成,但是在clear build可能会变慢
    }
}

4.packagingOptions 是打包时的一些配置,使用exclude 在打包时移除项目中的相关文件,不打入apk文件中。

针对引用的三方库会带有一些配置文件xxxx.properties,或者license信息,当我们打包的时候想去掉这些信息就可以使用上面的packagingOptions 配置方式。

packagingOptions {
    exclude 'META-INF/rxjava.properties'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'

}

5.是否在输出错误的时候,lint应该展示出全路径。默认是相对路径,也就是默认false。可以不配置

lintOptions{}的一般用法: lintOptions { abortOnError false }

6.当项目存在多个module时,如果想在某个library module中依赖aar文件时,那么其它所有直接或间接依赖该library module的module中都应声明该aar文件所在libs目录的相对路径或在project的build.gradle文件中进行统一配置声明。否则会同步依赖失败

1.将aar文件拷入想要依赖的library module下的libs文件夹中;
2.在module或project中声明aar文件路径:

  • 方式一、在module中声明:
    在依赖aar文件的library module的build.gradle文件中配置如下:
repositories {
    flatDir {
        dirs 'libs'
    }
}

在其它所有直接或间接依赖该library module的module中配置build.gradle文件如下:

repositories {
    flatDir {
        dirs '../xxx/libs','libs'  //将xxx替换为引入aar文件的module名
    }
}
  • 方式二、在project的build.gradle文件中统一配置如下:
allprojects {
    repositories 
        flatDir {
            dirs project(':xxx').file('libs')  //将xxx替换为引入aar文件的module名
        }
    }
}

3.在依赖aar文件的library module下的build.gradle文件中添加依赖如下:

implementation(name: 'xxx', ext: 'aar')  //只允许在依赖aar文件的module下调用该aar文件
或
api (name:'xxx',ext:'aar')  //在其他依赖该library module的module中,也可调用该library modu


找错误:gradlew compileDebugJavaWithJavac                                     gradlew compileDebugSource --stacktrace -info

猜你喜欢

转载自blog.csdn.net/lk2021991/article/details/89852718