Build variants in Gradle for a Library Project in Android

前端 未结 6 1538
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 12:32

I am trying to configure with Gradle a project which contains some external libraries. With Gradle I can setup different Environmental Configuration (with a class inside a c

6条回答
  •  眼角桃花
    2020-12-02 13:03

    UPDATE - since the time of this posting, there have been much progress in the gradle build process, thus this answer might not be the recommended best practice and new changes might even brake it. Use your own discretion.

    I believe that there is a bit of confusion over the whole project structure and configuration. Let's say that you have the following build.gradle configuration

    sourceSets {
    
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            //resources.srcDirs = ['src/main']
            //aidl.srcDirs = ['src/main']
            //renderscript.srcDirs = ['src/main']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }
    
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
    

    Your project folder structure should be as follow

    project_root
       -src
          -main
             -java
                -com
                   -example
       -build-types
          -debug
             -java
                -com
                   -example
                      -FooBar.java
          -release
             -java
                -com
                   -example
                      -FooBar.java
    

    FooBar.java must not be in the prooject_root/src/main/java/com/example. It must be in debug and release folder which is to reside outside src folder but inside build-types folder. That is configured by setRoot('build-types/*****') method. A lot of people get confused from seeing 'debug/java' and 'main/java', where the later is referenced in a manner 'src/main/java' from presentation and end up putting 'debug/java' in src, the wrong folder. I hope this help.

    For more complex environment involving other libraries, you can check out my answer here https://stackoverflow.com/a/19918834/319058

提交回复
热议问题