import{_ as l,r as o,o as p,c as i,b as s,d as a,a as c,e}from"./app--CGKFO0X.js";const r={},t=e('<h1 id="hikage-compiler" tabindex="-1"><a class="header-anchor" href="#hikage-compiler" aria-hidden="true">#</a> hikage-compiler</h1><p><img src="https://img.shields.io/maven-central/v/com.highcapable.hikage/hikage-compiler?logo=apachemaven&amp;logoColor=orange&amp;style=flat-square" alt="Maven Central"><span style="margin-left:5px;"></span><img src="https://img.shields.io/maven-metadata/v?metadataUrl=https%3A%2F%2Fraw.githubusercontent.com%2FHighCapable%2Fmaven-repository%2Frefs%2Fheads%2Fmain%2Frepository%2Freleases%2Fcom%2Fhighcapable%2Fhikage%2Fhikage-compiler%2Fmaven-metadata.xml&amp;logo=apachemaven&amp;logoColor=orange&amp;label=highcapable-maven-releases&amp;style=flat-square" alt="Maven metadata URL"><span style="margin-left:5px;"></span><img src="https://img.shields.io/badge/Min SDK-21-orange?logo=android&amp;style=flat-square" alt="Android Min SDK"></p><p>这是 Hikage 的自动化编译模块。</p><h2 id="配置依赖" tabindex="-1"><a class="header-anchor" href="#配置依赖" aria-hidden="true">#</a> 配置依赖</h2><p>你可以使用如下方式将此模块添加到你的项目中。</p>',5),d={class:"custom-container warning"},u=s("p",{class:"custom-container-title"},"注意",-1),v={href:"https://github.com/google/ksp/releases",target:"_blank",rel:"noopener noreferrer"},m=e(`<h3 id="version-catalog-推荐" tabindex="-1"><a class="header-anchor" href="#version-catalog-推荐" aria-hidden="true">#</a> Version Catalog (推荐)</h3><p>在你的项目 <code>gradle/libs.versions.toml</code> 中添加依赖。</p><div class="language-toml line-numbers-mode" data-ext="toml"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#ADBAC7;">[</span><span style="color:#F69D50;">versions</span><span style="color:#ADBAC7;">]</span></span>
<span class="line"><span style="color:#ADBAC7;">ksp = </span><span style="color:#96D0FF;">&quot;&lt;ksp-version&gt;&quot;</span></span>
<span class="line"><span style="color:#ADBAC7;">hikage-compiler = </span><span style="color:#96D0FF;">&quot;&lt;version&gt;&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">[</span><span style="color:#F69D50;">plugins</span><span style="color:#ADBAC7;">]</span></span>
<span class="line"><span style="color:#ADBAC7;">ksp = { id = </span><span style="color:#96D0FF;">&quot;com.google.devtools.ksp&quot;</span><span style="color:#ADBAC7;">, version.ref = </span><span style="color:#96D0FF;">&quot;ksp&quot;</span><span style="color:#ADBAC7;"> }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#ADBAC7;">[</span><span style="color:#F69D50;">libraries</span><span style="color:#ADBAC7;">]</span></span>
<span class="line"><span style="color:#ADBAC7;">hikage-compiler = { module = </span><span style="color:#96D0FF;">&quot;com.highcapable.hikage:hikage-compiler&quot;</span><span style="color:#ADBAC7;">, version.ref = </span><span style="color:#96D0FF;">&quot;hikage-compiler&quot;</span><span style="color:#ADBAC7;"> }</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在你的根项目 <code>build.gradle.kts</code> 中配置依赖。</p><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#DCBDFB;">plugins</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#DCBDFB;">alias</span><span style="color:#ADBAC7;">(libs.plugins.ksp) apply </span><span style="color:#6CB6FF;">false</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在你的项目 <code>build.gradle.kts</code> 中配置依赖。</p><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#DCBDFB;">plugins</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#DCBDFB;">alias</span><span style="color:#ADBAC7;">(libs.plugins.ksp)</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#DCBDFB;">dependencies</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#DCBDFB;">ksp</span><span style="color:#ADBAC7;">(libs.hikage.compiler)</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请将 <code>&lt;version&gt;</code> 修改为此文档顶部显示的版本，并将 <code>&lt;ksp-version&gt;</code> 修改为你项目当前使用的 Kotlin 版本对应的 KSP 版本。</p><h3 id="传统方式" tabindex="-1"><a class="header-anchor" href="#传统方式" aria-hidden="true">#</a> 传统方式</h3><p>在你的根项目 <code>build.gradle.kts</code> 中配置依赖。</p><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#DCBDFB;">plugins</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#DCBDFB;">id</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.google.devtools.ksp&quot;</span><span style="color:#ADBAC7;">) version </span><span style="color:#96D0FF;">&quot;&lt;ksp-version&gt;&quot;</span><span style="color:#ADBAC7;"> apply </span><span style="color:#6CB6FF;">false</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在你的项目 <code>build.gradle.kts</code> 中配置依赖。</p><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#DCBDFB;">plugins</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#DCBDFB;">id</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.google.devtools.ksp&quot;</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#DCBDFB;">dependencies</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#DCBDFB;">ksp</span><span style="color:#ADBAC7;">(</span><span style="color:#96D0FF;">&quot;com.highcapable.hikage:hikage-compiler:&lt;version&gt;&quot;</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请将 <code>&lt;version&gt;</code> 修改为此文档顶部显示的版本，并将 <code>&lt;ksp-version&gt;</code> 修改为你项目当前使用的 Kotlin 版本对应的 KSP 版本。</p><h2 id="功能介绍" tabindex="-1"><a class="header-anchor" href="#功能介绍" aria-hidden="true">#</a> 功能介绍</h2><p>Hikage 的编译模块将在运行时自动生成代码，在更新后，请重新运行 <code>assembleDebug</code> 或 <code>assembleRelease</code> Task 以生成最新的代码。</p><h3 id="生成布局组件" tabindex="-1"><a class="header-anchor" href="#生成布局组件" aria-hidden="true">#</a> 生成布局组件</h3><p>Hikage 可以在编译时为指定的布局组件自动生成布局组件对应的 <code>Hikageable</code> 函数。</p><h4 id="自定义-view" tabindex="-1"><a class="header-anchor" href="#自定义-view" aria-hidden="true">#</a> 自定义 View</h4><p>你可以在你的自定义 <code>View</code> 上加入 <code>HikageView</code> 注解，以标记它生成为 Hikage 布局组件。</p><table><thead><tr><th>参数名称</th><th>描述</th></tr></thead><tbody><tr><td><code>lparams</code></td><td>布局参数 <code>ViewGroup.LayoutParams</code> Class 对象，如果你的自定义 <code>View</code> 是 <code>ViewGroup</code> 的子类，则可以声明或留空使用默认值</td></tr><tr><td><code>alias</code></td><td>布局组件的别名，即要生成的函数名称，默认获取当前 Class 的名称</td></tr><tr><td><code>requireInit</code></td><td>是否要求填写布局的初始化方法块，默认为可省略的参数</td></tr><tr><td><code>requirePerformer</code></td><td>是否要求填写布局的 <code>performer</code> 方法块，默认为可省略的参数，仅在你的自定义 <code>View</code> 是 <code>ViewGroup</code> 的子类时生效</td></tr><tr><td><code>final</code></td><td>是否将布局声明为 “最终布局”，默认否，即此布局是否是 <code>ViewGroup</code> 还是从其继承都将不会生成 <code>performer</code> 方法块，设置为 <code>true</code> 之后，<code>lparams</code> 和 <code>requirePerformer</code> 将不再有效。</td></tr></tbody></table><blockquote><p>示例如下</p></blockquote><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#F69D50;">@HikageView</span><span style="color:#ADBAC7;">(lparams </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> LinearLayout.LayoutParams::</span><span style="color:#DCBDFB;">class</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#F47067;">class</span><span style="color:#ADBAC7;"> </span><span style="color:#F69D50;">MyLayout</span><span style="color:#ADBAC7;">(context: </span><span style="color:#F69D50;">Context</span><span style="color:#ADBAC7;">, attrs: </span><span style="color:#F69D50;">AttributeSet</span><span style="color:#ADBAC7;">? </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#6CB6FF;">null</span><span style="color:#ADBAC7;">) : </span><span style="color:#F69D50;">LinearLayout</span><span style="color:#ADBAC7;">(</span><span style="color:#F69D50;">context</span><span style="color:#ADBAC7;">, </span><span style="color:#F69D50;">attrs</span><span style="color:#ADBAC7;">) {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>编译后，你就可以在 Hikage 布局中使用 <code>MyLayout</code> 作为布局组件了。</p><blockquote><p>示例如下</p></blockquote><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#DCBDFB;">Hikageable</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#DCBDFB;">MyLayout</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">        </span><span style="color:#DCBDFB;">TextView</span><span style="color:#ADBAC7;">(</span></span>
<span class="line"><span style="color:#ADBAC7;">            lparams </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#DCBDFB;">LayoutParams</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">                topMargin </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#6CB6FF;">16</span><span style="color:#ADBAC7;">.dp</span></span>
<span class="line"><span style="color:#ADBAC7;">            }</span></span>
<span class="line"><span style="color:#ADBAC7;">        ) {</span></span>
<span class="line"><span style="color:#ADBAC7;">            text </span><span style="color:#F47067;">=</span><span style="color:#ADBAC7;"> </span><span style="color:#96D0FF;">&quot;Hello, World!&quot;</span></span>
<span class="line"><span style="color:#ADBAC7;">        }</span></span>
<span class="line"><span style="color:#ADBAC7;">    }</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="第三方组件" tabindex="-1"><a class="header-anchor" href="#第三方组件" aria-hidden="true">#</a> 第三方组件</h4><p>Hikage 同样可以为第三方提供的 <code>View</code> 组件自动生成布局组件函数，你可以使用 <code>HikageViewDeclaration</code> 注解来完成。</p><table><thead><tr><th>参数名称</th><th>描述</th></tr></thead><tbody><tr><td><code>view</code></td><td>需要声明的布局组件的 Class 对象</td></tr><tr><td><code>lparams</code></td><td>布局参数 <code>ViewGroup.LayoutParams</code> Class 对象，如果你的自定义 <code>View</code> 是 <code>ViewGroup</code> 的子类，则可以声明或留空使用默认值</td></tr><tr><td><code>alias</code></td><td>布局组件的别名，即要生成的函数名称，默认获取 <code>view</code> Class 的名称</td></tr><tr><td><code>requireInit</code></td><td>是否要求填写布局的初始化方法块，默认为可省略的参数</td></tr><tr><td><code>requirePerformer</code></td><td>是否要求填写布局的 <code>performer</code> 方法块，默认为可省略的参数，仅在你的自定义 <code>View</code> 是 <code>ViewGroup</code> 的子类时生效</td></tr><tr><td><code>final</code></td><td>是否将布局声明为 “最终布局”，默认否，即此布局是否是 <code>ViewGroup</code> 还是从其继承都将不会生成 <code>performer</code> 方法块，设置为 <code>true</code> 之后，<code>lparams</code> 和 <code>requirePerformer</code> 将不再有效。</td></tr></tbody></table><blockquote><p>示例如下</p></blockquote><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#F69D50;">@HikageViewDeclaration</span><span style="color:#ADBAC7;">(ThirdPartyView::</span><span style="color:#DCBDFB;">class</span><span style="color:#ADBAC7;">)</span></span>
<span class="line"><span style="color:#F47067;">object</span><span style="color:#ADBAC7;"> </span><span style="color:#F69D50;">ThirdPartyViewDeclaration</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><p>这个注解可以声明到任意一个 <code>object</code> 类上，仅作为注解扫描器需要自动纳入的类来使用，你可以将可见性设为 <code>private</code>，但要确保被注解的类一定是使用 <code>object</code> 修饰的。</p><p>同样地，编译后，你就可以在 Hikage 布局中使用 <code>ThirdPartyView</code> 作为布局组件了。</p><blockquote><p>示例如下</p></blockquote><div class="language-kotlin line-numbers-mode" data-ext="kt"><pre class="shiki github-dark-dimmed" style="background-color:#22272e;" tabindex="0"><code><span class="line"><span style="color:#DCBDFB;">Hikageable</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">    </span><span style="color:#DCBDFB;">ThirdPartyView</span><span style="color:#ADBAC7;"> {</span></span>
<span class="line"><span style="color:#ADBAC7;">        </span><span style="color:#768390;">// ...</span></span>
<span class="line"><span style="color:#ADBAC7;">    }</span></span>
<span class="line"><span style="color:#ADBAC7;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="custom-container tip"><p class="custom-container-title">小提示</p><p>Hikage 生成布局组件的函数包名路径为 <code>com.highcapable.hikage.widget</code> + 你的 <code>View</code> 或第三方 <code>View</code> 组件的完整包名。</p></div>`,36);function y(A,b){const n=o("ExternalLinkIcon");return p(),i("div",null,[t,s("div",d,[u,s("p",null,[a("你需要在你的项目中集成适合于你项目当前 Kotlin 版本的 "),s("a",v,[a("Google KSP"),c(n)]),a(" 插件。")])]),m])}const h=l(r,[["render",y],["__file","hikage-compiler.html.vue"]]);export{h as default};
