如何在不同的产品风味中使用不同的包名?

43

我想创建一个带有两种版本的单一项目:免费版和专业版。

我的应用程序已经在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包。专业版可以,但是Google不接受免费版,因为包名不正确。我该如何解决这个问题?

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


6
我之前不知道applicationIdSuffix也可以用在flavor上。使用applicationId来明确指定你想要在flavor中使用的应用ID。请注意,这并不改变原意。 - CommonsWare
@CommonsWare,非常好的回答!applicationIdSuffix只能与buildTypes一起使用!我使用了applicationId,它完美地工作了! :) - Felipe Porge Xavier
3个回答

38

使用新的Android Gradle构建系统,您可以轻松构建多个不同版本的应用程序。例如,您可以使用flavor构建应用程序的“免费”和“专业”版本,并且这些版本应该具有不同的包名,以便它们可以在Google Play商店中分别安装和购买,同时安装等。类似地,您还可以使用build types构建应用程序的“调试”、“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项目站点——ApplicationId与PackageName


3
你没有回答我的问题:我试图上传免费和专业版的构建APK文件。专业版可以,但免费版本因包格式不正确而无法被谷歌接受。我该如何解决这个问题? - Felipe Porge Xavier
2
他向你展示了如何做到这一点,你只需要在口味定义中使用适当的软件包名称即可。 - nasch

8
除了基本的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文件,其中包含所有现有的包名。否则,您将会遇到编译错误,提示新包名没有找到客户端。

1
非常感谢。我最初只在Firebase中添加了开发应用程序,但是出现了此错误。将所有应用程序风格都添加到Firebase中,并使用更新的google-services.json文件解决了这个问题。 - John Doe

8
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"
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接