简体   繁体   English

Android批注处理-为不同的构建风格生成不同的代码

[英]Android annotation processing - generate different code for different build flavor

I'm building a library that requires some annotation processing to generate code. 我正在建立一个需要一些注释处理才能生成代码的库。 I now run into an issue that the release build doesn't need to have as much code as the debug build does (since this is a library for modifying configuration variants - primarily used for testing purposes). 我现在遇到一个问题,即发行版本不需要像调试版本那样具有那么多的代码(因为这是一个用于修改配置变量的库,主要用于测试目的)。 The following code illustrates the situations. 以下代码说明了这种情况。 Let's say I want to create a class ConfigManager from some annotated classes and properties. 假设我想从一些带注释的类和属性创建一个类ConfigManager。 In debug builds, I need this much: 在调试版本中,我非常需要:

public class ConfigManager {
   public Class getConfigClass() {
      return abc.class;
   }
   public void method1() {
      doSomething1();
   }
   public void method2() {
      doSomething2();
   }
   public void method3() {
      doSomething3();
   }
}

While in release builds, I only need this much: 在发布版本中,我只需要这么多:

public class ConfigManager {
   public Class getConfigClass() {
      return abc.class;
   }
}

I have a feeling it may be possible by writing a Gradle plugin to check for build flavor at compile time and invoke a different processor/or somehow pass a parameter to a processor to generate different code. 我感觉有可能通过编写Gradle插件在编译时检查构建风格并调用不同的处理器/或以某种方式将参数传递给处理器以生成不同的代码来实现。 However this topic is pretty new to me so I'm not sure how to achieve this. 但是,这个主题对我来说还很陌生,所以我不确定如何实现。 A couple hours of googling also didnt help. 几个小时的谷歌搜索也没有帮助。 So I'm wondering if anyone could give me a direction or example? 所以我想知道是否有人可以给我一个方向或例子? Thanks 谢谢

Pass an option (release=true/false) to your processor. 将选项(release = true / false)传递给处理器。

From javac https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html javac https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html

-Akey[=value] Specifies options to pass to annotation processors. -Akey [= value]指定要传递给注释处理器的选项。 These options are not interpreted by javac directly, but are made available for use by individual processors. 这些选项不是由javac直接解释的,而是可供单个处理器使用的。 The key value should be one or more identifiers separated by a dot (.). 键值应该是一个或多个用点(。)分隔的标识符。

In combination with Processor.html#getSupportedOptions https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/Processor.html#getSupportedOptions 结合Processor.html#getSupportedOptions https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/Processor.html#getSupportedOptions

Returns the options recognized by this processor. 返回此处理器识别的选项。 An implementation of the processing tool must provide a way to pass processor-specific options distinctly from options passed to the tool itself, see getOptions. 处理工具的实现必须提供一种与传递给工具本身的选项不同的,传递处理器特定选项的方法,请参见getOptions。

Implementation outline: 实施纲要:

  public Set<String> getSupportedOptions() {
    Set<String> set = new HashSet<>();
    set.add("release");
    return set;
  }

  // -Arelease=true
  boolean isRelease(ProcessingEnvironment env) {
    return Boolean.parseBoolean(env.getOptions().get("release"));
  }

See Pass options to JPAAnnotationProcessor from Gradle for how to pass options in a gradle build. 请参阅从Gradle将选项传递给JPAAnnotationProcessor,以了解如何在gradle构建中传递选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM