gradle: Android Studio inherit buildtype

2020-06-30 05:16发布

问题:

I have in gradle (Android Studio) 4 build types

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

And I know, that my src folder can have for each build type one folder. 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.

How can I avoid to duplicate the src-folders? 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?

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.

Edit 2: I use now product flavors, because of that, I can have debug/release and with that google, amazon and samsung. That's the best solution for my purpose.

回答1:

You can inherit from build types like the following:

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

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


回答2:

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.

Of course you could add the kindle folder or the kindle_debug folder:

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.



回答3:

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

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. 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. The afore mentioned xml files are different for each customer and build type. 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). 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.

And when you build customerA for instance the correct file will be chosen if you build a debug or release build.

Do let me know if I should clarify this further.