Android Gradle library build: Include & exclude fi

2019-06-06 04:27发布

问题:

I exporting Android library via building aar with Gradle, and I have requirements for the jar inside the aar: Basically I need the jar to reflect following files structure that already exist in the project (jpeg files should be inside java package alongside .class files):

project_name
           |
           module_name
                     |
                     src
                       |
                       main
                          |
                          java
                             |
                             package_to_include
                                              |
                                              java_files
                                              jpeg_files
                             package_to_exclude

This is the relevant part from build.gradle script:

apply plugin: 'com.android.library'    
android {
    ... 
    sourceSets {
        main {
            java {
                srcDir 'src/main/java'
                exclude 'package_to_exclude/**'
                resources.includes = [ 'package_to_include/*.jpeg' ]
            }
            resources {
                srcDir 'src/../lib'
            }
        }
    }
}

Result: The exclude part works, the include not: In the jar inside package_to_include there are only the java source code files as .class files. The jpeg's not in the jar at all.

By the way I'm not totally familiar with the double astrix (**) operator. Maybe this is the direction for solution?

Thanks,

回答1:

As you suspect the ** is the missing component. Guessing the path to your include is probably missing the src/main/java prefix to the package_to_include.

** means any folder reclusively. So for example:

src/main/java/package_to_include/image.jpg
src/main/res/image.jpg
// both would match
**/image.jpg

Also this line is weird srcDir 'src/../lib' because its the same as srcDir 'lib'