[英]Android Gradle exclude classes for Debug BuildType
Bakground Bakground
I am using Google Play Services in my project for various reasons. 我出于各种原因在我的项目中使用Google Play服务。 Google Play Services are a big dependancy which is drastically increases my build time. Google Play服务是一个很大的依赖,它会大大增加我的构建时间。 So I would like to disable Google Play Services for my "Debug" version , so I could compile my project faster. 所以我想为我的“Debug”版本禁用Google Play服务,这样我就可以更快地编译我的项目。
What I do ? 我所做的 ?
To exclude Google Play Services during my debug builds , I just do the conditional compilation like this : 要在我的调试版本中排除Google Play服务,我只需执行以下条件编译:
releaseCompile 'com.google.android.gms:play-services-plus:7.5.+'
releaseCompile 'com.google.android.gms:play-services-ads:7.5.+'
releaseCompile 'com.google.android.gms:play-services-gcm:7.5.+'
So Google Play Services are being compiled only for release builds , and not included in Debug builds.So far so good. 因此,Google Play服务仅针对发布版本进行编译,而不包含在Debug版本中。所以非常好。
A pitfall 一个陷阱
There are some classes in my code that are dependent on Google Play Services.I can easily abstract them with interfaces , and load stubs instead.But the problem is , those classes are still being compiled during my "Debug" build even though I am not referencing them directly (in fact I am loading them using reflection). 我的代码中有一些类依赖于Google Play Services。我可以使用接口轻松地抽象它们,然后加载存根。但问题是,这些类仍在我的“Debug”构建期间编译,即使我不是直接引用它们(事实上我正在使用反射加载它们)。
Workaround 解决方法
To ignore compilation errors for classes I am not using ,I just exclude them from source sets in debug build type like this : 要忽略我没有使用的类的编译错误,我只是将它们从调试构建类型的源集中排除,如下所示:
debug {
minifyEnabled false
sourceSets {
main {
java {
exclude '**/tracking/impl/**'
exclude '**/GoogleApiClientWrapper.java'
}
}
}
}
And the problem is solved , I am able to compile by "Debug" version without Google play services and the build time is faster. 问题解决了,我可以通过“调试”版本编译而没有谷歌播放服务,并且构建时间更快。
The problem 问题
Even though I am specifying the sourceSets block only in the "Debug" build type , I have noticed that those classes are stripped anyway even in release build Types. 即使我只在“Debug”构建类型中指定sourceSets块,我也注意到即使在发布版本类型中也会剥离这些类。 Why ? 为什么? How can I exclude those classes ONLY for Debug build type ? 如何仅针对Debug构建类型排除这些类?
NOTE 注意
If you want to have Java classes that are unique to some build type or product flavor, put them in a sourceset for that build type or product flavor. 如果您希望拥有某些构建类型或产品风格所特有的Java类,请将它们放在该构建类型或产品风格的源集中。
If you have a typical Gradle for Android project structure, somewhere, you have src/main/java/
, with Java classes in there in appropriate directories based upon package name. 如果您有一个典型的Gradle for Android项目结构,在某处,您有src/main/java/
,其中Java类位于适当的目录中,基于包名称。 Those classes, being in the main
sourceset, are used for all builds. 这些类位于main
集中,用于所有构建。
If you want to have classes are are only used in a release
build, create a src/release/java/
directory, and move those classes from src/main/java/
to src/release/java/
. 如果您希望类只在release
版本中使用,请创建一个src/release/java/
目录,并将这些类从src/main/java/
到src/release/java/
。
In a debug
build, the release
classes are ignored. 在debug
版本中,将忽略release
类。 In a release
build, the release
classes are used. 在release
版本中,使用release
类。
Note that you cannot use this technique to replace classes in main
. 请注意,您无法使用此技术替换 main
类。 So, you cannot have com.ivelius.awesomeapp.Foo
in main
and another com.ivelius.awesomeapp.Foo
in release
— you will get some form of "duplicate class" error. 所以,你不能有com.ivelius.awesomeapp.Foo
在main
,另一个com.ivelius.awesomeapp.Foo
在release
-你会得到某种形式的“重复类”的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.