Android Studio - 构建变体和模块选择

标签 android gradle android-studio android-gradle-plugin

我正在构建一个应用程序,它需要一个包含 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 ,这样您就可以获取、运行和学习。

您还需要创建两种风格的应用和库。比如normalsimulator

应用程序的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

    来自 Gradle Plugin User Guide

    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')
    

    来自 Gradle Plugin User Guide

    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/

相关文章:

android - 将 iOS 应用程序移植到 Android

gradle - 构建错误 netflix fenzo 库

java - switch 语句无法识别

android - 部署到 App Engine 问题 - 您必须登录才能执行此操作

java - longclick时, ListView 中的删除按钮项目出现在另一个位置

android - 致命异常 : java. lang.IllegalStateException:onSaveInstanceState 后无法执行此操作

android - 如何获取安卓手机用户的谷歌账户名?

android - Gradle Android项目构建

git - Intellij 导入问题 - java.io.IOException : Cannot run program "git": error=2, 没有这样的文件或目录

android - 为什么使用 GlobalScope 需要 @DelicateCoroutinesApi