Using a different manifestPlaceholder for each Build Variant

前端 未结 6 536
长发绾君心
长发绾君心 2020-12-09 03:01

I will start by saying that I am very new to Gradle, so I apologize if this has already been answered.

I\'m working on an Android application that uses an API key to

6条回答
  •  盖世英雄少女心
    2020-12-09 03:52

    I would guess that you are referring to Fabric ApiKey? :) I just spent hours trying to do it in a similar way with the placeholders and specifying the ApiKey in the gradle file although it does not seem possible as of com.android.tools.build:gradle:1.3.1. It is possible to specify a placeholder for a specific flavor but not for a flavor AND buildType.

    Just to correct your syntax, the way you would have to do it (if it was possible) would be something like that but manifestPlaceholders are unknown to variants.

    applicationVariants.all{ variant->
        if (variant.productFlavors.get(0).name.equals("someFlavor")) {
            if (variant.buildType.name.equals("release")) {
                manifestPlaceholders = [ apiKey:"RELEASE_KEY_1" ]
            } else {
                manifestPlaceholders = [ apiKey:"DEBUG_KEY" ]
            }
        } else {
            if (variant.buildType.name.equals("release")) {
                manifestPlaceholders = [ apiKey:"RELEASE_KEY_2" ]
            } else {
                manifestPlaceholders = [ apiKey:"DEBUG_KEY" ]
            }    
        }
    }
    

    What you actually need to do is to keep the key in the AndroidManifest.xml and handle it with multiple manifest file

    src/AndroidManifest.xml

    
        
            
            
            
    
    

    src/someFlavorRelease/AndroidManifest.xml

    
        
            
            
            
    
    

    src/someOtherFlavorRelease/AndroidManifest.xml

    
        
            
            
            
    
    

    The manifestMerger will handle the replacement and you will end up with the proper key in every scenario. I just implemented it successfully. I just hope you were really referring to the Fabric key! :)

    Hope this helps!

提交回复
热议问题