How do I set my Android app package name based on

2019-04-09 08:54发布

I'm building my Android app with the Gradle plugin. I'm using the flavors feature to create four different variants. At the moment, it's a single dimension with four values, but it would be more logical to set it up as two dimensions with two values each. I've got that building, but this is where I run into trouble.

I need each of the four variants to have a different package name. The first three are easy since I have a default and each dimension can override the package, but I need to set a package name for when both non-default dimensions are in play.

flavorDimensions "foo", "bar"
productFlavors {
    boring.flavorDimension "foo"
    charm {
        flavorDimension "foo"
        packageName "com.example.charm"
    }
    strange {
        flavorDimension "bar"
        packageName "com.example.strange"
    }
    // This is the idea of what I want, but it doesn't work because there
    // must be only a single dimension specified here.
    charmStrange {
        flavorDimension "foo", "bar"
        packageName "com.example.charminglystrange"
    }
}

I tried setting it after declaration by looking up the composed flavor variant, but I didn't have much luck. I'm not very familiar with Gradle, so I'm sure there's trickery I haven't employed. Alternately, maybe I could specify the package name in src/charmStrange/AndroidManifest.xml and let the merge sort it out? That seems like it could cause problems in the future.

3条回答
Rolldiameter
2楼-- · 2019-04-09 09:20

I had a similar question this answer isn't tested in my case I was appending the product flavor to the app id set by the other dimension, using your example it would append .strange to package com.example.charm

android.applicationVariants.all { variant ->
    def flavorString = variant.getVariantData().getVariantConfiguration().getFlavorName()
    def mergedFlavour = variant.getVariantData().getVariantConfiguration().getMergedFlavor();
    def appId = variant.getVariantData().getVariantConfiguration().getApplicationId();

    if(flavorString.contains("Strange")) {
        mergedFlavour.setApplicationId(appId + ".strange")
    }


}

but in your case you want a full package name so something like

android.applicationVariants.all { variant ->
    def flavorString = variant.getVariantData().getVariantConfiguration().getFlavorName()
    def mergedFlavour = variant.getVariantData().getVariantConfiguration().getMergedFlavor();
    def appId = variant.getVariantData().getVariantConfiguration().getApplicationId();

    if(flavorString.contains("charmStrange")) {
        mergedFlavour.setApplicationId("com.example.charminglystrange)
    }


}

charmStrange might need to be CharmStrange, try playing with it

查看更多
【Aperson】
3楼-- · 2019-04-09 09:39

You will want to set the applicationId, not the packageName:

    productFlavors {
        boring.flavorDimension "foo"
        charm {
            flavorDimension "foo"
            applicationId "com.example.charm"
        }
        strange {
            flavorDimension "bar"
            applicationId "com.example.strange"
        }
        // This is the idea of what I want, but it doesn't work because there
        // must be only a single dimension specified here.
        charmStrange {
            flavorDimension "foo", "bar"
            packageName "com.example.charminglystrange"
        }
    }
查看更多
一纸荒年 Trace。
4楼-- · 2019-04-09 09:40

You can now set applicationIdSuffix for productFlavors:

android {  
     productFlavors {  
          free {   
               applicationIdSuffix = ".free"  
          }  
          prod {

          } 
     }  
} 

Source: http://android-developers.blogspot.ie/2015/12/leveraging-product-flavors-in-android.html

查看更多
登录 后发表回答