如何在口味之间使用不同的包名?

Fel*_*ier 16 merge android manifest android-flavors

我正在尝试创建一个有两种风格的单一项目:free和pro(版本).

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

这是我的build.gradle:

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

   pro {

   }
}
Run Code Online (Sandbox Code Playgroud)

这是我的清单文件:

<?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>
Run Code Online (Sandbox Code Playgroud)

我尝试免费上传构建apk和专业版.专业味道还可以,但谷歌不接受免费的味道,因为包装不正确.我怎么解决这个问题?

======解决:====== applicationIdSuffix仅适用于buildTypes.

Vee*_*ner 22

使用新的Android Gradle构建系统,您可以轻松构建多个不同版本的应用程序; 例如,您可以构建应用程序的"免费"版本和"专业版"(使用版本),这些版本应该在Google Play商店中有不同的软件包,以便可以单独安装和购买,两者都安装在同时,等等.同样,您也可以构建应用程序的"调试"和"alpha"和"beta"版本(使用构建类型),这些也可以同样有助于唯一的包名称.

应用程序/的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"
    }
...
Run Code Online (Sandbox Code Playgroud)

应用程序/的build.gradle:

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

buildTypes {
    debug {
        applicationIdSuffix ".debug"
    }
}
....
Run Code Online (Sandbox Code Playgroud)

来自Android Studio Project Site - ApplicationId与PackageName

  • 您没有回答我的问题:我尝试免费和专业版上传构建APK。Pro风格还可以,但免费包装不被Google接受,因为包装不正确。我该如何解决? (2认同)
  • 他向您展示了如何做到这一点,您只需要在风味定义中为应用程序使用适当的包名称即可。 (2认同)

Ben*_*nny 9

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

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

buildTypes {
  debug {
      applicationIdSuffix ".debug"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

ERROR: No matching client found for package name "com.example.appname.debug"
Run Code Online (Sandbox Code Playgroud)

要修复此问题,您需要在 Firebase 中注册新的包名称并下载google-services.json包含所有现有包名称的新包名称。如果没有,您将收到编译错误,指出未找到新包名称的客户端。


小智 7

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"
    }
}
Run Code Online (Sandbox Code Playgroud)