{"id":347053,"date":"2023-05-02T04:43:56","date_gmt":"2023-05-02T03:43:56","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=kotlin&#038;p=347053"},"modified":"2023-05-02T04:44:00","modified_gmt":"2023-05-02T03:44:00","slug":"kotlin-dsl-gradle","status":"publish","type":"kotlin","link":"https:\/\/blog.jetbrains.com\/zh-hans\/kotlin\/2023\/05\/kotlin-dsl-gradle\/","title":{"rendered":"Kotlin DSL \u73b0\u5728\u6210\u4e3a\u65b0 Gradle \u6784\u5efa\u7684\u9ed8\u8ba4\u8bbe\u7f6e"},"content":{"rendered":"<p>\u6211\u4eec\u59cb\u7ec8\u81f4\u529b\u4e8e\u5e2e\u52a9\u5f00\u53d1\u8005\u7f16\u5199\u7ed3\u6784\u66f4\u597d\u3001\u66f4\u6613\u7ef4\u62a4\u7684\u6784\u5efa\u3002 \u57fa\u4e8e\u8fd9\u4e2a\u76ee\u6807\uff0c\u5c06 Kotlin \u5e94\u7528\u4e8e\u7f16\u5199 Gradle \u6784\u5efa\u811a\u672c\u548c\u63d2\u4ef6\u4e5f\u5c31\u6c34\u5230\u6e20\u6210\u3002 Kotlin \u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u662f\u521b\u5efa DSL \u7684\u7edd\u4f73\u9009\u62e9\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u5f00\u53d1\u8005\u4f53\u9a8c\u3002<\/p>\n<p>\u81ea\u4ece <a href=\"https:\/\/blog.jetbrains.com\/kotlin\/2016\/05\/gradle-meets-kotlin\/\" target=\"_blank\" rel=\"noreferrer noopener\">Kotlin \u9996\u6b21\u88ab\u5f15\u5165 Gradle<\/a> \u4ee5\u6765\uff0c\u6211\u4eec\u5b8c\u6210\u4e86\u8bb8\u591a\u5de5\u4f5c\uff0c\u65e8\u5728\u8ba9 Gradle \u7528\u6237\u7684\u4f53\u9a8c\u66f4\u4e3a\u6d41\u7545\u6109\u5feb\u3002 \u4eca\u5929\uff0c\u6211\u4eec\u5f88\u9ad8\u5174\u5206\u4eab\u4e00\u4e2a\u91cd\u8981\u6d88\u606f\uff1aGradle \u5ba3\u5e03<strong><a title=\"https:\/\/blog.gradle.org\/kotlin-dsl-is-now-the-default-for-new-gradle-builds\" href=\"https:\/\/blog.gradle.org\/kotlin-dsl-is-now-the-default-for-new-gradle-builds\" target=\"_blank\" rel=\"noreferrer noopener\">\u5c06 Kotlin DSL \u9009\u4e3a\u65b0 Gradle \u6784\u5efa\u7684\u9ed8\u8ba4\u8bbe\u7f6e<\/a><\/strong>\uff01 \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u5b83\u5c06\u6210\u4e3a\u63d2\u4ef6\u548c\u6587\u6863\u4e2d\u7684\u9ed8\u8ba4\u9009\u9879\u3002 \u5f97\u76ca\u4e8e\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u4e00\u6d41\u7684\u51fd\u6570\u548c\u6269\u5c55\u65b9\u6cd5\uff0cKotlin \u53ef\u4ee5\u7ef4\u62a4\u751a\u81f3\u6539\u8fdb\u521b\u5efa Gradle \u811a\u672c\u7684\u6700\u4f73\u5143\u7d20\u3002<\/p>\n<p><!--more--><\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-341259\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/04\/header-Kotlin-for-Android-KTS-first-in-Android-Build-asset-2-2-2800x834.png\" alt=\"JetBrains\u3001Google \u548c Gradle \u5ba3\u5e03 Kotlin DSL \u73b0\u5728\u6210\u4e3a\u65b0 Gradle \u6784\u5efa\u7684\u9ed8\u8ba4\u8bbe\u7f6e\" width=\"2800\" height=\"834\" \/><\/figure>\n<blockquote class=\"wp-block-quote\">\n<p><em>\u201c<em>\u611f\u8c22 JetBrains\u3001Google \u548c Gradle \u4e4b\u95f4\u7684\u5171\u540c\u52aa\u529b\uff0cKotlin DSL \u5df2\u7ecf\u8fbe\u5230\u4e86\u66f4\u6210\u719f\u7684\u6c34\u5e73\uff0c\u6210\u4e3a\u5f00\u53d1\u8005\u53ef\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u7684\u66f4\u9ad8\u6548\u3001\u66f4\u4eba\u6027\u5316\u7684\u5de5\u5177\u3002\u201d <\/em> ~ Gradle \u56e2\u961f<\/em><\/p>\n<\/blockquote>\n<p>\u6b64\u5916\uff0cGoogle \u53d1\u8868\u58f0\u660e\uff0c\u8868\u793a<a title=\"https:\/\/android-developers.googleblog.com\/2023\/04\/kotlin-dsl-is-now-default-for-new-gradle-builds.html\" href=\"https:\/\/android-developers.googleblog.com\/2023\/04\/kotlin-dsl-is-now-default-for-new-gradle-builds.html\" target=\"_blank\" rel=\"noreferrer noopener\">\u5c06 Kotlin DSL \u4f5c\u4e3a Android \u5e94\u7528 Gradle \u6784\u5efa\u6587\u4ef6\u7684\u9ed8\u8ba4\u811a\u672c<\/a>\u3002<\/p>\n<p>Google\u3001Gradle \u548c JetBrains \u5c06\u7ee7\u7eed\u7d27\u5bc6\u5408\u4f5c\uff0c\u7ef4\u6301\u7edf\u4e00\u7684\u4ee3\u7801\u6837\u5f0f\u548c\u5b9a\u4e49\u7684\u4e60\u8bed\uff0c\u5e2e\u52a9\u6211\u4eec\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u60f3\u5206\u4eab Kotlin DSL \u5728\u7f16\u5199 Gradle \u811a\u672c\u65b9\u9762\u7684\u4e00\u4e9b\u5173\u952e\u4f18\u52bf\uff0c\u4ee5\u53ca IDE \u5bf9 build.gradle.kts \u6587\u4ef6\u7684\u652f\u6301\u7684\u73b0\u6709\u529f\u80fd\u3002<\/p>\n<h2 class=\"wp-block-heading\">Kotlin DSL \u76f8\u8f83\u4e8e Groovy \u7684\u4f18\u52bf<\/h2>\n<p>\u4e0d\u540c\u5f00\u53d1\u8005\u53ef\u80fd\u51fa\u4e8e\u4e0d\u540c\u7684\u539f\u56e0\u9009\u62e9\u4e00\u79cd\u8bed\u8a00\u800c\u975e\u53e6\u4e00\u79cd\u8bed\u8a00\u3002 \u5728\u4e0b\u9762\u7684\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd Kotlin \u7684\u5173\u952e\u4f18\u52bf\uff0c\u8bf4\u660e Kotlin DSL \u4e3a\u4ec0\u4e48\u662f\u7f16\u5199 Gradle \u6784\u5efa\u811a\u672c\u548c\u63d2\u4ef6\u7684\u66f4\u597d\u9009\u62e9\u3002<\/p>\n<p>\u5982\u679c\u60a8\u8fd8\u4e0d\u786e\u5b9a\u662f\u5426\u8981\u5c06 Android \u7684 Gradle \u6784\u5efa\u6587\u4ef6\u4ece Groovy \u8f6c\u6362\u4e3a Kotlin\uff0c\u4e0b\u9762\u662f\u4f7f\u7528 Kotlin \u7684 4 \u4e2a\u4e3b\u8981\u4f18\u52bf\u3002<\/p>\n<ol>\n<li><strong>\u7f16\u8bd1\u65f6\u68c0\u67e5<\/strong><\/li>\n<\/ol>\n<p>\u8fd9\u4e24\u79cd\u8bed\u8a00\u4e4b\u95f4\u7684\u4e00\u4e2a\u91cd\u8981\u533a\u522b\u662f\uff0cGroovy \u662f\u4e00\u79cd\u52a8\u6001\u8bed\u8a00\uff0c\u800c Kotlin \u662f\u9759\u6001\u7c7b\u578b\u3002 \u8fd9\u8868\u793a\uff0c\u4f7f\u7528 Kotlin DSL \u65f6\uff0c\u6784\u5efa\u811a\u672c\u9519\u8bef\u53ef\u80fd\u53d1\u751f\u5728\u7f16\u8bd1\u65f6\u800c\u4e0d\u662f\u8fd0\u884c\u65f6\u3002<\/p>\n<p>\u4f7f\u7528\u52a8\u6001\u7c7b\u578b\u68c0\u67e5\uff0c\u5728\u6267\u884c\u6784\u5efa\u811a\u672c\u4e4b\u524d\u4e0d\u4f1a\u68c0\u6d4b\u5230\u9519\u8bef\u3002 \u4f46\u662f\uff0c\u901a\u8fc7\u7f16\u8bd1\u65f6\u9519\u8bef\uff0c\u60a8\u6709\u673a\u4f1a\u66f4\u65e9\u5730\u4fee\u6b63\u95ee\u9898\u3002 \u53e6\u5916\uff0c\u5f53\u60a8\u5728 IDE \u4e2d\u5de5\u4f5c\u65f6\uff0c\u7f16\u8f91\u5668\u53ef\u4ee5\u9ad8\u4eae\u663e\u793a\u9519\u8bef\u3002 \u4f8b\u5982\uff0c\u5f15\u7528\u914d\u7f6e\u540d\u79f0 `applications` \u800c\u4e0d\u662f `application` \u65f6\uff0c\u60a8\u4f1a\u770b\u5230\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-341270\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/04\/Error.png\" alt=\"\u4f7f\u7528 Kotlin DSL \u65f6\uff0c\u6784\u5efa\u811a\u672c\u9519\u8bef\u53ef\u80fd\u53d1\u751f\u5728\u7f16\u8bd1\u65f6\" width=\"1304\" height=\"346\" \/><\/figure>\n<p>2. <strong>\u66f4\u597d\u7684 IDE \u4f53\u9a8c<\/strong><\/p>\n<p>\u6709\u4e86\u66f4\u591a\u5173\u4e8e\u4ee3\u7801\u7684\u8bed\u4e49\u4fe1\u606f\uff0c\u60a8\u53ef\u4ee5\u5145\u5206\u5229\u7528 IDE \u7684\u529f\u80fd\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u81ea\u52a8\u8865\u5168\u548c\u5185\u5bb9\u8f85\u52a9<\/li>\n<\/ul>\n<ul>\n<li>\u6e90\u4ee3\u7801\u5bfc\u822a<\/li>\n<\/ul>\n<ul>\n<li>\u91cd\u6784<\/li>\n<\/ul>\n<p>\u76ee\u524d\u4e3a\u6b62\uff0cIntelliJ IDEA \u548c Android Studio \u4e3a Kotlin \u63d0\u4f9b\u4e86\u96c6\u6210\u5ea6\u6700\u9ad8\u7684\u4f53\u9a8c\u3002 \u4f46\u662f\uff0c\u60a8\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6 IDE \u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u57fa\u4e8e Kotlin-DSL \u7684\u6784\u5efa\u3002 \u5728 Gradle \u6587\u6863\u4e2d\uff0c\u60a8\u53ef\u4ee5\u67e5\u770b <a href=\"https:\/\/docs.gradle.org\/current\/userguide\/kotlin_dsl.html#sec:ide_support\" target=\"_blank\" rel=\"noreferrer noopener\">Kotlin DSL \u7684 IDE \u652f\u6301\u77e9\u9635<\/a>\u3002<\/p>\n<p>3. <strong>\u7b80\u5316\u7684\u58f0\u660e\u5f0f\u63d2\u4ef6\u8bed\u6cd5<\/strong><\/p>\n<p>Kotlin DSL \u7684\u8bed\u6cd5\u80fd\u591f\u66ff\u4ee3\u4f20\u7edf Groovy DSL\uff0c\u5e76\u63d0\u4f9b\u4e0e\u6838\u5fc3 Kotlin \u8bed\u8a00\u76f8\u540c\u7684\u4f18\u52bf\u3002 \u6211\u4eec\u6765\u5bf9\u6bd4\u4e00\u4e0b\u8fd9\u4e24\u79cd\u8bed\u8a00\u3002<\/p>\n<p>\u5c06 Gradle \u63d2\u4ef6\u5e94\u7528\u5230\u9879\u76ee\u65f6\uff0c\u53ea\u9700\u6307\u5b9a\u63d2\u4ef6 ID\u3002 \u4f8b\u5982\uff0c\u4f7f\u7528 Groovy DSL \u6307\u5b9a `application` \u63d2\u4ef6\u65f6\uff0c\u5b83\u5c06\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-341281\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/04\/Groovy.png\" alt=\"\u4f7f\u7528 Groovy DSL \u6307\u5b9a\u63d2\u4ef6 ID\" width=\"1104\" height=\"126\" \/><\/figure>\n<p>\u5f97\u76ca\u4e8e Kotlin DSL \u7684\u7b80\u5316\u8bed\u6cd5\uff0c\u5728 Kotlin \u4e2d\u53ea\u9700\u8981\u8f93\u5165\u201capplication\u201d\uff1a<\/p>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-341292\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/04\/Screenshot-2023-03-14-at-3.32.44-PM.png\" alt=\"\u4f7f\u7528 Kotlin DSL \u6307\u5b9a\u63d2\u4ef6 ID\" width=\"1188\" height=\"264\" \/><\/figure>\n<p>\u5982\u4e0a\u6240\u8ff0\uff0c\u5982\u679c\u60a8\u5c1d\u8bd5\u5e94\u7528\u4e0d\u5b58\u5728\u7684\u63d2\u4ef6\uff0cIDE \u5c06\u663e\u793a\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<p>4. <strong>\u4e0d\u65ad\u58ee\u5927\u7684 Kotlin \u5f00\u53d1\u8005\u793e\u533a<\/strong><\/p>\n<p>Kotlin \u62e5\u6709\u5e9e\u5927\u800c\u7e41\u8363\u7684\u5f00\u53d1\u8005\u793e\u533a\uff0c\u793e\u533a\u6210\u5458\u4e3a\u8bed\u8a00\u548c\u5e93\u505a\u51fa\u8d21\u732e\u5e76\u63d0\u4f9b Gradle \u63d2\u4ef6\u3002<\/p>\n<p>\u4e0d\u65ad\u58ee\u5927\u7684\u793e\u533a\u4e3a\u7ecf\u9a8c\u4e30\u5bcc\u7684\u5f00\u53d1\u8005\u548c Kotlin \u65b0\u624b\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u4fe1\u606f\u3002 \u5176\u4e2d\u5305\u62ec Kotlin \u7684\u5b98\u65b9\u6587\u6863\u3001\u8bba\u575b\u3001\u535a\u5ba2\u3001\u793e\u4ea4\u5a92\u4f53\u8bdd\u9898\u548c\u5728\u7ebf\u6559\u80b2\u8bfe\u7a0b\u3002<\/p>\n<p>\u501f\u52a9\u5927\u91cf\u5b9e\u7528\u8d44\u6e90\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u5e76\u5c06\u6280\u80fd\u63d0\u5347\u5230\u66f4\u9ad8\u6c34\u5e73\u3002<\/p>\n<h2 class=\"wp-block-heading\">\u4ece Groovy \u8fc1\u79fb\u5230 Kotlin DSL<\/h2>\n<p>\u4ee5\u4e0a\u4f18\u52bf\u548c Kotlin DSL \u7684\u5f3a\u5927\u529f\u80fd\u4f7f\u5176\u6210\u4e3a\u63d0\u9ad8\u751f\u4ea7\u529b\u548c\u6784\u5efa\u9ad8\u6027\u80fd\u8f6f\u4ef6\u7684\u5b8c\u7f8e\u9009\u62e9\u3002 \u5982\u9700\u5c06\u73b0\u6709 Gradle \u6784\u5efa\u4ece Groovy \u8fc1\u79fb\u5230 Kotlin DSL\uff0c\u8bf7\u67e5\u770b <a href=\"https:\/\/docs.gradle.org\/current\/userguide\/migrating_from_groovy_to_kotlin_dsl.html#migrating_groovy_kotlin\" target=\"_blank\" rel=\"noreferrer noopener\">Gradle \u7684\u8fc1\u79fb\u6559\u7a0b<\/a>\u3002 Android \u5f00\u53d1\u8005\u53ef\u4ee5\u67e5\u770b<a href=\"https:\/\/developer.android.com\/studio\/build\/migrate-to-kts\" target=\"_blank\" rel=\"noreferrer noopener\">\u5c06 Android \u5e94\u7528\u7684 Gradle \u6784\u5efa\u6587\u4ef6\u4ece Groovy \u8f6c\u6362\u4e3a KTS<\/a> \u7684\u8be6\u5c3d\u6307\u5357\u3002<\/p>\n<p>Gradle \u4ecd\u5c06\u4fdd\u7559\u5bf9 Groovy DSL \u7684\u652f\u6301\u3002 \u5bf9\u4e8e\u4f7f\u7528 Groovy DSL \u7684\u5177\u6709\u590d\u6742\u6784\u5efa\u903b\u8f91\u7684\u5927\u578b\u9879\u76ee\uff0cGradle \u5efa\u8bae\u6682\u65f6\u4e0d\u8981\u8fc1\u79fb\u3002 \u4e0d\u8fc7\uff0c\u5728 Kotlin \u6210\u4e3a Gradle \u7684\u9ed8\u8ba4 DSL \u9009\u62e9\u540e\uff0c\u6211\u4eec\u76f8\u4fe1 Kotlin DSL \u5c06\u6781\u5927\u5730\u5e2e\u52a9\u5e0c\u671b\u5728\u7f16\u5199\u548c\u7f16\u8f91\u6784\u5efa\u811a\u672c\u65f6\u63d0\u5347\u4f53\u9a8c\u7684 Gradle \u7528\u6237\u3002<\/p>\n<p>\u540c\u6837\uff0cJetBrains \u56e2\u961f\u8fd8\u5c06\u81f4\u529b\u4e8e\u8fdb\u4e00\u6b65\u589e\u5f3a\u6211\u4eec\u7684\u6280\u672f\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u529f\u80fd\u4e30\u5bcc\u7684\u5de5\u5177\u6765\u5f00\u53d1\u9ad8\u6548\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u00a0<\/p>\n<p>\u672c\u535a\u6587\u82f1\u6587\u539f\u4f5c\u8005\uff1a<\/p>\n\n    <div class=\"about-author \">\n        <div class=\"about-author__box\">\n            <div class=\"row\">\n                <div class=\"about-author__box-img\">\n                    <img decoding=\"async\" src=\"https:\/\/secure.gravatar.com\/avatar\/?s=200&#038;r=g\" width=\"200\" height=\"200\" alt=\"\" loading=\"lazy\"  class=\"avatar avatar-200 wp-user-avatar wp-user-avatar-200 photo avatar-default\">\n                <\/div>\n                <div class=\"about-author__box-text\">\n                                                        <\/div>\n            <\/div>\n        <\/div>\n    <\/div>\n","protected":false},"author":1297,"featured_media":341879,"comment_status":"closed","ping_status":"closed","template":"","categories":[89],"tags":[1625,1738],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/347053"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/kotlin"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/1297"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=347053"}],"version-history":[{"count":4,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/347053\/revisions"}],"predecessor-version":[{"id":347326,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/347053\/revisions\/347326"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media\/341879"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=347053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=347053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=347053"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=347053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}