gradlew常见命令

作者: 高斯巴 | 来源:发表于2019-11-07 12:05 被阅读0次

    gradlew命令都可以使用简写,例如:

    ./gradlew assembleRelease 简写为:./gradlew aR

    快速构建命令

    # 查看所有任务

    ./gradlew tasks --all

    # 对某个module [moduleName] 的某个任务[TaskName] 运行.

    /gradlew:moduleName:taskName

    # 查看项目的依赖都依赖了哪些库。

    gradlew :app:dependencies 

    #只编译清单文件,并查看具体日志,快速定位清单文件报错

    gradlew :app:processDebugManifest --stacktrace:

    # 查看构建版本

    ./gradlew -v

    # 清除build文件夹

    ./gradlew clean

    # 检查依赖并编译打包

    ./gradlew build

    # 编译并安装debug包

    ./gradlew installDebug

    # 编译并打印日志

    ./gradlew build --info

    # 调试模式构建并打印日志

    ./gradlew build --info --debug --stacktrace

    # 强制更新最新依赖,清除构建并构建

    ./gradlew clean --refresh-dependencies build

    注意build命令把 debug、release环境的包都打出来的

    如果需要指定构建使用如下命令

    指定构建目标命令

    # 编译并打Debug包

    ./gradlew assembleDebug./gradlew aD

    # 编译并打Release的包

    ./gradlew assembleRelease./gradlew aR

    构建并安装调试命令

    # 编译并打Debug包

    ./gradlew assembleDebug

    # 编译app module 并打Debug包

    ./gradlew install app:assembleDebug

    # 编译并打Release的包

    ./gradlew assembleRelease

    #  Release模式打包并安装

    ./gradlew installRelease

    # 卸载Release模式包

    ./gradlew uninstallRelease

    多渠道打包

    assemble还可以和productFlavors结合使用,如果出现类似 Task'install'isambiguousinroot project 这种错误,请查看配置的多个渠道然后修改命令为./gradlew install[productFlavorsName] app:assembleDebug来用命令构建调试

    # Release模式打包并安装

    ./gradlew installRelease

    # 卸载Release模式包

    ./gradlew uninstallRelease

    # Release模式全部渠道打包

    ./gradlew assembleRelease

    # Release模式 test 渠道打包

    ./gradlew assembleTestRelease

    # debug release模式全部渠道打包

    ./gradlew assemble

    #查看包依赖

    ./gradlew dependencies

    # 编译时的依赖库

    ./gradlew app:dependencies --configuration compile

    # 运行时的依赖库

    ./gradlew app:dependencies --configuration runtime

    依赖管理

    传递依赖特性

    dependencies {    transitivetrue}

    手动配置transitive属性为false,阻止依赖的下载

    强制

    configurations.all{// transitive false// 强制指定版本resolutionStrategy{    force'org.hamcrest:hamcrest-core:1.3'// 强制不编译all*.excludegroup:'org.hamcrest',module:'hamcrest-core'}}

    动态依赖特性

    dependencies {// 任意一个版本compile group:'b',name:'b',version:'1.+'// 最新的版本compile group:'a',name:'a',version:'latest.integration'}

    查看详细依赖信息

    #使用离线模式

    ./gradlew aDR --offline

    #守护进程

    ./gradle build --daemon

    #并行编译模式

    ./gradle build --parallel --parallel-threads=N

    #按需编译模式

    ./gradle build --configure-on-demand

    #不使用snapshot依赖仓库

    前提是离线可以使用时

    ./gradlew clean aDR

    设定编码

    allprojects {...    tasks.withType(JavaCompile){        options.encoding ="UTF-8"}...}

    仓库设置

    设置中心仓库

    默认是jcenter、可以是mavenCentral

    repositories {    maven { url"http://maven.oschina.net/content/groups/public"}}

    Android Studio 提速

    禁用插件

    去掉一些没有用的插件

    Google Cloud Testing、Google Cloud Tools For Android Studio、Goole Login、Google Services、JavaFX、SDK Updater、TestNG-J

    android studio 2.2.2新特性 编译缓存

    工程根目录 gradle.properties 文件里加上

    android.enableBuildCache=true

    这个设置可以让Android Studio 会把依赖的 jar 或 arr 缓存到本地,并且把模块名称设置为 hash 值

    这个开启后,可能导致includeJarFilter配置失效,AndroidStudio升级到 2.3.0修复这个问题

    每次编译生成的缓存在 $HOME/.android/build-cache

    如果缓存过多可以手动删除该目录进行清除

    升级到 Android Studio 2.3 后编译不兼容问题

    升级到 Android Studio 2.3 后,Gradle Plugin 也升级到 2.3.0

    对应推荐使用的 Gradle 版本是 3.3

    这时候会发现工程模块目录下 {module name}/build/intermediates/exploded-aar/

    目录没了

    它会在 $HOME/.android/build-cache 下生成一部分缓存文件,来代替 exploded-aar

    如果需要生成exploded-aar,可以配置项目目录下的 gradle.properties ,添加一行内容

    android.enableBuildCache=false

    然后重新构建项目即可在 {module name}/build/intermediates/看到 exploded-aar 目录

    相关文章

      网友评论

        本文标题:gradlew常见命令

        本文链接:https://www.haomeiwen.com/subject/qtombctx.html