Android 安卓工作室-构建变体&;模块选择

Android 安卓工作室-构建变体&;模块选择,android,gradle,android-studio,android-gradle-plugin,Android,Gradle,Android Studio,Android Gradle Plugin,我正在构建一个应用程序,它需要一个包含两个变体的库-模拟器和实际 我的目标是创建构建任务,比如assembleSimulatorDebug&assembleDebug。 assembleSimulatorDebug将包括模拟器模块/库,assembleDebug将包括构建apk的实际模块 我正在考虑在build.gradle的依赖项部分使用if-else块。我可以要这样的吗 我对gradle很陌生,从昨天起我就开始尝试建立类似的东西。如果有人能提供一些提示或链接,我会很有帮助。更新: 找到了另一

我正在构建一个应用程序,它需要一个包含两个变体的库-模拟器和实际

我的目标是创建构建任务,比如assembleSimulatorDebug&assembleDebug。 assembleSimulatorDebug将包括模拟器模块/库,assembleDebug将包括构建apk的实际模块

我正在考虑在build.gradle的依赖项部分使用if-else块。我可以要这样的吗

我对gradle很陌生,从昨天起我就开始尝试建立类似的东西。如果有人能提供一些提示或链接,我会很有帮助。更新: 找到了另一个解决方案。这是我的另一个答案

使用android gradle插件的“风味产品”是可能的。您可以按产品风格划分库实现

初始需求'com.android.tools.build:gradle:1.0.0',android Studio 1.0.1

我创造的,所以你可以得到它,运行和学习

您还需要创建两种风格的应用程序和库。例如,
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')
}
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'
}
库的
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')
}
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
    (请参阅我的if,我会对此有疑问)在不同口味之间拆分实现
  • publishNonDefault true
    添加到其
    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')
    }
    
    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'
    }
    

    还可以发布库的所有变体。我们计划在使用正常的项目到项目依赖关系(如上图所示)时允许这样做,但由于Gradle的限制,现在不可能这样做(我们也在努力解决这些问题)。 默认情况下,不会启用所有变体的发布。 要启用它们:

    安卓{
    publishNonDefault true
    }

应用程序:

  • 添加
    normal
    simulator
    产品口味(
    productFlavors
  • 将下一行添加到
    依赖项
    部分

    normalCompile project(path: ':library', configuration: 'normalRelease')
    simulatorCompile project(path: ':library', configuration: 'simulatorRelease')
    

    要创建对另一个已发布工件的依赖关系,需要指定要使用的工件:

    依赖项{
    flavor1Compile项目(路径:':lib1',配置:'flavor1Release')
    flavor2Compile项目(路径:':lib1',配置:'flavor2Release')
    }

在所有这些之后,您将能够构建两种应用程序变体:普通和模拟器。

更新: 找到了另一个解决方案。这是我的另一个答案

使用android gradle插件的“风味产品”是可能的。您可以按产品风格划分库实现

初始需求'com.android.tools.build:gradle:1.0.0',android Studio 1.0.1

我创造的,所以你可以得到它,运行和学习

您还需要创建两种风格的应用程序和库。例如,
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')
}
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'
}
库的
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')
}
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
    (请参阅我的if,我会对此有疑问)在不同口味之间拆分实现
  • publishNonDefault true
    添加到其
    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')
    }
    
    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'
    }
    

    还可以发布库的所有变体。我们计划在使用正常的项目到项目依赖关系(如上图所示)时允许这样做,但由于Gradle的限制,现在不可能这样做(我们也在努力解决这些问题)。 默认情况下,不会启用所有变体的发布。 要启用它们:

    安卓{
    publishNonDefault true
    }

应用程序:

  • 添加
    normal
    simulator
    产品口味(
    productFlavors
  • 将下一行添加到
    依赖项
    部分

    normalCompile project(path: ':library', configuration: 'normalRelease')
    simulatorCompile project(path: ':library', configuration: 'simulatorRelease')
    

    要创建对另一个已发布工件的依赖关系,需要指定要使用的工件:

    依赖项{
    flavor1Compile项目(路径:':lib1',配置:'flavor1Release')
    flavor2Compile项目(路径:':lib1',配置:'flavor2Release')
    }


在所有这些之后,您将能够构建两种应用程序变体:普通和模拟器。

找到了两个库的解决方案,看起来更简单。它是基于下一个来自

最后,与构建类型一样,产品风格也可以有自己的依赖关系。例如,如果这些风格用于生成基于广告的应用程序和付费应用程序,则其中一种风格可能依赖于ads SDK,而另一种则不依赖。

依赖项{
风味1组合“…”
}

因此,您可以创建两个单独的库和两种风格,例如,库是
library normal
library simulator
,风格是
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(":library-normal")
    simulatorCompile project(":library-simulator")
}
应用程序:

  • 仅将这些行添加到
    依赖项中

    normalCompile project(":library-normal")
    simulatorCompile project(":library-simulator")
    
库:

  • 不需要任何更改

我在branch发布了这个解决方案的示例项目。

找到了两个库的解决方案,看起来更简单。它是基于下一个来自

最后,与构建类型一样,产品风格也可以有自己的依赖关系。例如,如果这些风格用于生成基于广告的应用程序和付费应用程序,则其中一种风格可能依赖于ads SDK,而另一种则不依赖。

依赖项{
风味1组合“…”
}

因此,您可以创建两个单独的库和两种风格,例如,库是
librarynormal
librarysimulator