Android Studio中的产品口味 [英] Product flavors in 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
模块: app 构建版本: flavor1Flavor2Flavor3Debug , flavor1Flavor2Flavor3Release
Module:app Build Variant:flavor1Flavor2Flavor3Debug,flavor1Flavor2Flavor3Release
我希望得到
- 以下构建变体: flavor1Debug , flavor2Debug , flavor3Debug , flavor1Release , flavor2Release , flavor3Release
- the following build variants: flavor1Debug,flavor2Debug,flavor3Debug,flavor1Release,flavor2Release,flavor3Release
我尝试过文件|用gradle文件同步项目"
I have tried "File | Sync project with gradle file"
...
我收到此错误
由以下原因引起:java.lang.RuntimeException:无法从以下位置读取packageName W:\ 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种构建类型(自动创建的
debug
和release
)和3种尺寸(dim1
,dim2
和dim3
),每种都有1种变体(flavor1
对于dim1
,flavor2
对于dim2
,...) 这样最多:
Currently you have 2 build types (the automatically created
debug
andrelease
) and 3 dimensions (dim1
,dim2
anddim3
), each one having 1 variant (flavor1
fordim1
,flavor2
fordim2
, ...) this gives at most :
2 * 1 * 1 * 1 = 2个组合
2 * 1 * 1 * 1 = 2 combinations
您应该切换到2种构建类型和1维(例如dim1
),其中包含3种变体(flavor1
,flaver2
和flavor3
):
You should switch to 2 build types and 1 dimension (say dim1
) having 3 variants (flavor1
, flaver2
and flavor3
) to have :
2 * 3 = 6个apks
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不同),如
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 属性指定软件包名称 在 main/清单文件中.您还必须在 您的源代码引用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屋!