简体   繁体   English

根据构建风格引用不同的资产

[英]Reference different assets based on build flavor

I am able to get the correct api_key.txt on debug builds because that is a buildType . 我可以在debug版本中获取正确的api_key.txt ,因为它是buildType However I am unable to get this working with the productFlavor amazon . 但是,我无法使用productFlavor amazon来实现此功能。

I've tried doing this in my build.gradle file which compiles but it will not actually reference the correct file at run time: 我尝试在编译的build.gradle文件中执行此操作,但在运行时它实际上不会引用正确的文件:

sourceSets {
amazon {
  assets.srcDirs = ['app/src/amazon/assets']
}

Here is my folder structure: 这是我的文件夹结构:

文件夹结构

How can I reference api_key.txt in the amazon/assets folder when my product flavor is amazon ? 当我的商品风格为amazon时,如何在amazon/assets文件夹中引用api_key.txt

All help is greatly appreciated, thank you. 非常感谢所有帮助,谢谢。

Based on this https://developer.android.com/studio/build/build-variants document you want this type of hierarchy structure of your application. 基于此https://developer.android.com/studio/build/build-variants文档,您需要这种类型的应用程序层次结构。 Remove assets.srcDirs from all build flavours and you can try with this kind of structure. 从所有构建版本中删除assets.srcDirs ,您可以尝试使用这种结构。

buildTypes {
  release {
    // ... the usual stuff here
  }
  releaseAlt {
    // .. the usual stuff here too like signing config etc...
  }
}

file hierarchy You should have like : 文件层次结构应具有:

project/
- app/
 - src/
  - main/
   - assets/
    - logo.png // Generic assets go here
   - java/
   - res/
   - ...

  - flavor1/
   - assets/
    - logo.png // Specific assets for all the flavor1 Variants

  - release/
   - assets/
    - logo.png // Specific assets for all the releaseAlt Variants.

  - flavor1Release/
   - assets/
    - logo.png // very specific assets for the flavor1ReleaseAlt Variant
- SDK/

I think you should write "src/amazon/assets" instead of "app/src/amazon/assets". 我认为您应该写“ src / amazon / assets”而不是“ app / src / amazon / assets”。

sourceSets {
amazon {
  assets.srcDirs = ['/src/amazon/assets']
}

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

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