rc-configuring-the-sdk
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseConfiguring the RevenueCat Android SDK
配置RevenueCat Android SDK
Use this skill to add and configure RevenueCat () in an Android app. A single call replaces setup, connection lifecycle management, reconnection handling, and launch-time purchase queries.
purchases-ktPurchases.configureBillingClientWork through the phases in order. The full chapter on revenuecat.com has the complete reference text.
当你需要在Android应用中添加和配置RevenueCat()时使用本技能。单次调用即可替代的初始化、连接生命周期管理、重连处理以及启动时的购买查询操作。
purchases-ktPurchases.configureBillingClient请按顺序完成以下阶段。revenuecat.com上的完整章节包含完整的参考内容。
Phase 1: Discovery
阶段1:排查
Find out whether RevenueCat is already wired up and where billing code currently lives.
-
Search for an existing configure call and prior SDK version.bash
rg -n "Purchases\.configure|PurchasesConfiguration" --type kotlin --type java rg -n "com\.revenuecat\.purchases" -g '*.gradle*' -g '*.toml' -
Search for the existing billing surface you may be replacing.bash
rg -n "BillingClient|PurchasesUpdatedListener|startConnection" --type kotlin --type java -
Record answers before moving on.
| Question | Where to look |
|---|---|
Is | App startup, |
| Which module owns billing today? | |
| Is there an auth system with a stable user id? | Login flow, session store |
| Debug vs release build detection available? | |
If already runs, stop and confirm with the user before changing it. Configuration runs once per process.
Purchases.configure确认RevenueCat是否已集成,以及当前计费代码的位置。
-
搜索现有的configure调用和之前的SDK版本。bash
rg -n "Purchases\.configure|PurchasesConfiguration" --type kotlin --type java rg -n "com\.revenuecat\.purchases" -g '*.gradle*' -g '*.toml' -
搜索可能需要替换的现有计费相关代码。bash
rg -n "BillingClient|PurchasesUpdatedListener|startConnection" --type kotlin --type java -
在进行下一步前记录答案。
| 问题 | 查看位置 |
|---|---|
是否已调用 | 应用启动处、 |
| 当前哪个模块负责计费? | |
| 是否存在带有稳定用户ID的认证系统? | 登录流程、会话存储 |
| 是否支持区分Debug和Release构建? | |
如果已在运行,修改前请先与用户确认。配置每个进程仅需运行一次。
Purchases.configurePhase 2: Plan
阶段2:规划
Decide three things before writing code.
编写代码前需确定三件事。
2.1 Where to call configure
2.1 确定调用configure的位置
| Location | When to choose |
|---|---|
| Default. Ensures the SDK is ready before any |
First | Only if the app has no custom |
Call exactly once per process. Calling it from an risks re-running it on configuration changes if you are not careful; the path avoids that.
configureActivityApplication| 位置 | 选择时机 |
|---|---|
| 默认选项。确保在任何 |
首个 | 仅当应用没有自定义 |
每个进程需恰好调用一次。若从中调用,若处理不当可能会在配置变更时重复运行;通过调用可避免此问题。
configureActivityApplication2.2 App user id strategy
2.2 应用用户ID策略
| Strategy | Pass to | Use when |
|---|---|---|
| Anonymous | | Users can purchase before signing in, or you have no auth. Later call |
| Known user | Your stable backend id (for example | Users are always authenticated before purchase. |
Do not pass device ids, email addresses, or values that can change. If you do not have the id at startup, configure anonymously and call after authentication.
logIn| 策略 | 传入 | 使用场景 |
|---|---|---|
| 匿名用户 | | 用户可在登录前进行购买,或应用无认证系统。后续可调用 |
| 已知用户 | 你的后端稳定ID(例如 | 用户始终在认证后进行购买。 |
请勿传入设备ID、电子邮件地址或可能变更的值。若启动时无法获取用户ID,可先以匿名方式配置,认证完成后调用。
logIn2.3 Log level
2.3 日志级别
| Build | Log level |
|---|---|
| Debug | |
| Release | |
Switch on so production builds stay quiet.
BuildConfig.DEBUG| 构建类型 | 日志级别 |
|---|---|
| Debug | 集成期间使用 |
| Release | 使用 |
通过控制日志级别,确保生产构建日志保持简洁。
BuildConfig.DEBUGPhase 3: Execute
阶段3:实施
3.1 Add the Gradle dependency
3.1 添加Gradle依赖
In the app module :
build.gradle.ktskotlin
dependencies {
implementation("com.revenuecat.purchases:purchases:<latest>")
}Replace with the current published version. If the project uses a Groovy , use . Sync Gradle after the change.
<latest>build.gradleimplementation 'com.revenuecat.purchases:purchases:<latest>'在应用模块的中:
build.gradle.ktskotlin
dependencies {
implementation("com.revenuecat.purchases:purchases:<latest>")
}将替换为当前发布的最新版本。若项目使用Groovy版,则使用。修改后同步Gradle。
<latest>build.gradleimplementation 'com.revenuecat.purchases:purchases:<latest>'3.2 Call configure at startup
3.2 在启动时调用configure
In your subclass:
Applicationkotlin
class App : Application() {
override fun onCreate() {
super.onCreate()
Purchases.logLevel =
if (BuildConfig.DEBUG) LogLevel.DEBUG else LogLevel.INFO
Purchases.configure(
PurchasesConfiguration.Builder(this, BuildConfig.RC_API_KEY)
.appUserID(null) // or your stable user id
.build()
)
}
}Register the class in with on . Keep the public Android SDK key out of source; inject it through or a secret manager.
AndroidManifest.xmlandroid:name=".App"<application>BuildConfig在你的子类中:
Applicationkotlin
class App : Application() {
override fun onCreate() {
super.onCreate()
Purchases.logLevel =
if (BuildConfig.DEBUG) LogLevel.DEBUG else LogLevel.INFO
Purchases.configure(
PurchasesConfiguration.Builder(this, BuildConfig.RC_API_KEY)
.appUserID(null) // 或你的稳定用户ID
.build()
)
}
}在的标签中通过注册该类。请勿将公开的Android SDK密钥存入源码;通过或密钥管理器注入。
AndroidManifest.xml<application>android:name=".App"BuildConfig3.3 Confirm the wiring with a minimal offerings fetch
3.3 通过获取最小化产品包确认集成成功
From any coroutine scope after has run:
configurekotlin
lifecycleScope.launch {
val offerings = Purchases.sharedInstance.awaitOfferings()
Log.d("RC", "current=${offerings.current?.identifier}")
}A non-null identifier means the API key, package name, and network path are working. If you see , check the dashboard offering setup and the package name match before touching code.
offerings.currentnull在运行后的任何协程作用域中执行:
configurekotlin
lifecycleScope.launch {
val offerings = Purchases.sharedInstance.awaitOfferings()
Log.d("RC", "current=${offerings.current?.identifier}")
}若不为空,说明API密钥、包名和网络路径均正常工作。若返回,请先检查控制台产品包设置与包名是否匹配,再修改代码。
offerings.current?.identifiernullWhat the SDK handles for you
SDK为你处理的事项
Once configured, you do not need to:
- Create or close a
BillingClient - Call or
startConnectionendConnection - Check or write reconnection logic for
isReadySERVICE_DISCONNECTED - Call at launch; the SDK posts unfinished transactions automatically
queryPurchasesAsync
配置完成后,你无需再:
- 创建或关闭
BillingClient - 调用或
startConnectionendConnection - 检查或编写
isReady的重连逻辑SERVICE_DISCONNECTED - 启动时调用;SDK会自动处理未完成的交易
queryPurchasesAsync
Common pitfalls
常见问题
| Symptom | Likely cause |
|---|---|
| Code touched |
Offerings always | Package name mismatch between Play Console and dashboard, or offering not marked current. |
| Duplicate anonymous users | |
| Verbose logs in production | Log level not gated on |
| 症状 | 可能原因 |
|---|---|
| 在 |
产品包始终返回 | Play控制台与后台的包名不匹配,或产品包未标记为当前可用。 |
| 重复的匿名用户 | 每个进程多次调用 |
| 生产环境日志过于冗长 | 日志级别未通过 |