{"id":743037,"date":"2026-09-28T14:20:58","date_gmt":"2026-09-28T13:20:58","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=kotlin&#038;p=743037"},"modified":"2026-09-28T14:21:02","modified_gmt":"2026-09-28T13:21:02","slug":"a-more-reliable-compilation-scheme-for-kotlin-multiplatform-modules","status":"publish","type":"kotlin","link":"https:\/\/blog.jetbrains.com\/zh-hans\/kotlin\/2026\/09\/a-more-reliable-compilation-scheme-for-kotlin-multiplatform-modules\/","title":{"rendered":"Kotlin Multiplatform \u6a21\u5757\uff1a\u66f4\u53ef\u9760\u7684\u7f16\u8bd1\u65b9\u6848"},"content":{"rendered":"\n<p>Kotlin Multiplatform \u9879\u76ee\u5f53\u524d\u7684\u7f16\u8bd1\u65b9\u5f0f\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u6709\u65f6\u4f1a\u51fa\u73b0\u610f\u5916\u6216\u96be\u4ee5\u9884\u6d4b\u7684\u884c\u4e3a\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u91cd\u8f7d\u9009\u62e9\u3001\u7c7b\u578b\u63a8\u65ad\uff0c\u751a\u81f3\u4ee3\u7801\u80fd\u5426\u901a\u8fc7\u7f16\u8bd1\u7b49\u95ee\u9898\u4e0a\uff0cIDE \u5206\u6790\u4e0e\u7f16\u8bd1\u5668\u7684\u7ed3\u679c\u53ef\u80fd\u4e0d\u4e00\u81f4\uff0c\u800c\u4e14 IDE \u7684\u68c0\u67e5<strong>\u66f4\u4e25\u683c<\/strong>\u3002<\/li>\n\n\n\n<li><code>commonTest<\/code> \u4e2d\u7684\u4ee3\u7801\u53ef\u80fd\u610f\u5916\u8c03\u7528\u5e73\u53f0\u6e90\u96c6\u4e2d\u7684\u58f0\u660e\uff0c\u6253\u7834\u4f60\u7684\u9884\u671f\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5728 Kotlin 2.5.0-Beta1 \u4e2d\uff0c\u6211\u4eec\u4e3a KMP \u5f15\u5165\u4e86\u53ef\u9009\u7684\u201c\u72ec\u7acb\u7f16\u8bd1\u201d\uff08separate compilation\uff09\u65b9\u6848\uff0c\u4ee5\u89e3\u51b3\u8fd9\u4e24\u4e2a\u95ee\u9898\uff1a\u8ba9\u7f16\u8bd1\u7ed3\u679c\u4e0e IDE \u5206\u6790\u4fdd\u6301\u4e00\u81f4\uff0c\u5e76\u66f4\u4e00\u81f4\u5730\u6307\u51fa\u516c\u5171\u6e90\u96c6\u4e2d\u5b58\u5728\u95ee\u9898\u7684\u5e93\u4ee3\u7801\u8c03\u7528\u3002\u6b64\u5916\uff0c\u8fd9\u4e00\u65b9\u6848\u8fd8\u8ba9\u6211\u4eec\u80fd\u591f\u4e3a\u516c\u5171\u6e90\u96c6\u5b9e\u73b0<a href=\"https:\/\/kotlinlang.org\/docs\/gradle-compilation-and-caches.html#incremental-compilation\" target=\"_blank\" rel=\"noopener\">\u589e\u91cf\u7f16\u8bd1<\/a>\u3002<\/p>\n\n\n\n<p>\u72ec\u7acb\u7f16\u8bd1\u76ee\u524d\u662f\u4e00\u9879<a href=\"https:\/\/kotlinlang.org\/docs\/components-stability.html#stability-levels-explained\" target=\"_blank\" rel=\"noopener\">\u5b9e\u9a8c\u6027<\/a>\u529f\u80fd\uff0c\u9ed8\u8ba4\u7981\u7528\u3002\u8981\u542f\u7528\u6b64\u529f\u80fd\uff0c\u8bf7\u5728 <code>gradle.properties<\/code> \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u7f16\u8bd1\u5668\u9009\u9879\uff08\u8bf7\u7559\u610f<a href=\"#known-issues\">\u5df2\u77e5\u95ee\u9898<\/a>\uff09\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">kotlin.kmp.separateCompilation=true<\/pre>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u8fd9\u4e9b\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u5757\u5185\u5982\u4f55\u89e3\u6790\u591a\u5e73\u53f0\u58f0\u660e<\/h2>\n\n\n\n<p>\u5728\u540c\u4e00\u6a21\u5757\u5185\uff0c\u591a\u5e73\u53f0\u4ee3\u7801\u7684\u884c\u4e3a\u7b26\u5408\u9884\u671f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ jvmMain\nfun foo() {}\n\n\/\/ commonMain\nfun test() {\n    foo() \/\/ Unresolved reference in IDE and during compilation\n}<\/pre>\n\n\n\n<p>\u7f16\u8bd1\u5668\u4e0e IDE \u7684\u5224\u65ad\u4e00\u81f4\uff1a<code>commonMain<\/code> \u4e2d\u7684\u4ee3\u7801\u4e5f\u4f1a\u7f16\u8bd1\u5230\u5176\u4ed6\u5e73\u53f0\uff0c\u4f8b\u5982 Kotlin\/JS\uff0c\u800c <code>jsMain<\/code> \u4e2d\u53ef\u80fd\u5e76\u6ca1\u6709 <code>fun foo()<\/code> \u58f0\u660e\u3002<a href=\"https:\/\/kotlinlang.org\/docs\/multiplatform\/multiplatform-expect-actual.html\" target=\"_blank\" rel=\"noopener\">expect\/actual \u58f0\u660e<\/a>\u6b63\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u800c\u8bbe\u8ba1\u7684\uff1a\u5c06\u5e73\u53f0\u7279\u6709\u58f0\u660e\u4e0e\u516c\u5171\u58f0\u660e\u663e\u5f0f\u5173\u8054\u8d77\u6765\u3002<\/p>\n\n\n\n<p>\u5982\u679c <code>foo()<\/code> \u51fd\u6570\u58f0\u660e\u5728\u4f9d\u8d56\u9879\u4e2d\uff08\u540c\u4e00\u9879\u76ee\u4e2d\u7684\u53e6\u4e00\u4e2a\u6a21\u5757\uff0c\u6216 Kotlin \u6807\u51c6\u5e93\u3001<code>kotlinx-coroutines<\/code> \u7b49\u4e8c\u8fdb\u5236\u5236\u54c1\uff09\uff0c\u4f60\u53ef\u80fd\u4e5f\u4f1a\u8ba4\u4e3a\u8fd9\u4e2a\u5f15\u7528\u5e94\u88ab\u6807\u8bb0\u4e3a\u65e0\u6cd5\u89e3\u6790\u3002\u9057\u61be\u7684\u662f\uff0c\u5728\u5f53\u524d\u7f16\u8bd1\u65b9\u6848\u4e0b\uff0cIDE \u4e0e\u7f16\u8bd1\u5668\u6070\u6070\u4f1a\u5728\u8fd9\u91cc\u4ea7\u751f\u5206\u6b67\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u5757\u4e4b\u95f4\u5982\u4f55\u89e3\u6790\u591a\u5e73\u53f0\u58f0\u660e<\/h2>\n\n\n\n<p>\u8981\u7406\u89e3\u95ee\u9898\u7684\u6839\u6e90\uff0c\u6211\u4eec\u5148\u4ed4\u7ec6\u770b\u770b\u5f53\u524d\u7684\u7f16\u8bd1\u914d\u7f6e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Kotlin Multiplatform \u7684\u7f16\u8bd1\u914d\u7f6e<\/h3>\n\n\n\n<p>\u5728 KMP \u9879\u76ee\u4e2d\uff0c\u5305\u542b\u516c\u5171\u4ee3\u7801\u7684\u6a21\u5757\u901a\u5e38\u7531\u591a\u4e2a\u6e90\u96c6\u7ec4\u6210\uff1a\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5305\u62ec\u4e00\u4e2a\u5171\u4eab\u6e90\u96c6\uff08<code>commonMain<\/code>\uff09\uff0c\u4ee5\u53ca\u4e3a\u5df2\u58f0\u660e\u76ee\u6807\u914d\u7f6e\u7684\u5e73\u53f0\u6e90\u96c6\uff08Kotlin\/JVM \u5bf9\u5e94 <code>jvmMain<\/code>\uff09\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u79cd\u914d\u7f6e\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u751f\u6210\u4ee5\u4e0b\u5236\u54c1\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e3a <code>jvmMain<\/code> \u8fd9\u6837\u7684\u6bcf\u4e2a\u5e73\u53f0\u6e90\u96c6\u751f\u6210\u5e73\u53f0\u5236\u54c1\uff1aJVM \u4e0a\u4e3a <code>*.jar<\/code>\uff0c\u5176\u4ed6\u5e73\u53f0\u4e0a\u4e3a <code>*.klib<\/code>\u3002<\/li>\n\n\n\n<li>\u4e3a\u6bcf\u4e2a\u516c\u5171\u6e90\u96c6\u6216\u4e2d\u95f4\u6e90\u96c6\uff08\u4f8b\u5982 <code>commonMain<\/code> \u6216 <code>nativeMain<\/code>\uff09\u751f\u6210\u5143\u6570\u636e KLIB\u3002\u5143\u6570\u636e KLIB \u5305\u542b\u88ab\u7f16\u8bd1\u6e90\u96c6\u4e2d\u7684\u6240\u6709\u58f0\u660e\uff0c\u4f46\u4e0d\u5305\u542b\u5b9e\u73b0\u4f53\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u90a3\u4e48\uff0c\u95ee\u9898\u51fa\u5728\u54ea\u91cc\uff1f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u516c\u5171\u4ee3\u7801\u4f9d\u636e\u5e73\u53f0\u5236\u54c1\u7f16\u8bd1\uff0c\u4e0e IDE \u7684\u5224\u65ad\u4e0d\u4e00\u81f4<\/h3>\n\n\n\n<p>\u7f16\u8bd1\u5e73\u53f0\u6e90\u96c6\u65f6\uff0c\u8be5\u6e90\u96c6\uff08<code>jvmMain<\/code>\uff09\u4e2d\u7684\u4ee3\u7801<strong>\u4ee5\u53ca<\/strong>\u6240\u6709\u76f8\u5173\u5171\u4eab\u6e90\u96c6\uff08<code>commonMain<\/code> \u548c\u5176\u4ed6<a href=\"https:\/\/kotlinlang.org\/docs\/multiplatform\/multiplatform-hierarchy.html#default-hierarchy-template\" target=\"_blank\" rel=\"noopener\">\u4e2d\u95f4\u6e90\u96c6<\/a>\uff09\u4e2d\u7684\u4ee3\u7801\uff0c\u90fd\u4f1a\u4f9d\u636e\u4f9d\u8d56\u9879\u7684\u5e73\u53f0\u5236\u54c1\u8fdb\u884c\u7f16\u8bd1\u3002\u6b64\u65f6\uff0c<code>commonMain<\/code> \u4e2d\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u9690\u5f0f\u89e3\u6790\u5230 <code>jvmMain<\/code> \u4e2d\u7684\u58f0\u660e\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8fd9\u6b63\u662f\u4f60\u9884\u671f\u7684\u884c\u4e3a\uff0c\u901a\u5e38\u4e0d\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\u3002\u4e0d\u8fc7\uff0c\u5982\u679c <code>commonMain<\/code> \u53ea\u80fd\u8c03\u7528\u4f9d\u8d56\u9879\u7684 <code>commonMain<\/code> \u4e2d\u7684\u58f0\u660e\uff08\u5373\u4f9d\u8d56\u9879\u5143\u6570\u636e KLIB \u4e2d\u5217\u51fa\u7684\u58f0\u660e\uff09\uff0c\u884c\u4e3a\u5c31\u4f1a\u66f4\u52a0\u900f\u660e\u3001\u53ef\u9884\u6d4b\u3002<\/p>\n\n\n\n<p>IntelliJ IDEA \u7684\u4ee3\u7801\u5206\u6790\u76ee\u524d\u6b63\u662f\u57fa\u4e8e\u8fd9\u4e00\u5047\u8bbe\u3002\u7531\u4e8e KLIB \u5143\u6570\u636e\u4e0d\u5305\u542b <code>jvmMain<\/code> \u4e2d\u7684\u58f0\u660e\uff0c\u5f53 <code>commonMain<\/code> \u5f15\u7528\u516c\u5171\u4ee3\u7801\u4e2d\u672a\u663e\u5f0f\u58f0\u660e\u7684\u5185\u5bb9\u65f6\uff0cIntelliJ IDEA \u4f1a\u62a5\u544a\u65e0\u6cd5\u89e3\u6790\u5f15\u7528\u7684\u9519\u8bef\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ lib\/jvmMain\nclass Foo\n\n\/\/ app\/commonMain\nfun main() {\n    Foo() \/\/ Unresolved reference in the IDE, no error during compilation\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1272\" height=\"394\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2026\/09\/Current-compilation-with-IDEA-1.jpg\" alt=\"\" class=\"wp-image-742856\"\/><\/figure>\n\n\n\n<p>\u540c\u6837\u7684\u673a\u5236\u4e5f\u9002\u7528\u4e8e <code>commonTest<\/code> \u6e90\u96c6\uff0c\u56e0\u4e3a\u6d4b\u8bd5\u4e5f\u201c\u4f9d\u8d56\u201d\u4e3b\u4ee3\u7801\uff1a\u5f53 <code>commonTest<\/code> \u4f9d\u636e <code>jvmMain<\/code> \u7f16\u8bd1\u65f6\uff0c\u5b83\u53ef\u4ee5\u6210\u529f\u8c03\u7528\u5e73\u53f0\u4ee3\u7801\u4e2d\u7684\u58f0\u660e\uff0c\u800c\u4e0d\u53ea\u9650\u4e8e <code>commonMain<\/code> \u4e2d\u7684\u58f0\u660e\u3002IDE \u5219\u4f1a\u62a5\u544a\u540c\u6837\u7684\u65e0\u6cd5\u89e3\u6790\u5f15\u7528\u7684\u95ee\u9898\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ app\/jvmMain\nclass Foo\n\n\/\/ app\/commonTest\nfun main() {\n    Foo() \/\/ &quot;Unresolved reference&quot; in the IDE, no error during compilation\n}<\/pre>\n\n\n\n<p>\u4e0b\u9762\u770b\u770b\u72ec\u7acb\u7f16\u8bd1\u5982\u4f55\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u72ec\u7acb\u7f16\u8bd1\u5982\u4f55\u89e3\u51b3\u95ee\u9898<\/h2>\n\n\n\n<p>KMP \u72ec\u7acb\u7f16\u8bd1\u8ba9\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1 KMP \u9879\u76ee\u7684\u516c\u5171\u6e90\u96c6\u65f6\u6267\u884c\u66f4\u4e25\u683c\u7684\u68c0\u67e5\uff0c\u4e0e IDE \u7684\u9884\u671f\u4fdd\u6301\u4e00\u81f4\u3002\u8fd9\u8ba9\u6574\u4f53\u884c\u4e3a\u66f4\u53ef\u9884\u6d4b\uff0c\u4e0d\u8fc7\u4f60\u53ef\u80fd\u9700\u8981\u8c03\u6574\u73b0\u6709\u4ee3\u7801\uff0c\u4ee5\u6ee1\u8db3\u66f4\u4e25\u683c\u7684\u7f16\u8bd1\u65f6\u68c0\u67e5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1272\" height=\"394\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2026\/09\/Separate-compilation-1.jpg\" alt=\"\" class=\"wp-image-742867\"\/><\/figure>\n\n\n\n<p>\u6211\u4eec\u901a\u8fc7\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\uff0c\u770b\u770b\u5207\u6362\u7f16\u8bd1\u65b9\u6848\u540e\u4f1a\u53d1\u751f\u54ea\u4e9b\u53d8\u5316\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">IDE \u4e2d\u4ee3\u7801\u6807\u7ea2\uff0c\u7f16\u8bd1\u5374\u4e0d\u62a5\u9519<\/h3>\n\n\n\n<p>\u542f\u7528\u72ec\u7acb\u7f16\u8bd1\u540e\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u65e0\u6cd5\u901a\u8fc7\u7f16\u8bd1\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u73b0\u5728\u4f1a\u4e25\u683c\u4f9d\u636e\u5143\u6570\u636e KLIB \u4e2d\u7684\u58f0\u660e\u6765\u89e3\u6790\u516c\u5171\u4ee3\u7801\u4e2d\u7684\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ lib\/jvmMain\nclass Foo\n\n\/\/ app\/commonMain\nfun main() {\n    Foo() \/\/ Unresolved reference in IDE, now also a compilation error\n}<\/pre>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6cd5\u662f\u5efa\u7acb\u663e\u5f0f\u5173\u8054\uff1a\u5728 <code>lib\/commonMain<\/code> \u4e2d\u58f0\u660e\u4e00\u4e2a <code>expect class<\/code>\uff0c\u5e76\u5c06\u5e73\u53f0\u4e0a\u7684 <code>Foo<\/code> \u7c7b\u6539\u4e3a <code>actual class<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ lib\/commonMain\nexpect class Foo()\n\n\/\/ lib\/jvmMain\nactual class Foo\n\n\/\/ app\/commonMain\nfun main() {\n    Foo() \/\/ ok\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1272\" height=\"394\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2026\/09\/Fixed-version-if-your-code-is-broken-by-separate-compilation.jpg\" alt=\"\" class=\"wp-image-742878\"\/><\/figure>\n\n\n\n<p><code>commonTest<\/code> \u4e5f\u662f\u5982\u6b64\uff1a\u542f\u7528\u72ec\u7acb\u7f16\u8bd1\u540e\uff0c\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u7684\u8c03\u7528\u5c06\u4e0d\u4f1a\u89e3\u6790\u5230 <code>jvmMain<\/code> \u4e2d\u7684\u5e73\u53f0\u58f0\u660e\u3002\u5982\u679c\u4f60\u5e0c\u671b\u6d4b\u8bd5\u7684\u6b63\u662f\u8fd9\u4e9b\u5e73\u53f0\u4ee3\u7801\uff0c\u8bf7\u5728 <code>commonMain<\/code> \u4e2d\u6dfb\u52a0\u76f8\u5e94\u7684 <code>expect<\/code> \u58f0\u660e\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ app\/jvmMain\nactual class Foo\n\n\/\/ app\/commonMain\nexpect class Foo\n\n\/\/ app\/commonTest\nfun main() {\n    Foo() \/\/ Ok, calls Foo from jvmMain\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">IDE \u4e0e\u7f16\u8bd1\u5668\u9009\u62e9\u4e86\u4e0d\u540c\u7684\u91cd\u8f7d<\/h3>\n\n\n\n<p>\u5f53 <code>lib\/commonMain<\/code> \u548c <code>lib\/jvmMain<\/code> \u4e2d\u58f0\u660e\u4e86\u591a\u4e2a\u91cd\u8f7d\u65f6\uff0cIDE \u76ee\u524d\u5bf9 <code>app\/commonMain<\/code> \u4e2d\u8c03\u7528\u7684\u89e3\u6790\u4e0e\u7f16\u8bd1\u5668\u4e0d\u540c\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ lib\/commonMain\nfun foo(x: Any) = &quot;common&quot;\n\n\/\/ lib\/jvmMain\nfun foo(x: String) = &quot;platform&quot;\n\n\/\/ app\/commonMain\nfun main() {\n    \/\/ &quot;Go to declaration&quot; on foo() jumps to lib\/commonMain,\n    \/\/ but at runtime &quot;platform&quot; is printed\n    println(foo(&quot;&quot;))\n}<\/pre>\n\n\n\n<p>IDE \u5047\u5b9a\u516c\u5171\u4ee3\u7801\u53ea\u80fd\u4f9d\u8d56\u516c\u5171\u58f0\u660e\uff0c\u56e0\u6b64\u4f1a\u5c06 <code>foo(\"\")<\/code> \u8c03\u7528\u89e3\u6790\u5230 <code>lib\/commonMain<\/code> \u4e2d\u7684 <code>fun foo(x: Any)<\/code> \u58f0\u660e\u3002\u4f46\u5728\u5f53\u524d\u7684\u5b9e\u9645\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\uff0c<code>app\/commonMain<\/code> \u53ef\u4ee5\u770b\u5230\u4e24\u4e2a\u58f0\u660e\uff0c\u5e76\u9009\u62e9\u66f4\u5177\u4f53\u7684 <code>fun foo(x: String)<\/code> \u91cd\u8f7d\u3002\u8fd0\u884c\u65f6\u4f1a\u8f93\u51fa\u201cplatform\u201d\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u65b0\u7684\u7f16\u8bd1\u65b9\u6848\u540e\uff0c\u8be5\u8c03\u7528\u4f1a\u89e3\u6790\u5230 <code>lib\/commonMain<\/code> \u4e2d\u7684 <code>fun foo(x: Any)<\/code>\uff0c\u8fd0\u884c\u65f6\u4f1a\u8f93\u51fa\u201ccommon\u201d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u610f\u5916\u7684\u7c7b\u578b\u63a8\u65ad\u7ed3\u679c<\/h3>\n\n\n\n<p>\u8fd8\u6709\u4e00\u79cd\u66f4\u68d8\u624b\u7684\u60c5\u51b5\uff1a\u9519\u8bef\u4e0d\u662f\u5728 <code>app\/commonMain<\/code> \u7684\u8c03\u7528\u4f4d\u7f6e\u89e6\u53d1\uff0c\u800c\u662f\u5728\u5e73\u53f0\u4ee3\u7801\u4e2d\u89e6\u53d1\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0cIDE \u5e76\u672a\u53d1\u73b0\u95ee\u9898\uff0c\u4f46 JVM \u7f16\u8bd1\u5374\u4f1a\u5931\u8d25\u3002<\/p>\n\n\n\n<p><code>actual<\/code> \u7c7b\u53ef\u4ee5\u5177\u6709\u4e0e\u5bf9\u5e94 <code>expect<\/code> \u7c7b\u4e0d\u540c\u7684\u8d85\u7c7b\u578b\uff0c\u56e0\u6b64\u51fa\u73b0\u4ee5\u4e0b\u4ee3\u7801\u7ed3\u6784\u662f\u5408\u7406\u7684\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ eventsLib\/commonMain\ninterface Event { val name: String }\n\nexpect class ClickEvent(target: String) : Event { override val name: String }\nexpect class ScrollEvent(offset: Int) : Event { override val name: String }\n\n\/\/ eventsLib\/jvmMain \u2014 serialized events get queued or stored in a session\nactual class ClickEvent actual constructor(target: String) : Event, Serializable { \/* ... *\/ }\nactual class ScrollEvent actual constructor(offset: Int) : Event, Serializable { \/* ... *\/ }\n\n\/\/ app\/commonMain\nfun currentEvent() = if (clicked()) ClickEvent(&quot;buy&quot;) else ScrollEvent(120)\n\nfun report() = currentEvent().name \/\/ e: Unresolved reference &#039;name&#039; only on the JVM<\/pre>\n\n\n\n<p>IDE \u53ea\u5339\u914d <code>commonMain<\/code> \u4e2d\u7684\u58f0\u660e\uff0c\u56e0\u6b64\u80fd\u591f\u6b63\u786e\u5730\u5c06 <code>currentEvent()<\/code> \u7684\u8fd4\u56de\u7c7b\u578b\u63a8\u65ad\u4e3a <code>Event<\/code>\uff0c\u5e76\u89e3\u6790 <code>.name<\/code> \u5f15\u7528\u3002<\/p>\n\n\n\n<p>\u7136\u800c\u5728 JVM \u4e0a\uff0c\u8be5\u5e93\u4e3a\u6bcf\u4e2a <code>actual<\/code> \u7c7b\u989d\u5916\u58f0\u660e\u4e86\u4e00\u4e2a\u8d85\u7c7b\u578b\u3002\u5f53\u524d\uff0c\u4e3a JVM \u7f16\u8bd1 <code>app<\/code> \u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u4f9d\u636e <code>eventsLib<\/code> \u7684 JAR \u89e3\u6790\u516c\u5171\u4ee3\u7801\uff0c\u5176\u4e2d\u5305\u542b\u7684\u662f\u8fd9\u4e9b\u7c7b\u7684 <code>eventsLib\/jvmMain<\/code> \u7248\u672c\u3002\u7531\u4e8e\u5b58\u5728\u4e24\u4e2a\u8d85\u7c7b\u578b\uff0c<code>currentEvent()<\/code> \u8c03\u7528\u7684\u8fd4\u56de\u7c7b\u578b\u4f1a\u88ab\u63a8\u65ad\u4e3a <code>Any<\/code>\uff0c\u800c\u5b83\u6ca1\u6709 <code>.name<\/code>\uff0c\u56e0\u6b64 JVM \u7f16\u8bd1\u5931\u8d25\u3002<\/p>\n\n\n\n<p>\u542f\u7528\u72ec\u7acb\u7f16\u8bd1\u540e\uff0c<code>app\/commonMain<\/code> \u4e2d\u7684\u4ee3\u7801\u53ea\u4f1a\u4f9d\u636e <code>eventsLib\/commonMain<\/code> \u4e2d\u7684\u58f0\u660e\u8fdb\u884c\u89e3\u6790\u3002\u56e0\u6b64\uff0c\u5728\u7f16\u8bd1 <code>app<\/code> \u7684 JVM \u76ee\u6807\u65f6\uff0c\u7f16\u8bd1\u5668\u5df2\u7ecf\u5b8c\u6210\u4e86\u516c\u5171\u4ee3\u7801\u7684\u89e3\u6790\uff0c\u5e76\u5c06 <code>currentEvent()<\/code> \u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u63a8\u65ad\u4e3a <code>Event<\/code>\u3002JVM \u7f16\u8bd1\u5355\u72ec\u8fdb\u884c\uff0c\u65e0\u9700\u518d\u505a\u7c7b\u578b\u63a8\u65ad\uff0c\u56e0\u800c\u80fd\u591f\u987a\u5229\u5b8c\u6210\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u542f\u7528\u72ec\u7acb\u7f16\u8bd1<\/h2>\n\n\n\n<p>\u72ec\u7acb\u7f16\u8bd1\u76ee\u524d\u662f\u4e00\u9879<a href=\"https:\/\/kotlinlang.org\/docs\/components-stability.html#stability-levels-explained\" target=\"_blank\" rel=\"noopener\">\u5b9e\u9a8c\u6027<\/a>\u529f\u80fd\uff0c\u9ed8\u8ba4\u7981\u7528\u3002\u8981\u542f\u7528\u6b64\u529f\u80fd\uff0c\u8bf7\u5728 <code>gradle.properties<\/code> \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u7f16\u8bd1\u5668\u9009\u9879\uff08\u8bf7\u7559\u610f\u4e0b\u6587\u4e2d\u7684\u5df2\u77e5\u95ee\u9898\uff09\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">kotlin.kmp.separateCompilation=true<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"known-issues\">\u5df2\u77e5\u95ee\u9898<\/h2>\n\n\n\n<p>\u8981\u8ba9\u72ec\u7acb\u7f16\u8bd1\u5728 KMP \u9879\u76ee\u4e2d\u6b63\u5e38\u5de5\u4f5c\uff0c\u591a\u5e73\u53f0\u5e93\u4f5c\u8005\u5fc5\u987b\u53d1\u5e03\u5143\u6570\u636e KLIB\u3002\u867d\u7136\u8fd9\u5df2\u7ecf\u662f KMP Gradle \u63d2\u4ef6\u53d1\u5e03\u4efb\u52a1\u7684\u9ed8\u8ba4\u884c\u4e3a\uff0c\u4f46\u5728\u65b0\u7684\u7f16\u8bd1\u65b9\u6848\u4e0b\uff0c\u5143\u6570\u636e KLIB \u6210\u4e3a\u4e86\u5fc5\u4e0d\u53ef\u5c11\u7684\u90e8\u5206\u3002<\/p>\n\n\n\n<p>KMP \u72ec\u7acb\u7f16\u8bd1\u4ecd\u5b58\u5728\u4e00\u4e9b\u5df2\u77e5\u95ee\u9898\uff0c\u6211\u4eec\u6b63\u5728\u4fee\u590d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>cinterop \u516c\u5171\u5316\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\uff08\u4f8b\u5982 <a href=\"https:\/\/youtrack.jetbrains.com\/issue\/KT-88178\" target=\"_blank\" rel=\"noopener\">KT-88178<\/a> \u6216 <a href=\"https:\/\/youtrack.jetbrains.com\/issue\/KT-41509\" target=\"_blank\" rel=\"noopener\">KT-41509<\/a>\uff09\u3002\u8fd9\u4e9b\u95ee\u9898\u901a\u5e38\u4e0d\u4f1a\u5f71\u54cd\u4e00\u822c\u7684 KMP \u9879\u76ee\uff0c\u4f46\u5982\u679c\u4f60\u4f7f\u7528\u4e86 <a href=\"https:\/\/kotlinlang.org\/docs\/native-c-interop.html\" target=\"_blank\" rel=\"noopener\">C \u4e92\u64cd\u4f5c<\/a>\uff0c\u8bf7\u8c28\u614e\u542f\u7528\u72ec\u7acb\u7f16\u8bd1\u6a21\u5f0f\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u5305\u542b\u516c\u5171\u6e90\u96c6\u3001\u4f46\u53ea\u58f0\u660e\u4e86\u4e00\u4e2a\u76ee\u6807\u7684 KMP \u6a21\u5757\uff0c\u72ec\u7acb\u7f16\u8bd1\u76ee\u524d<strong>\u4e0d\u4f1a<\/strong>\u4ea7\u751f\u5f71\u54cd\u3002<\/li>\n\n\n\n<li>\u8fd8\u6709\u4e00\u4e9b\u8f83\u5c0f\u7684\u95ee\u9898\uff0c\u4f8b\u5982 <a href=\"https:\/\/youtrack.jetbrains.com\/issue\/KT-88148\" target=\"_blank\" rel=\"noopener\">KT-88148<\/a>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u672c\u535a\u6587\u82f1\u6587\u539f\u4f5c\u8005\uff1a<a href=\"https:\/\/blog.jetbrains.com\/author\/aleksey-zamulla-jetbrains-com\">Aleksey Zamulla<\/a><\/p>\n\n\n\n<p>\u672c\u6587\u7531 AI \u8f85\u52a9\u7ffb\u8bd1\uff0c\u5e76\u7ecf\u4eba\u5de5\u5ba1\u6821\u3002\u5982\u53d1\u73b0\u4efb\u4f55\u95ee\u9898\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u6307\u6b63\u3002<\/p>\n","protected":false},"author":1297,"featured_media":743182,"comment_status":"closed","ping_status":"closed","template":"","categories":[],"tags":[811,1563,6436],"cross-post-tag":[],"class_list":["post-743037","kotlin","type-kotlin","status-publish","has-post-thumbnail","hentry","tag-compiler","tag-experimental-features","tag-kotlin-multiplatform"],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/743037","targetHints":{"allow":["GET"]}}],"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=743037"}],"version-history":[{"count":3,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/743037\/revisions"}],"predecessor-version":[{"id":743198,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/743037\/revisions\/743198"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media\/743182"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=743037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=743037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=743037"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=743037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}