Gradle常用命令与参数&依赖管理和版本决议(1),2024年最新HarmonyOS鸿蒙解答题面试题

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新HarmonyOS鸿蒙全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img

img
img
htt

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注鸿蒙)
img

正文

#!/usr/bin/env sh

Copyright 2015 the original author or authors.

Licensed under the Apache License, Version 2.0 (the “License”);

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an “AS IS” BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

##############################################################################

Gradle start up script for UN*X

##############################################################################

Attempt to set APP_HOME

Resolve links: $0 may be a link

PRG=“$0”

Need this for relative symlinks.

while [ -h “ P R G " ] ; d o l s = ‘ l s − l d " PRG" ] ; do ls=`ls -ld " PRG"];dols=lsld"PRG” link=expr "KaTeX parse error: Can't use function '\(' in math mode at position 13: ls" : '.*-> \̲(̲.*\) if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG=dirname “ P R G " ‘ " / PRG"`"/ PRG"‘"/link”
fi
done
SAVED=“pwd
cd “dirname \"$PRG\"/” >/dev/null
APP_HOME=“pwd -P
cd “$SAVED” >/dev/null

APP_NAME=“Gradle”
APP_BASE_NAME=basename "$0"

Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.

DEFAULT_JVM_OPTS=‘“-Xmx64m” “-Xms64m”’

Use the maximum available, or set MAX_FD != -1 to use that value.

MAX_FD=“maximum”

warn () {
echo “$*”
}

die () {
echo
echo “$*”
echo
exit 1
}

OS specific support (must be ‘true’ or ‘false’).

cygwin=false
msys=false
darwin=false
nonstop=false
case “uname” in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar

Determine the Java command to use to start the JVM.

if [ -n “ J A V A H O M E " ] ; t h e n i f [ − x " JAVA_HOME" ] ; then if [ -x " JAVAHOME"];thenif[x"JAVA_HOME/jre/sh/java” ] ; then

IBM’s JDK on AIX uses strange locations for the executables

JAVACMD=“ J A V A H O M E / j r e / s h / j a v a " e l s e J A V A C M D = " JAVA_HOME/jre/sh/java" else JAVACMD=" JAVAHOME/jre/sh/java"elseJAVACMD="JAVA_HOME/bin/java”
fi
if [ ! -x “$JAVACMD” ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=“java”
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no ‘java’ command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi

Increase the maximum file descriptors if we can.

if [ “ c y g w i n " = " f a l s e " − a " cygwin" = "false" -a " cygwin"="false"a"darwin” = “false” -a “$nonstop” = “false” ] ; then
MAX_FD_LIMIT=ulimit -H -n
if [ ? − e q 0 ] ; t h e n i f [ " ? -eq 0 ] ; then if [ " ?eq0];thenif["MAX_FD" = “maximum” -o “ M A X F D " = " m a x " ] ; t h e n M A X F D = " MAX_FD" = "max" ] ; then MAX_FD=" MAXFD"="max"];thenMAXFD="MAX_FD_LIMIT”
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn “Could not set maximum file descriptor limit: $MAX_FD”
fi
else
warn “Could not query maximum file descriptor limit: $MAX_FD_LIMIT”
fi
fi

For Darwin, add options to specify how the application appears in the dock

if d a r w i n ; t h e n G R A D L E O P T S = " darwin; then GRADLE_OPTS=" darwin;thenGRADLEOPTS="GRADLE_OPTS “-Xdock:name=KaTeX parse error: Expected group as argument to '\"' at position 11: APP_NAME\" ̲\"-Xdock:icon=APP_HOME/media/gradle.icns”"
fi

For Cygwin or MSYS, switch paths to Windows format before running java

if [ “ c y g w i n " = " t r u e " − o " cygwin" = "true" -o " cygwin"="true"o"msys” = “true” ] ; then
APP_HOME=cygpath --path --mixed "$APP_HOME"
CLASSPATH=cygpath --path --mixed "$CLASSPATH"

JAVACMD=cygpath --unix "$JAVACMD"

We build the pattern for arguments to be converted via cygpath

ROOTDIRSRAW=find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null
SEP=“”
for dir in R O O T D I R S R A W ; d o R O O T D I R S = " ROOTDIRSRAW ; do ROOTDIRS=" ROOTDIRSRAW;doROOTDIRS="ROOTDIRS S E P SEP SEPdir"
SEP=“|”
done
OURCYGPATTERN=“(^($ROOTDIRS))”

Add a user-defined pattern to the cygpath arguments

if [ “ G R A D L E C Y G P A T T E R N " ! = " " ] ; t h e n O U R C Y G P A T T E R N = " GRADLE_CYGPATTERN" != "" ] ; then OURCYGPATTERN=" GRADLECYGPATTERN"!=""];thenOURCYGPATTERN="OURCYGPATTERN|($GRADLE_CYGPATTERN)”
fi

Now convert the arguments - kludge to limit ourselves to /bin/sh

i=0
for arg in “ @ " ; d o C H E C K = ‘ e c h o " @" ; do CHECK=`echo " @";doCHECK=echo"arg”|egrep -c “ O U R C Y G P A T T E R N " − ‘ C H E C K 2 = ‘ e c h o " OURCYGPATTERN" -` CHECK2=`echo " OURCYGPATTERN"CHECK2=echo"arg”|egrep -c “^-”` ### Determine if an option

if [ $CHECK -ne 0 ] && [ KaTeX parse error: Expected 'EOF', got '#' at position 42: … #̲## Added a cond…i`=`cygpath --path --ignore --mixed “ a r g " ‘ e l s e e v a l ‘ e c h o a r g s arg"` else eval `echo args arg"‘elseevalechoargsi`=”“$arg”"
fi
i=`expr $i + 1`
done
case $i in
0) set – ;;

  1. set – “$args0” ;;
  2. set – “ a r g s 0 " " args0" " args0""args1” ;;
  3. set – “ a r g s 0 " " args0" " args0""args1” “$args2” ;;
  4. set – “ a r g s 0 " " args0" " args0""args1” “ a r g s 2 " " args2" " args2""args3” ;;
  5. set – “ a r g s 0 " " args0" " args0""args1” “ a r g s 2 " " args2" " args2""args3” “$args4” ;;
  6. set – “ a r g s 0 " " args0" " args0""args1” “ a r g s 2 " " args2" " args2""args3” “ a r g s 4 " " args4" " args4""args5” ;;
  7. set – “ a r g s 0 " " args0" " args0""args1” “ a r g s 2 " " args2" " args2""args3” “ a r g s 4 " " args4" " args4""args5” “$args6” ;;
  8. set – “ a r g s 0 " " args0" " args0""args1” “ a r g s 2 " " args2" " args2""args3” “ a r g s 4 " " args4" " args4""args5” “ a r g s 6 " " args6" " args6""args7” ;;
  9. set – “ a r g s 0 " " args0" " args0""args1” “ a r g s 2 " " args2" " args2""args3” “ a r g s 4 " " args4" " args4""args5” “ a r g s 6 " " args6" " args6""args7” “$args8” ;;
    esac
    fi

Escape application args

save () {
for i do printf %s\n “$i” | sed “s/‘/’\\‘’/g;1s/^/‘/;$s/$/’ \\/” ; done
echo " "
}
APP_ARGS=save "$@"

Collect all arguments for the java command, following the shell quoting and substitution rules

eval set – $DEFAULT_JVM_OPTS $JAVA_OPTS G R A D L E O P T S " − ¨ D o r g . g r a d l e . a p p n a m e = GRADLE_OPTS "\"-Dorg.gradle.appname= GRADLEOPTS"¨Dorg.gradle.appname=APP_BASE_NAME"" -classpath "“ C L A S S P A T H " ¨ o r g . g r a d l e . w r a p p e r . G r a d l e W r a p p e r M a i n " CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain " CLASSPATH"¨org.gradle.wrapper.GradleWrapperMain"APP_ARGS”

exec “ J A V A C M D " " JAVACMD" " JAVACMD""@”

主要干了这几件事

  1. 设置 APP_HOME
  2. 配置默认的 JVM 选项
  3. 设置 CLASSPATH 路径,即指定 gradlew-wrapper.jar 路径
  4. 检查 JAVA 环境配置等
1.2、命令大全

执行 ./gradlew --help gradle 会为我们输出帮助信息:(这里只列出了部分,更多可以自己敲命令去看看)

PS D:\workspace\GradleDemo> ./gradlew --help

To see help contextual to the project, use gradlew help

USAGE: gradlew [option…] [task…]

-?, -h, --help Shows this help message.
-b, --build-file Specify the build file. [deprecated]
–build-cache Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds.
-c, --settings-file Specify the settings file. [deprecated]
–configuration-cache Enables the configuration cache. Gradle will try to reuse the build configuration from previous builds. [incubating]
–configuration-cache-problems Configures how the configuration cache handles problems (fail or warn). Defaults to fail. [incubating]
–configure-on-demand Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating]
–console Specifies which type of console output to generate. Values are ‘plain’, ‘auto’ (default), ‘rich’ or ‘verbose’.
–continue Continue task execution after a task failure.
-D, --system-prop Set system property of the JVM (e.g. -Dmyprop=myvalue).
-d, --debug Log in debug mode (includes normal stacktrace).
–daemon Uses the Gradle daemon to run the build. Starts the daemon if not running.
–export-keys Exports the public keys used for dependency verification.
-F, --dependency-verification Configures the dependency verification mode. Values are ‘strict’, ‘lenient’ or ‘off’.
–foreground Starts the Gradle daemon in the foreground.
-g, --gradle-user-home Specifies the Gradle user home directory. Defaults to ~/.gradle
-I, --init-script Specify an initialization script.
-i, --info Set log level to info.
-p, --project-dir

这些命令也是可以简写的,例如 --help 可以简写为 -?-h

1.3、命令格式

gradle 命令的整体格式如下:

gradle [taskName…] [–option-name…]

2 gradle 常见命令

根据命令的功能目的,Gradle 命令大致可以分为下图的这些类型。

2.1 gradle 自身相关
2.1.1 查看 gradle 版本

查看 gradle 版本一般我们都是直接看项目中的 gradle.property 文件,里面的 distributionUrl 参数。除了这种方式,我们还可以通过gradle 命令查看: 输入:

./gradlew -version
//or
./gradlew -v

输出:


Gradle 8.0

Build time: 2023-02-13 13:15:21 UTC
Revision: 62ab9b7c7f884426cf79fbedcf07658b2dbe9e97

Kotlin: 1.8.10
Groovy: 3.0.13
Ant: Apache Ant™ version 1.10.11 compiled on July 10 2021
JVM: 17.0.8 (Amazon.com Inc. 17.0.8+7-LTS)
OS: Mac OS X 13.0.1 x86_64

2.1.2 gradle 版本升级

1、直接修改工程 gradle/wrapper/gradle-wrapper.properties 文件的 distributionUrl修改 gradle 版本 2、File>Project Structure 修改 AndroidGradle Plugin Version 和 Gradle 的版本然后 apply 3、使用命令行输入命令升级

./gradlew wrapper --gradle-version xxx

2.1.3 列出某个命令将要执行的所有任务(并不是真的执行它)

./gradlew xxx --dry-run

其中xxx就是要执行的命令,我们在学习的时候可以通过这个命令来快速的查看某个命令将要执行哪些任务。也方便快速对比相似功能的命令在执行过程中有哪些差异。

2.2 编译命令
2.2.1 编译打包

./gradlew build
//or
./gradlew assemble

这两个命令都是编译并输出全部类型的包。他们之间差别在于,build 命令是包含了 assemble 命令的全生命周期的,从上面的 task --all 命令说明也可以看出。下面的这个图可以更直观的反映:

分别执行 ./gradlew assemble --dry-run./gradlew assemble --dry-run命令,把输出结果进行对比。差别就是 build 命令会在 assemble 之后执行额外的 check 任务

2.2.2 编译 debug 包

./gradlew assembleDebug
//or
//反映
./gradlew installDebug

两个任务前面步骤一致,最后一步不一样,分别是执行 :app:assebmleDebug:app:installDebug

2.2.3 编译 release 包

./gradlew assembleRelease
//or
./gradlew installRelease

两个任务前面步骤一致,最后一步不一样,分别是执行 :app:assebmleRelease:app:installRelease

2.2.4 编译并打印日志

./gradlew assembleDebug --info
//or
./gradlew assembleRelease --info

加上 --info,编译日志会更完整,方便定位问题。

2.3 清除命令

./gradlew clean

等同于 Build > Clean Project,会清除构建目录下的产物

2.4 卸载命令

./gradlew uninstallDebug
//or
./gradlew uninstallRelease

这个命令是直接卸载连接设备上的当前项目安装包,跟 adb 指定包名进行卸载一样

2.5 调试命令

当我们使用 gradle 进行打包报错时,调试命令就可以帮我们更方便的定位出问题原因

2.5.1 编译并输出堆栈日志

./gradlew assembleDebug --stacktrace
//or
./gradlew assembleDebug -s

或者可以在参数前加上 --full 输出更详细的日志信息

./gradlew assembleDebug --full-stacktrace
//or
./gradlew assembleDebug --full-s

2.5.2 设置日志级别

Gradle 的日志分级

Level作用
ERROR错误
WARNING警告
LIFECYCLE生命周期
INFO信息
DEBUG调试
TRACE跟踪

日志过滤

过滤命令作用
-q 或 -quiet只显示 ERROR 和 WARNING 日志
–i 或 --info显示 INFO 及以上日志
-d 或 --debug显示 DEBUG 及以上日志

示例:

./gradlew assembleDebug -e

2.6 任务相关命令
2.6.1 查看项目 gradle task

./gradlew tasks //只列出关键的 task
//or
./gradlew tasks --all //所有的 task 都将列出

这个命令将列出项目所有要执行的 task,并且会有这个 task 的相应说明信息(这里只截取了一部分)

2.6.2 执行 Task

./gradlew
//or
./gradlew ::

可以在 AndroidStudio 右边栏的 Gradle 菜单下选择 Tasks 执行也是一样的效果

2.7 依赖查看命令

依赖查看命令可以让我们更清晰的了解引入的依赖库,解决因为依赖导致的错误或者剔除一些不需要的依赖

./gradlew app:dependencies //输出 app 模块依赖树
//or
./gradlew dependencies > xxx.txt //输出依赖树到 xxx.txt 文件

依赖关系树后面有的会跟(*)``(c)有的又没有,代表如下意义:

  1. * :表示该依赖项已被标记为被选中的版本。这意味着它是根据解析规则中的约束条件被选择的版本,而不是默认的最新版本。这通常是由于在项目的构建配置中明确指定了依赖项的版本或者存在其他依赖项对其版本有限制的情况。
  2. c :表示该依赖项是由于冲突解决而被强制选择的版本。当项目中存在多个依赖项,它们对同一依赖的版本有不同的要求时,Gradle 将尝试解决这些冲突。如果无法找到满足所有依赖项的版本,Gradle 将选择一个版本并标记为冲突(conflict)。(c) 标记表示该依赖项是由于冲突解决而被强制选择的版本。

举个例子:

假设项目 A 依赖于库 X 的版本 1.0,而项目 B 依赖于库 X 的版本 2.0。这两个项目都作为依赖项添加到同一个项目 C 中。现在,项目 C 需要同时满足项目 A 和项目 B 的依赖。

Gradle 将尝试解决这个冲突,它会检查是否有一个版本可以同时满足项目 A 和项目 B 的依赖。如果存在满足所有依赖项的版本,Gradle 将选择该版本,并将其标记为被选中的版本 (*)

然而,如果无法找到满足所有依赖项的版本,Gradle 将不得不做出一个决策。在这种情况下,Gradle 会选择一个版本,并将其标记为冲突 (c)。这表示所选的版本是为了解决冲突而被强制选择的,可能无法完全满足所有依赖项的要求。

2.8 性能相关命令
2.8.1 使用本地缓存版本离线编译

./gradlew assembleDebug --offline

也可以在AndroidStudio 侧边栏可视化开启关闭。差别在于,如果没开启离线编译,每次编译的时候都会去检查版本,对于没指定版本号或者动态版本号的依赖每次都会以最新的版本进行编译。

2.8.2 构建缓存

./gradlew assembleDebug --build-cache //开启
./gradlew assembleDebug --no-build-cache //不开启

2.8.3 配置缓存

./gradlew assembleDebug --configuration-cache //开启
./gradlew assembleDebug --no-configuration-cache //不开启

2.8.4 并行构建

./gradlew assembleDebug --parallel //开启
./gradlew assembleDebug --no-parallel //不开启

性能相关的这几个命令执行效果,和直接在 gradle.properties 中配置效果是一样的。

2.8.5 编译性能报告

./gradlew assembleDebug --profile

会在项目目录的 build > reports > profile 路径下生成报告文件

./gradlew assembleDebug --scan

会生成更详细的在线报告,首次执行会邮箱验证授权,完成后会输出一个链接。

2.9 动态传参命令

./gradlew assembleDebug -PisTest=true

build.gradle 中通过如下方式获取参数值

if(project.hasProperty(“isTest”)){
// do something
}else{
// do something else
}

通过 hasProperty 来获取命令行的参数

project.getProperty(“isTest”)

上面的方式都是判断参数是否存在,那么怎么获取参数的值呢?

if(project.hasProperty(“isTest”)){
println(“hasProperty isTest yes”)
if(Boolean.valueOf(project.getProperty(‘isTest’)){
println(“property isTest true”)
}else{
println(“property isTest false”)
}
}else{
println(“hasProperty isTest no”)
}

获取参数的数据类型需要基本数据类型转换成对应的类型,即 XXX.valueOf(getProperty('key'))。通过动态参数,我们可以在打包过程中做一些差异化的构建。

二、依赖管理和版本决议

在我们的开发中,经常会遇到各个模块中引入了相同的三方库,但是三方库版本又各不相同。有时候只是升级了一下某个依赖库存,结果运行就各种报错了。这部分课程将介绍一下 Gradle 的依赖管理方式和版本决议机制,弄明白 Gradle 是怎么把三方库引入到项目中的。在遇到同库不同版本的时候又是怎样决定使用哪个版本的。

1、依赖管理

不使用 maven 方式接入三方库,我们一般都是直接导入 jar 包或者 aar 包。替换版本的时候又重新去导入新的版本的 jar 或者 aar。遇到导入了相同 jar 或者 aar 的时候还出现冲突编不过。如果我们基于 Gradle 来开发项目,则可以依靠 Gradle 比较灵活方便的对依赖库版本进行管理。下图是 Gradle 工作的大致示意图:

构建过程中,Gradle 会先从本地检索,找不到就挨个从远端找依赖库,然后缓存到本地。 我们也可以通过以下方式,开启离线编译,对于未指定版本号或者使用动态版本号的依赖使用本地的版本进行编译

1.1 声明依赖
1.1.1 配置 maven 和依赖信息

声明 maven 依赖包含两部分:

  • 仓库配置 我这里是在 gradle 8.0 的环境下演示的。配置信息在 projetc > settings.gradle 下,低版本配置信息一般在 project > build.gradle 下

pluginManagement {
repositories {
//指定 maven 仓库的地址
maven { url “https://www.jitpack.io” }
google()
//maven 官方仓库地址
mavenCentral()
//自定义 gitlab 仓库作为 maven 地址

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注鸿蒙)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

赖使用本地的版本进行编译

1.1 声明依赖
1.1.1 配置 maven 和依赖信息

声明 maven 依赖包含两部分:

  • 仓库配置 我这里是在 gradle 8.0 的环境下演示的。配置信息在 projetc > settings.gradle 下,低版本配置信息一般在 project > build.gradle 下

pluginManagement {
repositories {
//指定 maven 仓库的地址
maven { url “https://www.jitpack.io” }
google()
//maven 官方仓库地址
mavenCentral()
//自定义 gitlab 仓库作为 maven 地址

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注鸿蒙)
[外链图片转存中…(img-WtEm24D9-1713128244533)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 23
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值