英文:
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 个项目:
- 使用生产版本类。
- 使用调试版本类。
- 使用根据 迁移 Maven 配置文件 ... 参数化的代码,模拟 Maven 在 Gradle 中的配置文件行为。 (我也是个 Maven 用户,因此无法准确告诉您如何在 Gradle 中实现。)
- 创建一个多项目工程,其中 1./2./3. 作为子项目进行配置,以便一次性构建所有项目,也可以根据参数构建仅 1.+ 3. 或 2.+ 3。
英文:
Following @JFabianMeier's answer you could use 4 projects:
- with the production version class
- with the debug version class
- 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.)
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论