<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Gradle常用的幾十個(gè)命令一網(wǎng)打盡!

          共 22628字,需瀏覽 46分鐘

           ·

          2023-07-19 17:41

              
                ?安卓進(jìn)階漲薪訓(xùn)練營(yíng)
                ,讓一部分人先進(jìn)大廠
                


          大家好,我是皇叔,最近開(kāi)了一個(gè)安卓進(jìn)階漲薪訓(xùn)練營(yíng),可以幫助大家突破技術(shù)&職場(chǎng)瓶頸,從而度過(guò)難關(guān),進(jìn)入心儀的公司。


          詳情見(jiàn)文章:沒(méi)錯(cuò)!皇叔開(kāi)了個(gè)訓(xùn)練營(yíng)


          作者:yechaoa
          https://juejin.cn/post/7171493698243395597


          前言


          Gradle的命令有很多,熟悉常用命令之后,在日常開(kāi)發(fā)中,不僅可以提升效率,也可以輔助我們快速定位并解決編譯問(wèn)題;而且某些情況下命令行(CLI)與按鈕執(zhí)行的編譯結(jié)果是不一樣的,比如構(gòu)建時(shí)要傳參(-P),所以就單拎出來(lái)一篇講解,希望對(duì)你有幫助~


          f18696fb17e573b29a390d4e696289a8.webp

          1.Gradle命令

          1.1、gradlew

          Gradle執(zhí)行命令行主要用到的是Gradle Wrapper,關(guān)于Gradle Wrapper的介紹,在前文( 【Gradle-2】一文搞懂Gradle配置 )中有介紹,這里不再贅述。

          https://juejin.cn/post/7160337743552675847#heading-5

          所以我們常用的 ./gradlew (Mac),gradlew即Gradle Wrapper的簡(jiǎn)寫。

          Gradle Wrapper工作流:

          e5223a28916a3219aeb2c55d48e430ad.webp

          再來(lái)看下gradlew的腳本內(nèi)容:

              
                  #!/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?"$PRG"?]?;?do
          ????ls=`ls?-ld?"$PRG"`
          ????link=`expr?"$ls"?:?'.*->?(.*)$'`
          ????if?expr?"$link"?:?'/.*'?>?/dev/null;?then
          ????????PRG="$link"
          ????else
          ????????PRG=`dirname?"$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?"$JAVA_HOME"?]?;?then
          ????if?[?-x?"$JAVA_HOME/jre/sh/java"?]?;?then
          ????????#?IBM's?JDK?on?AIX?uses?strange?locations?for?the?executables
          ????????JAVACMD="$JAVA_HOME/jre/sh/java"
          ????else
          ????????JAVACMD="$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?[?"$cygwin"?=?"false"?-a?"$darwin"?=?"false"?-a?"$nonstop"?=?"false"?]?;?then
          ????MAX_FD_LIMIT=`ulimit?-H?-n`
          ????if?[?$??-eq?0?]?;?then
          ????????if?[?"$MAX_FD"?=?"maximum"?-o?"$MAX_FD"?=?"max"?]?;?then
          ????????????MAX_FD="$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?$darwin;?then
          ????GRADLE_OPTS="$GRADLE_OPTS?"-Xdock:name=$APP_NAME"?"-Xdock:icon=$APP_HOME/media/gradle.icns""
          fi

          #?For?Cygwin?or?MSYS,?switch?paths?to?Windows?format?before?running?java
          if?[?"$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?$ROOTDIRSRAW?;?do
          ????????ROOTDIRS="$ROOTDIRS$SEP$dir"
          ????????SEP="|"
          ????done
          ????OURCYGPATTERN="(^($ROOTDIRS))"
          ????#?Add?a?user-defined?pattern?to?the?cygpath?arguments
          ????if?[?"$GRADLE_CYGPATTERN"?!=?""?]?;?then
          ????????OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
          ????fi
          ????#?Now?convert?the?arguments?-?kludge?to?limit?ourselves?to?/bin/sh
          ????i=0
          ????for?arg?in?"$@"?;?do
          ????????CHECK=`echo?"$arg"|egrep?-c?"$OURCYGPATTERN"?-`
          ????????CHECK2=`echo?"$arg"|egrep?-c?"^-"`?????????????????????????????????###?Determine?if?an?option

          ????????if?[?$CHECK?-ne?0?]?&&?[?$CHECK2?-eq?0?]?;?then????????????????????###?Added?a?condition
          ????????????eval?`echo?args$i`=`cygpath?--path?--ignore?--mixed?"$arg"`
          ????????else
          ????????????eval?`echo?args$i`=""$arg""
          ????????fi
          ????????i=`expr?$i?+?1`
          ????done
          ????case?$i?in
          ????????0)?set?--?;;
          ????????1)?set?--?"$args0"?;;
          ????????2)?set?--?"$args0"?"$args1"?;;
          ????????3)?set?--?"$args0"?"$args1"?"$args2"?;;
          ????????4)?set?--?"$args0"?"$args1"?"$args2"?"$args3"?;;
          ????????5)?set?--?"$args0"?"$args1"?"$args2"?"$args3"?"$args4"?;;
          ????????6)?set?--?"$args0"?"$args1"?"$args2"?"$args3"?"$args4"?"$args5"?;;
          ????????7)?set?--?"$args0"?"$args1"?"$args2"?"$args3"?"$args4"?"$args5"?"$args6"?;;
          ????????8)?set?--?"$args0"?"$args1"?"$args2"?"$args3"?"$args4"?"$args5"?"$args6"?"$args7"?;;
          ????????9)?set?--?"$args0"?"$args1"?"$args2"?"$args3"?"$args4"?"$args5"?"$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?$GRADLE_OPTS?""-Dorg.gradle.appname=$APP_BASE_NAME""?-classpath?""$CLASSPATH""?org.gradle.wrapper.GradleWrapperMain?"$APP_ARGS"

          exec?"$JAVACMD"?"$@"

          代碼量不多,gradlew主要干了幾件事:

          1. 獲取電腦系統(tǒng)內(nèi)核的信息,JRE環(huán)境信息等;

          2. 設(shè)置classpath路徑;

          3. 執(zhí)行java命令工具,調(diào)用gradle jar包的class文件;

          1.2、命令大全

          當(dāng)我們想知道一個(gè)工具有哪些命令的時(shí)候,最簡(jiǎn)單直接的方式就是使用help命令查看支持哪些,然后從中找到我們想要的。

          執(zhí)行:

              
                  ?./gradlew?--help

          輸出:

              
                  USAGE:?gradlew?[option...]?[task...]

          -?,?-h,?--help?????????????????????Shows?this?help?message.
          -a,?--no-rebuild???????????????????Do?not?rebuild?project?dependencies.
          -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.
          --include-build????????????????????Include?the?specified?build?in?the?composite.
          -M,?--write-verification-metadata??Generates?checksums?for?dependencies?used?in?the?project?(comma-separated?list)
          -m,?--dry-run??????????????????????Run?the?builds?with?all?task?actions?disabled.
          --max-workers??????????????????????Configure?the?number?of?concurrent?workers?Gradle?is?allowed?to?use.
          --no-build-cache???????????????????Disables?the?Gradle?build?cache.
          --no-configuration-cache???????????Disables?the?configuration?cache.?[incubating]
          --no-configure-on-demand???????????Disables?the?use?of?configuration?on?demand.?[incubating]
          --no-daemon????????????????????????Do?not?use?the?Gradle?daemon?to?run?the?build.?Useful?occasionally?if?you?have?configured?Gradle?to?always?run?with?the?daemon?by?default.
          --no-parallel??????????????????????Disables?parallel?execution?to?build?projects.
          --no-scan??????????????????????????Disables?the?creation?of?a?build?scan.?For?more?information?about?build?scans,?please?visit?https://gradle.com/build-scans.
          --no-watch-fs??????????????????????Disables?watching?the?file?system.
          --offline??????????????????????????Execute?the?build?without?accessing?network?resources.
          -P,?--project-prop?????????????????Set?project?property?for?the?build?script?(e.g.?-Pmyprop=myvalue).
          -p,?--project-dir??????????????????Specifies?the?start?directory?for?Gradle.?Defaults?to?current?directory.
          --parallel?????????????????????????Build?projects?in?parallel.?Gradle?will?attempt?to?determine?the?optimal?number?of?executor?threads?to?use.
          --priority?????????????????????????Specifies?the?scheduling?priority?for?the?Gradle?daemon?and?all?processes?launched?by?it.?Values?are?'normal'?(default)?or?'low'
          --profile??????????????????????????Profile?build?execution?time?and?generates?a?report?in?the?<build_dir>/reports/profile?directory.
          --project-cache-dir????????????????Specify?the?project-specific?cache?directory.?Defaults?to?.gradle?in?the?root?project?directory.
          -q,?--quiet????????????????????????Log?errors?only.
          --refresh-dependencies?????????????Refresh?the?state?of?dependencies.
          --refresh-keys?????????????????????Refresh?the?public?keys?used?for?dependency?verification.
          --rerun-tasks??????????????????????Ignore?previously?cached?task?results.
          -S,?--full-stacktrace??????????????Print?out?the?full?(very?verbose)?stacktrace?for?all?exceptions.
          -s,?--stacktrace???????????????????Print?out?the?stacktrace?for?all?exceptions.
          --scan?????????????????????????????Creates?a?build?scan.?Gradle?will?emit?a?warning?if?the?build?scan?plugin?has?not?been?applied.?(https://gradle.com/build-scans)
          --status???????????????????????????Shows?status?of?running?and?recently?stopped?Gradle?daemon(s).
          --stop?????????????????????????????Stops?the?Gradle?daemon?if?it?is?running.
          -t,?--continuous???????????????????Enables?continuous?build.?Gradle?does?not?exit?and?will?re-execute?tasks?when?task?file?inputs?change.
          --update-locks?????????????????????Perform?a?partial?update?of?the?dependency?lock,?letting?passed?in?module?notations?change?version.?[incubating]
          -v,?--version??????????????????????Print?version?info.
          -w,?--warn?????????????????????????Set?log?level?to?warn.
          --warning-mode?????????????????????Specifies?which?mode?of?warnings?to?generate.?Values?are?'all',?'fail',?'summary'(default)?or?'none'
          --watch-fs?????????????????????????Enables?watching?the?file?system?for?changes,?allowing?data?about?the?file?system?to?be?re-used?for?the?next?build.
          --write-locks??????????????????????Persists?dependency?resolution?for?locked?configurations,?ignoring?existing?locking?information?if?it?exists
          -x,?--exclude-task?????????????????Specify?a?task?to?be?excluded?from?execution.

          簡(jiǎn)寫:

              
                  ./gradlew -?
          //?or
          ./gradlew?-h

          可以看到上面的輸出已經(jīng)列出來(lái)了很多命令,可能有些見(jiàn)過(guò)有些沒(méi)見(jiàn)過(guò),下面將把常用的提煉出來(lái)講解,并進(jìn)行分類。

          1.3、命令結(jié)構(gòu)

              
                  gradle?[taskName...]?[--option-name...]

          多個(gè)任務(wù)用空格分隔。

          2.Gradle相關(guān)

          2.1、查看Gradle版本

          常見(jiàn)的查看Gradle版本有兩種方式。

          第一種是直接在gradle>wrapper> gradle-wrapper.properties 文件下查看 distributionUrl 所使用的gradle版本下載地址:

              
                  distributionUrl=https://services.gradle.org/distributions/gradle-7.4-bin.zip

          這里的 distributionUrl 即表示當(dāng)前所用的gradle版本為7.4。

          第二種就是使用命令行的方式查看當(dāng)前版本。

          執(zhí)行:

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

          輸出:

              
                  ------------------------------------------------------------
          Gradle?7.4
          ------------------------------------------------------------

          Build?time:???2022-02-08?09:58:38?UTC
          Revision:?????f0d9291c04b90b59445041eaa75b2ee744162586

          Kotlin:???????1.5.31
          Groovy:???????3.0.9
          Ant:??????????Apache?Ant(TM)?version?1.10.11?compiled?on?July?10?2021
          JVM:??????????11?(Oracle?Corporation?11+28)
          OS:???????????Mac?OS?X?10.16?x86_64

          2.2、升級(jí)Gradle

          常見(jiàn)的升級(jí)Gradle有3種方式。

          第一種,先手動(dòng)修改 wrapper.properties 文件下 distributionUrl 指向的版本,再手動(dòng)修改Android Gradle Plugin(AGP)版本,然后重新sync。

          第二種,打開(kāi)file>Project Structure修改AGP和Gradle的版本,然后apply。

          471dfee5b48a3fea740bd6ef354b281d.webp

          第三種,用命令行的方式(官方推薦,不過(guò)跟著AS一起升也是可以的):

              
                  ./gradlew?wrapper?--gradle-version?7.5.1
                

          3.編譯命令


          3.1、檢查依賴并編譯打包

              
                  ./gradlew?build

          3.2、編譯并打出Debug包

              
                  ./gradlew?assembleDebug

          3.3、編譯打出Debug包并安裝

              
                  ./gradlew?installDebug

          3.4、編譯并打出Release包

              
                  ./gradlew?assembleRelease

          3.5、編譯打出Release包并安裝

              
                  ./gradlew?installRelease

          3.6、Debug/Release編譯并打印日志

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

          4.清除命令

          清除構(gòu)建目錄下的產(chǎn)物。

              
                  ./gradlew?clean

          等同于Build->Clean Project。

          5.卸載命令

          5.1、卸載Debug/Release安裝包

              
                  ./gradlew?uninstallDebug
          //?or
          ./gradlew?uninstallRelease

          輸出:

              
                  Uninstalling?com.yechaoa.gradlex?(from?app:debug)?from?device?'Pixel_5_API_31(AVD)?-?12'?(emulator-5554).
          Uninstalled?com.yechaoa.gradlex?from?1?device.


          5.2、adb卸載

          在Android Studio中執(zhí)行是直接卸載的當(dāng)前項(xiàng)目安裝包,如果是adb執(zhí)行則需要指定包名。

              
                  adb?uninstall?com.yechaoa.gradlex??
                

          6.調(diào)試命令

          調(diào)試命令在定位編譯問(wèn)題的時(shí)候非常有用。

          當(dāng)我們遇到編譯錯(cuò)誤的時(shí)候,經(jīng)常會(huì)看到這個(gè)提示:

              
                  *?Try:
          >?Run?gradle?tasks?to?get?a?list?of?available?tasks.
          >?Run?with?--stacktrace?option?to?get?the?stack?trace.
          >?Run?with?--info?or?--debug?option?to?get?more?log?output.
          >?Run?with?--scan?to?get?full?insights.


          6.1、編譯并打印堆棧日志

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

          詳細(xì)版:

              
                  ./gradlew?assembleDebug?--full-stacktrace
          //?or
          ./gradlew?assembleDebug?-S


          6.2、日志級(jí)別

          有時(shí)候構(gòu)建日志會(huì)有很多,看到的可能不全,甚至不是真正的編譯問(wèn)題,而構(gòu)建日志又不能像logcat那樣可以可視化的篩選,這個(gè)時(shí)候就需要用日志級(jí)別來(lái)篩選一下。

              
                  -q,--quiet
          僅記錄錯(cuò)誤。

          -w,--warn
          將日志級(jí)別設(shè)置為警告。

          -i,--info
          將日志級(jí)別設(shè)置為信息。

          -d,--debug
          調(diào)試模式(包括正常的stacktrace)。


          示例:

              
                  ./gradlew?assembleDebug?-w
                

          7.任務(wù)相關(guān)

          7.1、查看主要Task

              
                  ./gradlew?tasks


          7.2、查看所有Task

              
                  ./gradlew?tasks?--all


          7.3、執(zhí)行Task

              
                  ./gradlew?taskName
          //?or
          ./gradlew?:moduleName:taskName


          同時(shí),可在AS右側(cè)工具類Gradle中查看項(xiàng)目及module的Task,并可以點(diǎn)擊執(zhí)行對(duì)應(yīng)Task。

          ee9f273b93854c9c6d61b38bbe42d520.webp

          8.查看依賴

          編譯有很多問(wèn)題都是依賴導(dǎo)致的錯(cuò)誤,查看依賴能幫我們快速定位問(wèn)題所在。


          8.1、查看項(xiàng)目根目錄下的依賴

              
                  ./gradlew?dependencies

          8.2、查看app模塊下的依賴

              
                  ./gradlew?app:dependencies


          8.3、查看依賴輸出到文件

              
                  ./gradlew?app:dependencies?>?dependencies.txt
                


          示例:

          cd14df90d9c3dd634ace4fd69188122d.webp

          9.性能相關(guān)

          9.1、離線編譯

              
                  ./gradlew?assembleDebug?--offline


          9.2、構(gòu)建緩存

              
                  ./gradlew?assembleDebug?--build-cache?//?開(kāi)啟

          ./gradlew?assembleDebug?--no-build-cache?//?不開(kāi)啟


          9.3、配置緩存

              
                  ./gradlew?assembleDebug?--configuration-cache?//?開(kāi)啟

          ./gradlew?assembleDebug?--no-configuration-cache?//?不開(kāi)啟


          9.4、并行構(gòu)建

              
                  ./gradlew?assembleDebug?--parallel?//?開(kāi)啟

          ./gradlew?assembleDebug?--no-parallel?//?不開(kāi)啟


          以上9.1-9.4的配置也都可以在 gradle.properties 中配置。

          示例:

              
                  #并行編譯
          org.gradle.parallel=true

          #構(gòu)建緩存
          org.gradle.caching=true


          9.5、編譯并輸出性能報(bào)告

              
                  ./gradlew?assembleDebug?--profile
                

          性能報(bào)告位于構(gòu)建項(xiàng)目的GradleX/build/reports/profile/路徑下。

              
                  See?the?profiling?report?at:?file:///Users/yechao/AndroidStudioProjects/GradleX/build/reports/profile/profile-2022-11-29-23-13-29.html


          輸出的是html文件,用瀏覽器打開(kāi):

          9ddfba117dec07dffd21b35656b0b4ea.webp

          9.6、編譯并輸出更詳細(xì)性能報(bào)告

              
                  ./gradlew?assembleDebug?--scan




          首次執(zhí)行需要郵箱驗(yàn)證,授權(quán)即可,完事之后即可打開(kāi)鏈接,scan報(bào)告內(nèi)容比profile更加詳細(xì)。

          6eac2ef6904298979ca57c19e0dee6d8.webp

          10.動(dòng)態(tài)傳參

          再來(lái)介紹一個(gè)比較常用的傳參屬性, --project-prop ,我們一般常用-P表示,用來(lái)設(shè)置根項(xiàng)目的項(xiàng)目屬性。

          10.1、獲取參數(shù)

          示例:

              
                  ?./gradlew?assembleDebug?-PisTest=true?

          這里我們用-P傳了一個(gè)isTest字段,并賦值為true。

          那在代碼里如何獲取這個(gè)參數(shù)呢?然后在 build.gradle 中編寫如下代碼:

              
                  if?(hasProperty("isTest")){
          ????println("---hasProperty?isTest?yes")
          }else?{
          ????println("---hasProperty?isTest?no")
          }

          我們可以用 hasProperty 來(lái)獲取命令行(CLI)的參數(shù),module或者插件也可以這么獲取:

              
                  project.property('isTest')

          然后我們用上面的命令編譯看下輸出:

              
                  ???GradleX?git:(master)???./gradlew?assembleDebug?-PisTest=true?
          ---Gradle:開(kāi)始初始化了
          ---Gradle:settingsEvaluated Settings對(duì)象評(píng)估完畢
          ---Gradle:projectsLoaded 準(zhǔn)備加載Project對(duì)象了

          >?Configure?project?:
          ---Gradle:Projec beforeEvaluate Project開(kāi)始評(píng)估,對(duì)象是?= GradleX
          ---hasProperty?isTest?yes?//???---?看這里?---
          ---Gradle:Projec afterEvaluate Project評(píng)估完畢,對(duì)象是?= GradleX

          >?Configure?project?:app
          ---Gradle:buildFinished 構(gòu)建結(jié)束了

          可以看到已經(jīng)打印出來(lái)了。

          還沒(méi)完,獲取到參數(shù)是不錯(cuò),但是還沒(méi)獲取到參數(shù)的值。

          10.2、獲取參數(shù)值

          我們可以用 getProperty() 來(lái)獲?。?/span>

              
                  if?(hasProperty("isTest"))?{
          ????println("---hasProperty?isTest?yes")
          ????if?(Boolean.valueOf(getProperty('isTest')))?{
          ????????println("---isTest?true")
          ????}?else?{
          ????????println("---isTest?false")
          ????}
          }?else?{
          ????println("---hasProperty?isTest?no")
          }

          注意, getProperty('isTest') 這里要用單引號(hào),另外命令行里面的參數(shù)值都是對(duì)象,還需要基本數(shù)據(jù)類型轉(zhuǎn)換一下,即 Boolean.valueOf(getProperty('isTest')) 。

          10.3、自定義操作

          ok,現(xiàn)在我們就可以針對(duì)獲取的參數(shù)去做一些自定義的操作了,比如修改我們的依賴。

          app> build.gradle

              
                  dependencies?{

          ????implementation?'androidx.core:core-ktx:1.7.0'

          ????if?(project.hasProperty("isTest"))?{
          ????????println("---hasProperty?isTest?yes")
          ????????if?(Boolean.valueOf(getProperty('isTest')))?{
          ????????????println("---isTest?true")

          ????????????implementation?'com.yechaoa.gradlex.devtools:devtools:1.1.1'

          ????????}?else?{
          ????????????println("---isTest?false")

          ????????????implementation?'com.yechaoa.gradlex.devtools:devtools:2.2.2'
          ????????}
          ????}?else?{
          ????????println("---hasProperty?isTest?no")
          ????}

          ????testImplementation?'junit:junit:4.13.2'
          }

          這里舉例,在 isTest=true 的時(shí)候依賴了devtools 1.1.1版本, isTest=false 時(shí)依賴了devtools 2.2.2版本。

          除了 dependencies 里面的依賴之外,Plugin、Task之類的也可以通過(guò)動(dòng)態(tài)傳參的方式去做自定義操作。

          11.總結(jié)

          本文介紹了Gradle Command-Line Interface(CLI)相關(guān)的知識(shí),像調(diào)試命令、查看依賴、性能相關(guān)、動(dòng)態(tài)傳參這些,在定位問(wèn)題、提升效率的時(shí)候還是非常有用的,希望能給你帶來(lái)收獲。(別忘了三連啊喂~)

          Github

          https://github.com/yechaoa/GradleX

          相關(guān)文檔 Command-Line Interface https://docs.gradle.org/current/userguide/command_line_interface.html The Gradle Wrapper https://docs.gradle.org/current/userguide/gradle_wrapper.html Shell腳本gradle命令和gradlew命令的區(qū)別 https://www.jianshu.com/p/9be8317e7d2c Gradle Logging https://docs.gradle.org/current/userguide/logging.html Build Environment https://docs.gradle.org/current/userguide/build_environment.html Gradle Project https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:hasProperty(java.lang.String) 深度探索 Gradle 自動(dòng)化構(gòu)建技術(shù)(一、Gradle 核心配置篇) https://juejin.cn/post/6844904122492125198


          567ab04c8f1b82c04bed2b2b57128edd.webp a2545081d2d51d315fd317770e669601.webp
          3fbb480376827eca6cde3e6d5ef8ec29.webp


          為了防止失聯(lián),歡迎關(guān)注我防備的小號(hào)

          ?

          ? ? ? ? ? ? ???微信改了推送機(jī)制,真愛(ài)請(qǐng)星標(biāo)本公號(hào)??

          瀏覽 80
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  蜜桃视频成人 免费 | 天天操天天透 | 一级二级黄色视屏 | 青青青免费视频观看在线 | 豆花视频在线观看国产豆花 |