简体   繁体   English

gradle:Android Studio继承buildtype

[英]gradle: Android Studio inherit buildtype

I have in gradle (Android Studio) 4 build types 我在gradle(Android Studio)中有4种构建类型

android {
  buildTypes {
     release { ... }
     debug { ... }
     kindle { ... }
     kindle_debug { ... }
  }
}

And I know, that my src folder can have for each build type one folder. 而且我知道,我的src文件夹可以为每个构建类型都有一个文件夹。 So it ends up into 所以它最终变成

 src/
  -- debug
  -- kindle
  -- kindle_debug
  -- main // used for every project
  -- release

At the moment kindle is the same as release and kindle_debug is the same as debug . 目前, kindlerelease相同, kindle_debugdebug相同。

How can I avoid to duplicate the src-folders? 如何避免重复src文件夹? Is there a way to inherit from release and debug or have I to set the src-folders by myself in the build.gradle file? 有没有一种方法可以从releasedebug继承,或者我可以自己在build.gradle文件中设置src文件夹?

Edit: One solution, that seems to work, is to set symlinks, but I want to use Mac OS and Windows OS and not every new user wants to set symlinks by them self. 编辑:一种似乎可行的解决方案是设置符号链接,但是我想使用Mac OS和Windows OS,并不是每个新用户都希望自己设置符号链接。

Edit 2: I use now product flavors, because of that, I can have debug/release and with that google, amazon and samsung. 编辑2:我现在使用产品口味,因此,我可以进行调试/发布,并且可以使用该Google,亚马逊和三星。 That's the best solution for my purpose. 这是我达到目的的最佳解决方案。

You can inherit from build types like the following: 您可以继承如下的构建类型:

buildTypes {
     release { ... }
     debug { ... }

     kindle {
         initWith buildTypes.release
         ...
     }
     kindle_debug {
         initWith buildTypes.debug
         ...
     }
  }

The answer may be a little late, but I had a similar problem and managed to solve this by doing the following: 答案可能有点晚,但是我遇到了类似的问题,并通过执行以下操作设法解决了这个问题:

android {
    sourceSets {
        kindle {
            java.srcDirs = ["src/release/java", "src/main/java"]
        }
        kindle_debug {
            java.srcDirs = ["src/debug/java", "src/main/java"]
        }
    }
}

If you add this to your build.gradle file, the kindle build type will only use java files from release and main folder and the kindle_debug build type will use debug and release folder. 如果将其添加到build.gradle文件中,kindle构建类型将仅使用release和main文件夹中的java文件,kindle_debug构建类型将使用debug和release文件夹。

Of course you could add the kindle folder or the kindle_debug folder: 当然,您可以添加kindle文件夹或kindle_debug文件夹:

java.srcDirs = ["src/kindle/java", "src/release/java", "src/main/java"]
java.srcDirs = ["src/kindle_debug/java", "src/debug/java", "src/main/java"]

but you may run into duplicate class errors. 但是您可能会遇到重复的类错误。

If I understand this correctly you want only a few files to differ between debug and release builds? 如果我正确理解了这一点,那么您只希望调试和发行版之间只有几个文件有所不同? In this case, the following should work (My usecase was the same as yours): 在这种情况下,以下方法应该起作用(我的用例与您的用例相同):

https://stackoverflow.com/a/28279105/1041533 https://stackoverflow.com/a/28279105/1041533

For the sake of completeness I'll paste the relevant part of above answer here: 为了完整起见,我将上述答案的相关部分粘贴到此处:

  1. Ressource directory based approach 基于资源目录的方法

The intend was to modify a file in the xml folder of each customer based on whether it is a release or a debug build. 目的是根据发布或调试版本来修改每个客户的xml文件夹中的文件。 This can be achieved by a corresponding folder structure. 这可以通过相应的文件夹结构来实现。 Based on the original question we have 3 customers, and each customer has a debug and a release build. 根据最初的问题,我们有3个客户,每个客户都有一个调试和一个发布版本。 The afore mentioned xml files are different for each customer and build type. 对于每个客户和构建类型,上述xml文件都是不同的。 Hence the following directory structure: 因此,以下目录结构为:

src/
  - customerA
    //Contains all relevant resource files specific to customer A
  - customerB
    //Contains all relevant resource files specific to customer B
  - customerC
    //Contains all relevant resource files specific to customer C

  - customerADebug
    //Contains debug server-settings file for customer A
  - customerBDebug
    //Contains debug server-settings file for customer B
  - customerCDebug
    //Contains debug server-settings file for customer C
  - customerARelease
    //Contains release server-settings file for customer A
  - customerBRelease
    //Contains release server-settings file for customer B
  - customerCRelease
    //Contains release server-settings file for customer C

So the main content for each product flavor was in the folder with the same name as the flavor (customerA, customerB etc. see first part of above snippet). 因此,每种产品口味的主要内容都在与口味相同的文件夹中(customerA,customerB等,请参见上面代码片段的第一部分)。 Now this one file, that different based on whether it was a debug or release build for each customer is put into the appropriate folders such as customerADebug --> contains file with server settings for debug mode etc. 现在,将这个文件(基于每个客户的调试版本或发布版本而有所不同)放入相应的文件夹中,例如customerADebug->包含具有调试模式等服务器设置的文件。

And when you build customerA for instance the correct file will be chosen if you build a debug or release build. 例如,在构建customerA时,如果您构建调试或发布版本,则将选择正确的文件。

Do let me know if I should clarify this further. 请让我知道是否需要进一步澄清。

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

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