繁体   English   中英

Android Studio:如何在产品风格中排除google-services模块?

[英]Android Studio: How to exclude google-services module in product flavor?

在我的Android项目中,有几种产品风格:

buildTypes {
    release {}
    debug {}
    staging {}
}

productFlavors {
    freeVersion {}
    proVersion {}
    partnerVersion {}
}

另外,我使用Google Analytics:

apply plugin: 'com.google.gms.google-services'

dependencies {
    compile 'com.google.android.gms:play-services-analytics:8.4.0'
}

如何在其中一个中排除google-services? 例如,在:

freeVersion {}

请注意freeCompile的使用并声明可变flavor以有条件地应用插件。

apply plugin: 'com.android.application'

def flavor

android {

    ....

    ....

    productFlavors {
        free {
            applicationId "com.anandbibek.builditbigger.free"
            flavor = "free"
        }
        paid {
            applicationId "com.anandbibek.builditbigger.paid"
            flavor = "paid"
        }
    }
}

dependencies {

    // Added for AdMob
    freeCompile 'com.google.firebase:firebase-ads:9.6.1'

    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.google.code.findbugs:jsr305:2.0.1'

}
if(flavor == "free") {
    apply plugin: 'com.google.gms.google-services'
}

确保将google-services.json文件放在特定于flavor的文件夹中。 在我的情况下,我把它放在app/src/free 当您在主项目gradle文件中使用classpath 'com.google.gms:google-services:3.0.0'时,此工具可用。

另一种解决方案是禁用google-services插件添加的任务 - 这里我启用任务,如果flavorName不是“freeVersion”,但是这个逻辑可以清楚地更新,以便更改为看看变体buildType。

apply plugin: 'com.google.gms.google-services'

// must be after the plugin is applied otherwise no tasks will be found
android.applicationVariants.all { variant ->
    def googleTask = tasks.findByName("process${variant.name.capitalize()}GoogleServices")
    googleTask.enabled = !"freeVersion".equals(variant.flavorName)
}

我最初误解了这个问题。 要排除免费版本,您将使用proVersionCompile和partnerVersionCompile以及所需的依赖项来排除freeVersion。

dependencies {
    proVersionCompile 'com.google.android.gms:play-services-analytics:8.4.0'
    partnerVersionCompile 'com.google.android.gms:play-services-analytics:8.4.0'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM