Android Studio 中的产品风味 [英] Product flavors in Android Studio

查看:26
本文介绍了Android Studio 中的产品风味的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让产品口味起作用.我有这个 gradle

I can not get product flavours working. I have this gradle

apply plugin: 'com.android.application'
android {
    defaultConfig {
      minSdkVersion 14
      targetSdkVersion 24
      compileSdkVersion 27
    }
    signingConfigs {
        release {
      }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            signingConfig signingConfigs.release
        }
    }
    repositories {
        maven { url "https://jitpack.io" }
    }
    flavorDimensions "dim1", "dim2", "dim3"
    productFlavors {
        flavor1 {
            dimension "dim1"
            applicationId "com.example.dim1.app"
        }
        flavor3 {
            dimension "dim2"
            applicationId "com.example.dim2.app"
        }
        flavor3 {
            dimension "dim3"
            applicationId "com.example.dim3.app"
        }
    }
    sourceSets {
        flavor1 {
          java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
          manifest.srcFile "W:/android-studio-projects/sharedid/app/src/example1/AndroidManifest.xml"
          assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/example1/assets/"]
          resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/example1/res/"]
        }
        flavor2 {
          java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
          manifest.srcFile "W:/android-studio-projects/sharedid/app/src/example2/AndroidManifest.xml"
          assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/example2/assets/"]
          resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/example2/res/"]
        }
        flavor3 {
          java.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/java/"]
          manifest.srcFile "W:/android-studio-projects/sharedid/app/src/example3/AndroidManifest.xml"
          assets.srcDirs = ["W:/android-studio-projects/sharedid/app/src/example3/assets/"]
          resources.srcDirs = ["W:/android-studio-projects/sharedid/app/src/main/res/", "W:/android-studio-projects/sharedid/app/src/example3/res/"]
        }
    }
}
dependencies {
    api 'com.google.android.gms:play-services-maps:15.0.0'
    api 'com.google.android.gms:play-services-location:15.0.0'
    api 'com.android.support:appcompat-v7:27.1.1'
    api 'com.github.PhilJay:MPAndroidChart:v2.0.8'

}

...

当我进入构建|选择变体"时,我只能选择

When I got to "Build | Select variant" I can only select

模块:应用构建变体:flavor1Flavor2Flavor3Debug,flavor1Flavor2Flavor3Release

我很想得到

  • 以下构建变体:flavor1Debugflavor2Debugflavor3Debugflavor1Releaseflavor2Release,flavor3Release

我尝试过文件 | 将项目与 gradle 文件同步"

I have tried "File | Sync project with gradle file"

...

我收到此错误

Caused by: java.lang.RuntimeException: Cannot read packageName fromW:\android-studio-projects\sharedid\app\src\main\AndroidManifest.xml

Caused by: java.lang.RuntimeException: Cannot read packageName from W:\android-studio-projects\sharedid\app\src\main\AndroidManifest.xml

我都试过

  • 没有这样的文件(希望它采用产品风味?)
  • 让主"清单仅定义所有产品风味之间的共享内容

推荐答案

我觉得有两个不相关的问题:

I think there are two unrelated problems :

  • 目前您有 2 个构建类型(自动创建的 debugrelease)和 3 个维度(dim1dim2dim3),每个都有 1 个变体(flavor1 表示 dim1flavor2 表示 <代码>dim2, ...)这最多给:

  • Currently you have 2 build types (the automatically created debug and release) and 3 dimensions (dim1, dim2 and dim3), each one having 1 variant (flavor1 for dim1, flavor2 for dim2, ...) this gives at most :

2 * 1 * 1 * 1 = 2 种组合

2 * 1 * 1 * 1 = 2 combinations

您应该切换到 2 个构建类型和 1 个维度(比如 dim1),具有 3 个变体(flavor1flaver2flavor3)) 有:

You should switch to 2 build types and 1 dimension (say dim1) having 3 variants (flavor1, flaver2 and flavor3) to have :

2 * 3 = 6 个应用程序

2 * 3 = 6 apks

你应该有一个主清单.与其他资源不同,清单不是简单地覆盖而是从多个来源合并(参见 合并多个清单文件 了解更多详情).

You should have a main manifest. Unlike other resources the manifest is not simply overriden but merged from multiple sources (see Merge Multiple Manifest Files for more details).

它至少应该包含一个包名称(可能与最终的 applicationId(s) 不同),正如 配置产品风味 :

It should at least contains a package name (possibly different from the final applicationId(s)) as explained by this note from Configure Product Flavors :

注意:您仍然需要使用 package 属性指定包名称在 ma​​in/ 清单文件中.您还必须在你的源代码引用 R 类,或解决任何相关活动或服务注册.这允许您使用 applicationId为每种产品口味提供唯一的包装 ID 和分发,而无需更改您的源代码.

Note : You still need to specify a package name using the package attribute in the main/ manifest file. You must also use that package name in your source code to refer to the R class, or resolve any relative activity or service registration. This allows you to use applicationId to give each product flavor a unique ID for packaging and distribution, without having to change your source code.

这篇关于Android Studio 中的产品风味的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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