{"id":1633,"date":"2023-03-25T21:28:50","date_gmt":"2023-03-25T13:28:50","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1633"},"modified":"2023-04-23T22:01:13","modified_gmt":"2023-04-23T14:01:13","slug":"android-apt-annotation-processor-automatically-generates-files","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/android-apt-annotation-processor-automatically-generates-files\/","title":{"rendered":"Android APT\u6ce8\u89e3\u5904\u7406\u5668\u81ea\u52a8\u751f\u6210\u6587\u4ef6"},"content":{"rendered":"<h2>\u80cc\u666f<\/h2>\n<p>\u4f7f\u7528(<code>Annotation Processing Tool<\/code>)\u5373\u6ce8\u89e3\u5904\u7406\u5668\uff0c\u65e0\u8bba\u600e\u4e48Build\u90fd\u4e0d\u80fd\u81ea\u52a8\u751f\u6210\u6240\u9700\u8981\u7684\u6587\u4ef6<\/p>\n<h2>\u6dfb\u52a0\u6ce8\u89e3<\/h2>\n<p><!-- more --><\/p>\n<p>Annotation\u4e3aJava library Module\uff0c\u5f88\u7b80\u5355<\/p>\n<pre><code class=\"language-java\">@Target(ElementType.FIELD) \/\/\u4f5c\u7528\u4e8e\u5c5e\u6027\u4e0a  \n@Retention(RetentionPolicy.CLASS) \/\/\u4f5c\u7528\u4e8e\u7f16\u8bd1\u671f\uff0c.class \u5b58\u5728\u4e8eapk\u4e2d\npublic @interface BindView {\n    int value();\n}<\/code><\/pre>\n<pre><code>apply plugin: &#039;java-library&#039;\n\ndependencies {\n    compileOnly fileTree(dir: &#039;libs&#039;, include: [&#039;*.jar&#039;])\n}\n\nsourceCompatibility = JavaVersion.VERSION_1_8\ntargetCompatibility = JavaVersion.VERSION_1_8<\/code><\/pre>\n<h2>\u6dfb\u52a0\u4f9d\u8d56<\/h2>\n<p>\uff081\uff09\u5728\u5904\u7406\u6ce8\u89e3\u7684Java library Module \u540d\u5b57\u4e3aComplier\uff0c\u6dfb\u52a0\u4f9d\u8d56<\/p>\n<pre><code>apply plugin: &#039;java-library&#039;\n\ndependencies {\n    \/\/ implementation fileTree(dir: &#039;libs&#039;, include: [&#039;*.jar&#039;])\n    \/\/ compiler \u4e2d\u7528 implementation\n    \/\/if (hasProperty(&quot;publishAar&quot;)) {\n        \/\/implementation &quot;cc.androidx:annotation:${VERSION_NAME}&quot;\n    \/\/} else {\n        implementation project(path: &#039;:annotation&#039;)\n    \/\/}\n\n    \/\/compileOnly &#039;com.google.auto.service:auto-service:1.0-rc6&#039;\n    implementation &#039;com.google.auto.service:auto-service:1.0-rc6&#039;\n    annotationProcessor &#039;com.google.auto.service:auto-service:1.0-rc6&#039;\n    implementation &#039;com.google.auto:auto-common:0.10&#039;\n    implementation &#039;com.squareup:javapoet:1.11.1&#039;\n}\n\n\/\/\u4e2d\u6587\u4e71\u7801\u95ee\u9898\uff08\u9519\u8bef\uff1a\u7f16\u7801GBK\u4e0d\u53ef\u6620\u5c04\u5b57\u7b26\uff09\ntasks.withType(JavaCompile) {\n    options.encoding = &quot;UTF-8&quot;\n}\n\nsourceCompatibility = JavaVersion.VERSION_1_8\ntargetCompatibility = JavaVersion.VERSION_1_8<\/code><\/pre>\n<p>\uff082\uff09Java\u4ee3\u7801\u4e3b\u8981\u6709\u4e00\u4e2a\u7c7bButterKnife\u548c\u4e00\u4e2a\u63a5\u53e3ViewBinder\uff0c\u5982\u4e0b\uff1a<\/p>\n<p>ButterKnife.java<\/p>\n<pre><code class=\"language-java\">public class ButterKnife {\n    public static void bind(Activity activity) {\n        String className = activity.getClass().getName() + &quot;$ViewBinder&quot;;\n        try {\n            Class&lt;?&gt; aClass = Class.forName(className);\n            ViewBinder viewBinder = (ViewBinder) aClass.newInstance();\n            viewBinder.bind(activity);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<p>ViewBinder.java<\/p>\n<pre><code class=\"language-java\">public interface ViewBinder&lt;T&gt; {\n    void bind(T t);\n}<\/code><\/pre>\n<h2>\u6ce8\u89e3\u5904\u7406\u5668(Complier)<\/h2>\n<p>\u5728Complier Module\u4e2d\uff0c\u53ea\u9700\u6709\u4e00\u4e2a\u7c7b<code>ButterKnifeProcessor<\/code>\u5373\u53ef\u5b9e\u73b0<\/p>\n<pre><code class=\"language-java\">@AutoService(Processor.class)  \/\/\u52a0\u4e0a\u8fd9\u4e2a\u6ce8\u89e3\u624d\u80fd\u53bb\u6267\u884c\npublic class ButterKnifeProcessor extends AbstractProcessor {\n    private Elements elementUtils;  \/\/Elements\u4e2d\u5305\u542b\u7528\u4e8e\u64cd\u4f5c\u7684\u5de5\u5177\n    private Filer filer;  \/\/\u7528\u6765\u521b\u5efa\u65b0\u7684\u6e90\u6587\u4ef6\uff0cclass\u4ee5\u53ca\u5176\u4ed6\u6587\u4ef6\n\n    @Override\n    public synchronized void init(ProcessingEnvironment processingEnvironment) {\n        super.init(processingEnvironment);\n        \/\/\u6b64\u65b9\u6cd5\u4e3b\u8981\u662f\u53bb\u521d\u59cb\u5316\u9700\u8981\u7528\u5230\u7684\u5de5\u5177\n        elementUtils = processingEnvironment.getElementUtils();\n        filer = processingEnvironment.getFiler();\n    }\n\n    @Override\n    public Set&lt;String&gt; getSupportedAnnotationTypes() {\n        \/\/\u6dfb\u52a0\u652f\u6301\u7684\u6ce8\u89e3\u7c7b\u578b\n        Set&lt;String&gt; setTypes = new LinkedHashSet&lt;&gt;();\n        setTypes.add(BindView.class.getCanonicalName())\uff1a\n        return setTypes;\n    }\n\n    @Override\n    public SourceVersion getSupportedSourceVersion() {\n        \/\/\u7528\u54ea\u4e2a\u7248\u672c\u7684jdk\u7f16\u8bd1\n        return SourceVersion.RELEASE_8;\n    }\n\n    \/**\n     * \u6ce8\u89e3\u5904\u7406\u5668\u7684\u6838\u5fc3\u65b9\u6cd5\uff0c\u5904\u7406\u5177\u4f53\u7684\u6ce8\u89e3\uff0c\u751f\u6210\u65b0\u7684 Java Class\u6587\u4ef6\n     *\/\n    @Override\n    public boolean process(Set&lt;? extends TypeElement&gt; set, RoundEnvironment roundEnvironment) {\n        try {\n            JavaFileObject fileObject = filer.createSourceFile(&quot;LuacherActivity$ViewBinder&quot;);\n            \/\/\u83b7\u53d6writer\uff0cwirter\u5c31\u7c7b\u4f3c\u4e00\u652f\u7b14\uff0c\u53bb\u5199java\u6587\u4ef6\u91cc\u7684\u6bcf\u4e00\u884c\u4ee3\u7801\n            Writer writer = fileObject.openWriter();\n            writer.write(&quot;package cn.appblog.apt;\\n&quot;);      \/\/\u5305\u540d\n            writer.write(&quot;\\n&quot;);\n            writer.write(&quot;import cn.appblog.aptlib.ViewBinder;\\n&quot;);\/\/\u5bfc\u5305\n            writer.write(&quot;\\n&quot;);\n            writer.write(&quot;import cn.appblog.apt.LuacherActivity;\\n&quot;);\n            writer.write(&quot;\\n&quot;);\n             writer.write(&quot;import cn.appblog.apt.R;\\n&quot;);\n            writer.write(&quot;\\n&quot;);\n            writer.write(&quot;public class LuacherActivity$ViewBinder implements ViewBinder&lt;LuacherActivity&gt; {\\n&quot;);\n            writer.write(&quot;    @Override\\n&quot;);\n            writer.write(&quot;    public void bind(LuacherActivity target) {\\n&quot;);\n            writer.write(&quot;        target.tvText = target.findViewById(R.id.tv_text);\\n}\\n&quot;);\n            writer.write(&quot;}&quot;);\n            \/\/\u6700\u540e\u4e0d\u8981\u5fd8\u8bb0\u5173\u6d41\n            writer.flush();\n            writer.close();\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n        return false;\n    }\n}<\/code><\/pre>\n<p>\u76ee\u524d\u4ee3\u7801\u5199\u6b7b\uff0c\u771f\u6b63\u5b9e\u73b0\u53ef\u4ee5\u4ece\u53c2\u6570RoundEnvironment\u4e2d\u83b7\u53d6\u5230\u6ce8\u89e3\u7684\u7c7b\uff0c\u8fd9\u91cc\u53ea\u662f\u770b\u4e0bWriter\u7684\u7528\u6cd5\u3002\u5177\u4f53\u5b9e\u73b0\u53ef\u4ee5\u53c2\u8003 <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.jianshu.com\/p\/7af58e8e3e18\" title=\"\u3010Android\u3011APT\">\u3010Android\u3011APT<\/a><\/p>\n<p>\u4e3b\u8981\u662f\u91cd\u5199<code>init(ProcessingEnvironment processingEnvironment)<\/code>\uff0c<code>getSupportedAnnotationTypes()<\/code>\uff0c<code>getSupportedSourceVersion()<\/code>\u4ee5\u53ca<code>process(Set&lt;? extends TypeElement&gt; set, RoundEnvironment roundEnvironment)<\/code>\u8fd9\u56db\u4e2a\u65b9\u6cd5<\/p>\n<h2>\u5bfb\u627e\u4e0d\u80fd\u81ea\u52a8\u751f\u6210\u6587\u4ef6\u7684\u95ee\u9898<\/h2>\n<p>\uff081\uff09<code>@AutoService(Processor.class)<\/code>\uff0c\u8fd9\u4e2a\u6ce8\u89e3\u5fc5\u987b\u6dfb\u52a0\uff0c\u5426\u5219\u6ce8\u89e3\u5904\u7406\u5668\u4e0d\u80fd\u6267\u884c<\/p>\n<p>\uff082\uff09\u6700\u540e\u5199\u5b8cwirter\u6d41\u6ca1\u6709\u5173\u95ed\uff0c\u4f1a\u9020\u6210\u751f\u6210\u6587\u4ef6\uff0c\u4f46\u6587\u4ef6\u5185\u5bb9\u4e3a\u7a7a\uff0c\u6216\u8005\u4e0d\u5168<\/p>\n<p>\uff083\uff09app Module \u8981\u6dfb\u52a0\u4e4b\u524d\u7684\u4e09\u4e2aLibrary\u4f9d\u8d56\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code>implementation project(path: &#039;:Annotation&#039;)\nannotationProcessor project(path: &#039;:Complier&#039;)<\/code><\/pre>\n<p>\u4ee5\u4e0a\u53cd\u590d\u68c0\u67e5\u4e86N\u6b21\uff0c\u786e\u8ba4\u65e0\u8bef\u4ecd\u7136\u4e0d\u80fd\u7f16\u8bd1\u751f\u6210\u60f3\u8981\u770b\u5230\u7684\u6587\u4ef6\uff0c\u6700\u540e\u627e\u4e2a\u5927\u795e\u8bf7\u6559\u4e0b\u3002\u8bf4\u662fAndroid Gradle\u53ca\u6784\u5efa\u7248\u672c\u95ee\u9898\uff0c\u6211\u7684\u662f<code>3.4.2 + 5.2.1<\/code>\uff0c\u4f1a\u51fa\u73b0\u4e0d\u517c\u5bb9\u7684\u60c5\u51b5\uff0c\u5927\u795e\u5efa\u8bae<code>3.4.2 \uff0b 5.1.1<\/code>\u53ca\u4ee5\u4e0b\u90fd\u53ef\u4ee5\u3002\u542c\u4e86\u5efa\u8bae\u964d\u4f4e\u7248\u672c\u679c\u7136\u6784\u5efa\u7f16\u8bd1\u6210\u529f\uff0c\u65b0\u7684\u6587\u4ef6\u751f\u6210<\/p>\n<p>\u81ea\u52a8\u751f\u6210\u7684<code>LuacherActivity$ViewBinder<\/code>\u7c7b<\/p>\n<pre><code class=\"language-java\">package cn.appblog.apt;\n\nimport cn.appblog.aptlibViewBinder;\nimport cn.appblog.apt.LuacherActivity;\nimport cn.appblog.apt.R;\n\npublic class LuacherActivity$ViewBinder implements ViewBinder&lt;LuacherActivity&gt; {\n    @Override\n    public void bind(LuacherActivity target) {\n        target.tvText = target.findViewById(R.id.tv_text);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u4f7f\u7528(Annotation Processing Tool)\u5373\u6ce8\u89e3\u5904\u7406\u5668\uff0c\u65e0\u8bba\u600e\u4e48Build\u90fd\u4e0d\u80fd\u81ea\u52a8\u751f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[409],"class_list":["post-1633","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-apt"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1633"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1633\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}