使用不同的资源(图形和字符串)使用摇篮不同的应用口味 [英] Use different resources (graphics and strings) for different application flavors using gradle

查看:106
本文介绍了使用不同的资源(图形和字符串)使用摇篮不同的应用口味的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有已经在这几个问题(<一href="http://stackoverflow.com/questions/18460095/how-to-provide-different-launcher-icon-per-product-flavour">different没有工作的图标和<一href="http://stackoverflow.com/questions/22607020/how-to-change-android-app-name-in-the-build-gradle-file">different这名没有工作),但回答这些都还没有完全不足以让我的应用程序的工作,我需要的方式。我是全新的,以摇篮,只是昨天开始转换它,并有只是一对夫妇的事情,我一直无法弄清楚。

There are already several questions on this (different icons which didn't work and different names which did work), but the answers to those aren't yet quite enough to get my app working the way I need. I'm brand-new to Gradle, just started converting to it yesterday, and there are just a couple things I haven't been able to figure out.

我有3+的应用程序工作过相同的codeBase的,每个使用不同的应用程序启动器的图标和名称,网址,但现在我不能得到不同的图标和应用程序名称,以工作的每个应用程序。在我加入sourceSets,我的appName和其他自定义字符串的工作,但是图标没有,现在没有什么作品。

I have 3+ apps working off the same codebase, each using different app launcher icons and names, URLs, but right now I can't get the different icons and app names to work for each app. Before I added the sourceSets, my appName and other custom strings worked, but the icon didn't, and now nothing works.

在我的src目录下,我有4个目录: flav1 flav2 flav3 (包含所有我的核心code和默认的资源)。 Android的工作室已经上涨 .RES 到每个每年年底托利每个目录下有一个绘制和值目录与自定义strings.xml中和的icon.png,分别里面。我不,如果绘制应该有一个特定的名称:绘制,绘制,MDPI等,如果它只是不管我决定,我要绘制

Inside my src directory, I have 4 directories: flav1, flav2, flav3, and main (contains all my core code and default resources). Android Studio has tacked .res onto each the end of each flav Each directory has a drawable and values directory with the custom strings.xml and icon.png, respectively, inside. I don't if drawable is supposed to have a specific name: drawable, drawable-mdpi, etc. If it's just whatever I decide, I want drawable.

我没有得到任何错误,只是在键,默认的图标机器人工作室,但我的默认的Andr​​oidManifest.xml里面主不会承认我的图标和它们了。

I'm not getting any errors, just the default strings defined in main and the default icon Android Studio, except my default AndroidManifest.xml inside main won't recognize my icon and them anymore.

下面是我的build.gradle:

Here's my build.gradle:

apply plugin: 'android'

android {
   compileSdkVersion 19
   buildToolsVersion '19.0.2'

   defaultConfig {
      minSdkVersion 8
      targetSdkVersion 19
   }

   buildTypes {
      release {
         runProguard false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
      }
   }

   productFlavors {
      flav1 {
         packageName "com.example.flav1"
         versionCode 32
         versionName "1.0.5"
      }

      flav2 {
         packageName "com.example.flav2"
         versionCode 33
         versionName "1.0.6"
      }

      flav3 {
         packageName "com.example.flav3"
         versionCode 27
         versionName "1.0.0"
      }
   }

   sourceSets {
      main {
          // I don't understand how these are supposed to work
          java.srcDirs = ['java']
          resources.srcDirs = ['src']
          aidl.srcDirs = ['src']
          renderscript.srcDirs = ['src']
          // res.srcDirs = ['res']
          assets.srcDirs = ['assets']
      }

      flav1 {
          // I can't figure out the path to these
          res.srcDirs = ['res', '/flav1']
      }

      flav2 {
          res.srcDirs = ['res', '/flav2']
      }

      flav3 { 
          res.srcDirs = ['res', '/flav3']
      }
   }

   // I read that these are necessary, but I don't know how to use them
   sourceSets.flav1 {
      res {
          srcDir 'flav1'
      }
      resources {
          srcDir 'flav1'
      }
   }
   sourceSets.flav2 {
      res {
          srcDir 'flav2'
      }
      resources {
          srcDir 'flav2'
      }
   }
   sourceSets.flav3 {
      res {
          srcDir 'flav3'
      }
      resources {
          srcDir 'flav3'
      }
   }
}

dependencies {
   // code
}

我AP preciate任何帮助!

I appreciate any help!

推荐答案

如果你可以重新安排你的文件,这是解决这一问题的最简单方法。

If you can rearrange your files, that's the simplest way to fix this.

您想把它们是这样的:

yourApp/
  src/
    main/
      java/
      res/
    flav1/
      java/
      res/
    flav2/
      java/
      res/
    flav3/
      java/
      res/

然后在build.gradle,你可以摆脱sourceSets的全部。保持productFlavors。

Then in build.gradle, you can get rid of sourceSets entirely. Keep productFlavors.

至于资源的压倒一切的,它应该是自动的:构建系统合并所有资源的全部源目录,如果不同的文件夹中包含的资源具有相同名称的构建变量,则优先顺序的。以下内容:构建类型的资源会覆盖从产品的味道,这覆盖在主源代码目录的资源

As far as the overriding of resources, it should be automatic: "The build system merges all the resources from the all the source directories. If different folders contain resources with the same name for a build variant, the priority order is the following: build type resources override those from the product flavor, which override the resources in the main source directory."

这篇关于使用不同的资源(图形和字符串)使用摇篮不同的应用口味的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆