【问题标题】:How to use different package names between flavors?如何在口味之间使用不同的包名?
【发布时间】:2017-01-25 22:16:57
【问题描述】:

我正在尝试创建具有 2 种风格的单个项目:免费和专业(版本)。

我的应用已经在 PlayStore 中,包含不同的包(例如:com.example.appfree 和 com.example.app)

这是我的 build.gradle:

defaultConfig {
   applicationId "com.example.appfree"
}
productFlavors{
   lite {
       applicationIdSuffix 'lite'
   }

   pro {

   }
}

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".SplashScreenActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity"/>

    </application>

</manifest>

我尝试上传免费和专业版的构建 apk。专业版还可以,但免费版不被谷歌接受,因为包装不正确。我该如何解决这个问题?

====== 已解决:====== applicationIdSuffix 仅适用于 buildTypes。

【问题讨论】:

  • 我不知道applicationIdSuffix 对口味有用。使用 applicationId 拼出您要用于风味的确切应用程序 ID。
  • @CommonsWare,很好的答案! applicationIdSuffix 仅适用于 buildTypes!我使用了 applicationId,它运行良好! :)

标签: android merge manifest android-flavors


【解决方案1】:

借助新的 Android Gradle 构建系统,您可以轻松构建应用的多个不同版本;例如,您可以构建应用程序的“免费”版本和“专业”版本(使用风味),并且它们应该在 Google Play 商店中具有不同的软件包,以便可以单独安装和购买,两者都安装在同时,以此类推。同样,您也可以构建应用的“调试”、“alpha”和“beta”版本(使用构建类型),这些也可以类似地为唯一的包名称做出贡献。

app/build.gradle:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 19
    buildToolsVersion "19.1"
    defaultConfig {
        applicationId "com.example.my.app"
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
...

app/build.gradle:

productFlavors {
    pro {
        applicationId = "com.example.my.pkg.pro"
    }
    free {
        applicationId = "com.example.my.pkg.free"
    }
}

buildTypes {
    debug {
        applicationIdSuffix ".debug"
    }
}
....

来自Android Studio Project Site - ApplicationId versus PackageName

【讨论】:

  • 您没有回答我的问题:我尝试上传免费和专业版的构建 apk。专业版还可以,但免费版不被谷歌接受,因为包装不正确。我该如何解决这个问题?
  • 他向您展示了如何做到这一点,您只需在风味定义中为您的应用使用适当的包名称。
【解决方案2】:
flavorDimensions "version"
productFlavors {
    demo {
        // Assigns this product flavor to the "version" flavor dimension.
        // This property is optional if you are using only one dimension.
        dimension "version"
        applicationIdSuffix ".demo"
        versionNameSuffix "-demo"
    }
    full {
        dimension "version"
        applicationIdSuffix ".full"
        versionNameSuffix "-full"
    }
}

【讨论】:

    【解决方案3】:

    除了基本的 gradle.build(应用程序级别)更改。请注意,您只需要添加您需要的配置,而不是所有这些:

    productFlavors {
      enterprise {
          applicationId = "com.example.appname.enterprise"
      }
      consumer {
          applicationId = "com.example.appname.consumer"
      }
    }
    
    buildTypes {
      debug {
          applicationIdSuffix ".debug"
      }
    }
    

    我想补充一点,如果您使用的是 Firebase,并且您需要有一个 google-services.json 文件。您将看到以下内容:

    ERROR: No matching client found for package name "com.example.appname.debug"
    

    要修复它,您需要在 Firebase 中注册新的包名称并下载新的 google-services.json,它将包含您现有的所有包名称。如果没有,您将收到一个编译错误,提示找不到新包名称的客户端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      相关资源
      最近更新 更多