开发 Google play 应用内购买从零开始 —— Flutter 篇

Qiang 发布在技术 1

参考链接

开发环境

$ flutter doctor -v
[✓] Flutter (Channel stable, 2.5.3, on Mac OS X 10.15.7 19H2026 darwin-x64, locale zh-Hans-CN)
    • Flutter version 2.5.3 at /Users/me/Documents/bag/flutter/flutter_macos-stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 18116933e7 (1 year, 6 months ago), 2021-10-15 10:46:35 -0700
    • Engine revision d3ea636dc5
    • Dart version 2.14.4

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/me/Documents/bag/android/android-sdk-macosx
    • Platform android-33, build-tools 30.0.3
    • ANDROID_HOME = /Users/me/Documents/bag/android/android-sdk-macosx
    • ANDROID_SDK_ROOT = /Applications/Android Studio.app/sdk
    • Java binary at: /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/bin/java
    • Java version Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[!] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    ! CocoaPods 1.8.4 out of date (1.10.0 is recommended).
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin
        usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To upgrade see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (version 2022.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] VS Code (version 1.77.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.60.0

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 111.0.5563.146

前置条件

  1. 添加应用内购买权限:<uses-permission android:name="com.android.vending.BILLING" />
  2. 添加应用内购买 kotlin 库:implementation("com.android.billingclient:billing-ktx:5.1.0")

参考项目:Play billing samples - ClassyTaxiAppKotlin

两个前置条件,如果没有满足,无法开通应用内购买。
billing 库每两年强制更新一次,当前最新版本为 5.1.0,此版本对 Kotlin 也有要求,使用低版本的 Kotlin 无法编译通过(错误日志见以下 Log),可参考上面的项目使用的 Kotlin 版本:1.6.10(当前时间 04/06/2023)。

Log

运行日志

$ flutter build appbundle

💪 Building with sound null safety 💪

e: /Users/me/.gradle/caches/transforms-2/files-2.1/ec06db3fe695e0f0548202f6601d1e03/jetified-billing-ktx-5.1.0-api.jar!/META-INF/java.com.google.android.libraries.play.billing.public.ktbilling_granule.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.1.15.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileReleaseKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 47s
Running Gradle task 'bundleRelease'...                             48.2s
Gradle task bundleRelease failed with exit code 1

创建一个应用

在 Google play 上创建一个应用,并完成应用信息填写。

应用内商品 —— 在应用内销售一次性收费的商品

您可以选择销售耐用型商品,用户购买此类商品后可一直使用(例如付费内容的访问权限);也可以选择销售消耗型商品,此类商品在使用时会消耗掉,并且可重复购买(例如游戏币或额外的生命值)。了解详情

创建应用内商品

字段(* 表示必填):

然后保存。

启用应用内商品

保存好商品后,我们发现此时商品状态是“无效”。

image

我们点击页面下方的“启用”按钮,稍等片刻,商品状态已变成“有效”。

订阅 —— 按周期或以预付款形式出售内容或服务

您能以各种方式创建和出售订阅;配置自动续订型订阅、内含充值服务的预付费方案和各种订阅期;还能添加优惠,这有助于您赢得新订阅者或激励现有订阅者升级。了解详情

创建订阅内容

若要创建订阅项目,您需要提供产品 ID。此 ID 不能更改,也不能重复使用。您日后将能够配置更多详情。此信息不会与用户分享。 了解详情

字段(* 表示必填):

然后保存。

设置订阅

如要使订阅项目可供购买,请说明订阅福利,然后创建基础方案及相关优惠。

有以下几个内容:

添加一项基础方案

基础方案会指定特定时长的价格以及适用国家/地区。基础方案有自动续订和预付费两种类别。 每个基础方案可以拥有一个或多个优惠。优惠会向符合条件的用户提供折扣价格。您可以使用优惠吸引首次订阅者或激励现有订阅者升级。 了解详情

您需要至少添加并发布 1 项基础方案才能提供此订阅项目。您可以随时创建优惠和其他基础方案。了解详情

字段(* 表示必填):

然后启用,此时你的基础方案已从草稿状态变为有效状态。

TOP
前往 GitHub Discussion 评论