[英]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 个项目:
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:您可以执行以下操作:
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.