简体   繁体   English

我怎样才能有单独的 gradle 项目的调试和发布版本,它们可以引入相同 class 的不同版本?

[英]How can I have separate debug and release versions of gradle project that pull in different versions of the same class?

I have two versions of the same Java class (same name / methods).我有两个版本的相同 Java class (相同的名称/方法)。 Since it's Java, both.java files have the same name.由于它是 Java,因此两个.java 文件具有相同的名称。 I want to configure gradle in such a way that I can build a "debug" version of my application that pulls in one of these files, and a "production" version of my application that pulls in the other one.我想配置 gradle,这样我就可以构建应用程序的“调试”版本来拉入其中一个文件,以及构建应用程序的“生产”版本来拉入另一个文件。 How would I go about doing this?我将如何 go 这样做?

This class has only static methods.这个 class 只有 static 方法。 I don't ever want to make an instance of this class.我不想创建这个 class 的实例。 I additionally don't want to add the overhead of an if statement in each of the methods on this class to check which version I'm in.我也不想在这个 class 的每个方法中添加if语句的开销来检查我所在的版本。

Following @JFabianMeier's answer you could use 4 projects:按照@JFabianMeier 的回答,您可以使用 4 个项目:

  1. with the production version class与生产版本 class
  2. with the debug version class带调试版本 class
  3. with code that uses either of the two, parameterized according to Migrating Maven profiles...Example 6. Mimicking the behavior of Maven profiles in Gradle .使用使用两者之一的代码,根据迁移 Maven 配置文件进行参数化...示例 6. 在 Gradle 中模仿 Maven 配置文件的行为 (I'm also a Maven guy and therefore can't tell you exactly how to do it in Gradle.) (我也是一个 Maven 的人,因此不能告诉你在 Gradle 中的具体操作方法。)
  4. a multi - project with 1./2./3.具有 1./2./3 的项目 as sub [-] projects for building all of them in one go, maybe parameterized to build just 1.+ 3. or 2.+ 3.作为[-] 项目,用于在一个 go 中构建所有这些项目,可能参数化为仅构建 1.+ 3. 或 2.+ 3。

Have you tried creating production and debug source directories/sets?您是否尝试过创建生产和调试源目录/集? According to the docs you can use multiple directories when specifying source directories in your source set.根据文档,您可以在源集中指定源目录时使用多个目录。 Try dropping the different versions of your class in their respective production/debug source directories.尝试将 class 的不同版本放到它们各自的生产/调试源目录中。

I haven't tested myself (not sure about the syntax), but this is based on the Gradle's Java compilation documentation .我没有测试过自己(不确定语法),但这是基于 Gradle 的Java 编译文档

sourceSets {
    // Could also name this production
    main {
        java {
            srcDirs ['src/main/java', 'src/prod/java']
        }
    }
    debug {
        java {
            srcDirs ['src/main/java', 'src/debug/java']
        }
    }
}

You could do the following:您可以执行以下操作:

  • Put the class into a separate project (so generate a separate jar from it)将 class 放入一个单独的项目中(因此从中生成一个单独的 jar)
  • Then you can have two different jars, for production and debugging然后就可以有两个不同的jars,用于生产调试
  • Then you can pull either one or the other jar in gradle depending on a parameter.然后,您可以根据参数在 gradle 中拉出一个或另一个 jar。

Alternatively, you could look into template engines like Velocity which allow you to generate source code during the build depending on variables and then compile it.或者,您可以查看 Velocity 之类的模板引擎,它允许您在构建期间根据变量生成源代码,然后编译它。

Android has a neat feature called Product Flavors. Android 有一个称为产品风味的简洁功能。 It lets you swap classes at compile time effortlessly and keep your project clean.它可以让你在编译时毫不费力地交换类并保持你的项目干净。

This post is very good to get a taste of it: https://android-developers.googleblog.com/2015/12/leveraging-product-flavors-in-android.html这个帖子很好去尝尝: https://android-developers.googleblog.com/2015/12/leveraging-product-flavors-in-android.html

And here is the full documentation: https://developer.android.com/studio/build/build-variants#product-flavors这是完整的文档: https://developer.android.com/studio/build/build-variants#product-flavors

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

相关问题 如何在多项目项目中拥有两个不同的 Gradle 版本? - How can I have two different Gradle versions in a multi-project project? 同一 Gradle 项目中不同 Java 版本的代码 - Code for different Java versions in same Gradle Project 为什么我有不同版本的 gradle 和 gradle wrapper? - why do I have different versions of gradle and gradle wrapper? Gradle-同一库的2个不同版本 - Gradle - 2 Different versions of the same library 我如何在SonarQube中比较同一项目的两个任意版本 - How can i compare two arbitrary versions of same project in SonarQube Java - 如何加载同一类的不同版本? - Java - how to load different versions of the same class? 如何在同一应用程序中使用不同版本的 class? - How to use different versions of a class in the same application? 我可以在同一个项目上使用两个不同的 Eclipse (IDE java) 版本吗? - Can I use two different Eclipse (IDE java) versions on same project? 如何将同一个程序包的多个版本作为项目的间接依赖项? - How to have multiple versions of the same package as the indirect dependencies of a project? 如何从java maven项目中的两个不同版本的jar加载类的两个版本? - How to load two versions of a class from two different versions of jars in a java maven project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM