简体   繁体   English

Android Gradle排除Debug BuildType的类

[英]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 注意

  • Already configured proguard-rules to not strip those classes (since I am using reflection) 已配置的proguard规则不剥离这些类(因为我使用反射)
  • Disabled proguard for release build 发布版本已禁用proguard
  • Tried to define source sets in different places (even in flavors) , it looks like the latest sourceSet defined overrides all the previous. 试图在不同的地方定义源集(甚至在风格中),看起来最新的sourceSet定义覆盖了所有以前的。

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.Foomain ,另一个com.ivelius.awesomeapp.Foorelease -你会得到某种形式的“重复类”的错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM