我正在构建一个应用程序,它需要一个包含 2 个变体的库 - 模拟器和实际。
我的目标是创建构建任务,例如 assembleSimulatorDebug 和 assembleDebug。 assembleSimulatorDebug 将包括模拟器模块/库,assembleDebug 将包括构建 apk 的实际模块。
我正在考虑在 build.gradle 的依赖部分中加入 if else block 。我可以要这样的东西吗?
我是 gradle 的新手,从昨天开始尝试设置这样的东西。如果任何人都可以提供一些提示或链接,我可以获得实现此目标的想法,那将会很有帮助。
最佳答案
更新: 找到了另一个解决方案。这是我的另一个答案。
这可以通过使用 android gradle 插件的“Flavor products”来实现。您可以按产品风格拆分库实现。
初始要求'com.android.tools.build:gradle:1.0.0',Android Studio 1.0.1。
我创建了 project at GitHub ,这样您就可以获取、运行和学习。
您还需要创建两种风格的应用和库。比如normal
和simulator
应用程序的build.gradle
apply plugin: 'com.android.application'
android {
lintOptions {
abortOnError false
}
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.tivogi.gradle"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
normal {
}
simulator {
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
normalCompile project(path: ':library', configuration: 'normalRelease')
simulatorCompile project(path: ':library', configuration: 'simulatorRelease')
}
库的 build.gradle
apply plugin: 'com.android.library'
android {
publishNonDefault true
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
normal {
}
simulator {
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
图书馆:
- 按
productFlavors
拆分不同口味的实现(如果对此有疑问,请参阅我的 sample project); 将
publishNonDefault true
添加到其build.gradle
It is also possible to publish all variants of a library. We are planning to allow this while using a normal project-to-project dependency (like shown above), but this is not possible right now due to limitations in Gradle (we are working toward fixing those as well). Publishing of all variants are not enabled by default. To enable them:
android {
publishNonDefault true
}
应用:
- 添加
普通
和模拟器
产品 flavor (productFlavors
); 将下一行添加到
dependencies
部分normalCompile project(path: ':library', configuration: 'normalRelease') simulatorCompile project(path: ':library', configuration: 'simulatorRelease')
To create a dependency on another published artifact, you need to specify which one to use:
dependencies {
flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
flavor2Compile project(path: ':lib1', configuration: 'flavor2Release')
}
完成所有这些之后,您将能够构建 2 个应用程序变体:普通应用程序和模拟器。
关于Android Studio - 构建变体和模块选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519128/