Gradle swap jniLibs resources based on build flavor

后端 未结 3 973
挽巷
挽巷 2021-01-13 10:15

I am trying to swap some resources in the res/raw folder and the jniLibs/armeabi folder based on whether its a release buildType or a

3条回答
  •  醉酒成梦
    2021-01-13 11:17

    Anything that's normally under src/main/ can be put in a different folder:

    src//AndroidManifest.xml
    src//java
    src//res
    src//assets
    src//resources
    src//jni
    src//jniLibs
    src//aidl
    src//rs
    

    Where element is the name of a build type or a product flavor. If your variant includes such a element (build type or flavor), then that source set is also used in addition to src/main

    Note that the location is really not relevant if you have configured it. What matters is that there is a android.sourcesets.main element that contains the sources common to all variants, and each variant has a set of sourcesets.

    For instance if you have a flavor phoneRelease it's really using the following sourcesets:

    android.sourcesets.main
    android.sourcesets.phone
    android.sourcesets.release
    android.sourcesets.phoneRelease
    

    If you have another variant tabletRelease, it'll use the following:

    android.sourcesets.main
    android.sourcesets.tablet
    android.sourcesets.release
    android.sourcesets.phoneRelease
    

    So the phone/tablet sourceset is different and is where you'd put the variant specific sources, unless you want to be even more specific and use the phoneRelease/tabletRelease sourcesets (though those are less used generally.) By default these would be src/phone/... and src/tablet/... (or src/phoneRelease/...) but you can change that however you want and as long as it's connected to the android.sourcesets.* objects it'll be fine.

    for example, doing:

    android {
      sourcesets {
        phone {
          jniLibs.srcDirs = ['phoneRelease/jniLibs/']
        }
        tablet {
          jniLibs.srcDirs = ['tabletRelease/jniLibs/']
        }
      }
    }
    

    is fine. But do be aware that you only changed the jniLibs folder and not the other source elements (java, res, etc...)

    If you kept the default location for the main sourcesets, I'd just keep everything under src/

    You can see more info about sourcesets and how multiple sourcesets are combined here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Sourcesets-and-Dependencies

提交回复
热议问题