如何在Gradle项目中拥有单独的调试和发布版本,以引入同一类的不同版本?

huangapple go评论71阅读模式
英文:

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

问题

我有同名的两个版本的Java类(名称和方法相同)。由于是Java,两个 .java 文件的名称相同。我想要配置gradle,以便我可以构建应用的“调试”版本,该版本会引入其中一个文件,并且构建应用的“生产”版本会引入另一个文件。我应该如何操作?

这个类只包含静态方法。我永远不想创建这个类的实例。另外,我不想在这个类的每个方法中都添加一个if语句来检查我在哪个版本中,以避免增加额外的开销。

英文:

I have two versions of the same Java class (same name / methods). Since it's Java, both .java files have the same name. 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. How would I go about doing this?

This class has only static methods. I don't ever want to make an instance of this 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.

答案1

得分: 4

以下是翻译好的部分:

根据 @JFabianMeier 的回答,您可以使用以下 4 个项目:

  1. 使用生产版本类。
  2. 使用调试版本类。
  3. 使用根据 迁移 Maven 配置文件 ... 参数化的代码,模拟 Maven 在 Gradle 中的配置文件行为。 (我也是个 Maven 用户,因此无法准确告诉您如何在 Gradle 中实现。)
  4. 创建一个多项目工程,其中 1./2./3. 作为子项目进行配置,以便一次性构建所有项目,也可以根据参数构建仅 1.+ 3. 或 2.+ 3。
英文:

Following @JFabianMeier's answer you could use 4 projects:

  1. with the production version class
  2. with the debug version 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. (I'm also a Maven guy and therefore can't tell you exactly how to do it in Gradle.)
  4. a multi-project with 1./2./3. as sub[-]projects for building all of them in one go, maybe parameterized to build just 1.+ 3. or 2.+ 3.

答案2

得分: 3

你尝试过创建生产和调试源目录/集吗?根据文档,当在源集中指定源目录时,可以使用多个目录。尝试将不同版本的类放入各自的生产/调试源目录中。

我自己没有测试过(不确定语法),但这是基于Gradle的Java编译文档

sourceSets {
    // 也可以将此命名为production
    main {
        java {
            srcDirs ['src/main/java', 'src/prod/java']
        }
    }
    debug {
        java {
            srcDirs ['src/main/java', 'src/debug/java']
        }
    }
}
英文:

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.

I haven't tested myself (not sure about the syntax), but this is based on the Gradle's Java compilation documentation.

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

答案3

得分: 2

你可以进行以下操作:

  • 将这个类放入一个单独的项目中(从而从中生成一个独立的 jar 文件)
  • 然后你可以拥有两个不同的 jar 文件,用于生产和调试
  • 然后根据参数在 Gradle 中引入其中一个 jar 文件

或者,你可以研究像 Velocity 这样的模板引擎,它允许你根据变量在构建过程中生成源代码,然后进行编译。

英文:

You could do the following:

  • Put the class into a separate project (so generate a separate jar from it)
  • Then you can have two different jars, for production and debugging
  • Then you can pull either one or the other jar in gradle depending on a parameter.

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.

答案4

得分: 1

安卓有一个很棒的功能叫做产品风味(Product Flavors)。它允许你在编译时轻松地交换类,并保持项目的清洁。

这篇文章非常适合入门:https://android-developers.googleblog.com/2015/12/leveraging-product-flavors-in-android.html

这里是完整的文档:https://developer.android.com/studio/build/build-variants#product-flavors

英文:

Android has a neat feature called Product Flavors. 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

And here is the full documentation: https://developer.android.com/studio/build/build-variants#product-flavors

huangapple
  • 本文由 发表于 2020年7月24日 01:55:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63060339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定