Google Android Studio 中 Android 应用的 Play 许可
Google Play Licencing for an Android app in Android Studio
我正在尝试为 Android studio 中用 Kotlin 编写的应用程序设置 Google Play 许可。我的目标是避免用户在未通过商店购买我的应用程序的情况下共享 APK 文件。
我尝试过的:
我试过关注他们的documentation。这不是很有用。它跳过了许多细节,它并不是真正的教程。我无法使用它。
我看过 this 问题,它确实有一个长而详细的类似教程的答案。但答案似乎早已过时。它会导致大量警告并以异常“Intent must be explicit”终止。
我的问题概括起来是:
如何通过 Google 设置许可证检查,以便没有通过商店购买该应用程序的人无法安装它。这似乎是一件很常见的事情,尽管我找不到很多合适的答案。
以下是我在 2020 年的工作方式:
- 打开 Android Studio。
- 单击工具 -> SDK 管理器
- 切换到 SDK 工具 选项卡
- 确保 Google Play 许可库 已安装。如果未安装,请单击复选标记并单击 应用。
- 在该屏幕上您可以看到 Android SDK 位置 。复制该路径:
- 单击文件 -> 新建 -> 导入模块...:
- 粘贴您复制的路径并单击文本输入行右侧的小文件夹图标:
- 点击Android\Sdk\extras\google\market_licensing\library然后点击确定:
- 点击下一步:
- 勾选所有内容并单击 完成:
- 现在你的项目中应该有一个
library
文件夹:
- 右键单击
app
,然后单击 打开模块设置:
- 单击依赖项:
- 单击加号按钮并选择 3 模块依赖关系:
- 选中
library
并单击确定:
- 再次单击确定并等待同步。
- 如果出现错误
The minSdk version should not be declared in the android manifest file. You can move the version from the manifest to the defaultConfig in the build.gradle file.
转到 library > manifests > AndroidManifest.xml 并删除行 <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="15" />
.
- 转到 Gradle 脚本 > build.gradle(模块:库):
- 将
minSdkVersion
更改为 4,并根据需要更改 compileSdkVersion
、buildToolsVersion
和 targetSdkVersion
,然后单击 Sync Now
:
- 现在库已准备就绪,我们需要实际执行许可证检查。转到
MainActivity.kt
。
- 您需要找到您的 Base 64 public 密钥并生成盐,如 this 答案所示。我将引用该答案的必要部分,但将代码翻译成 Kotlin:
1.1 Your Base64 unique application key
How to get it:
a. Go to your developer console. Link.
b. If you haven't already created an application draft for your app, do it now.
c. Once you have created the draft, it is a good idea to upload your
.apk
as Alpha or Beta. Leave it unpublished.
d. Click Services & APIs
e. Scroll down and find YOUR LICENSE KEY FOR THIS APPLICATION
f. Copy the key into your app like this:
private const val BASE64_PUBLIC_KEY = "YOUR LICENSE KEY FOR THIS APPLICATION";
Make sure that there are no spaces.
1.2 A salt
a. What is a salt?
A salt is random data that is additional input when hashing a
password. They are used to defend against dictionary attacks and
rainbow table attacks.
b. How do I get one?
This is a good link to generate a random salt. There should be exactly
20 random integers, so put 20
in for the amount of random strings to
generate, each string should be 2
characters long (used for this
example, it doesn't have to be). Check numeric digits, and check
Identical strings are allowed. They can be negative numbers too. Try
to remove any redundancy, e.g. 00 -> 0
, for the sake of consistency.
c. Where do I put the salt?
When declaring variables just put this code in, except with your
random salt.
private val SALT = byteArrayOf(YOUR RANDOM SALT COMMA SEPARATED 20 INTEGERS)
- 步骤 21 中的变量应添加到您的主 activity class 中。现在,您应该向主 activity 添加一些代码。它大致应该是这样的(注意
// TODO
评论):
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.vending.licensing.*
import kotlin.system.exitProcess
class MainActivity : AppCompatActivity()
{
companion object
{
private const val BASE64_PUBLIC_KEY = "YOUR LICENSE KEY FOR THIS APPLICATION" // TODO replace with your own key
private val SALT = byteArrayOf(YOUR RANDOM SALT COMMA SEPARATED 20 INTEGERS) // TODO replace with your own salt
}
private val deviceId: String by lazy {
Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
}
private lateinit var licenseCheckerCallback: LicenseCheckerCallback
private lateinit var checker: LicenseChecker
private fun doCheck()
{
checker.checkAccess(licenseCheckerCallback)
}
override fun onDestroy()
{
super.onDestroy()
checker.onDestroy()
}
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
// Construct the LicenseCheckerCallback. The library calls this when done.
licenseCheckerCallback = MyLicenseCheckerCallback()
// Construct the LicenseChecker with a Policy.
checker = LicenseChecker(
this,
ServerManagedPolicy(this, AESObfuscator(SALT, packageName, deviceId)),
BASE64_PUBLIC_KEY // Your public licensing key.
)
doCheck()
setContentView(R.layout.activity_main) // TODO Replace with your own layout
}
private fun displayResult(result: String)
{
// TODO you can change this how the info is displayed
Toast.makeText(this, result, Toast.LENGTH_SHORT).show()
}
private inner class MyLicenseCheckerCallback : LicenseCheckerCallback
{
override fun allow(reason: Int)
{
if (isFinishing)
{
// Don't update UI if Activity is finishing.
return
}
// Should allow user access.
}
override fun applicationError(errorCode: Int)
{
// TODO handle the error your own way. Calling `dontAllow` is common.
dontAllow(Policy.NOT_LICENSED)
}
override fun dontAllow(reason: Int)
{
if (isFinishing)
{
// Don't update UI if Activity is finishing.
return
}
if (reason == Policy.RETRY)
{
// If the reason received from the policy is RETRY, it was probably
// due to a loss of connection with the service, so we should give the
// user a chance to retry. So show a dialog to retry.
// TODO handle Policy.RETRY
}
else
{
// Otherwise, the user isn't licensed to use this app.
// Your response should always inform the user that the application
// isn't licensed, but your behavior at that point can vary. You might
// provide the user a limited access version of your app or you can
// take them to Google Play to purchase the app.
// TODO implement goto market
}
displayResult("Not Licensed")
// TODO you may not abort if you have some other way to handle the fail case
abort()
}
}
private fun abort()
{
finishAffinity()
exitProcess(0)
}
}
- 将这些权限添加到您的清单文件:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.vending.CHECK_LICENSE"/>
- 如果您收到异常消息类似于:
Service Intent must be explicit: Intent { act=com.android.vending.licensing.ILicensingService }
应用 this 答案中的修复程序。
- 应该就这些了。有关详细信息,请参阅我之前引用的 answer。我希望这能为其他人节省一些时间。
我正在尝试为 Android studio 中用 Kotlin 编写的应用程序设置 Google Play 许可。我的目标是避免用户在未通过商店购买我的应用程序的情况下共享 APK 文件。
我尝试过的:
我试过关注他们的documentation。这不是很有用。它跳过了许多细节,它并不是真正的教程。我无法使用它。
我看过 this 问题,它确实有一个长而详细的类似教程的答案。但答案似乎早已过时。它会导致大量警告并以异常“Intent must be explicit”终止。
我的问题概括起来是:
如何通过 Google 设置许可证检查,以便没有通过商店购买该应用程序的人无法安装它。这似乎是一件很常见的事情,尽管我找不到很多合适的答案。
以下是我在 2020 年的工作方式:
- 打开 Android Studio。
- 单击工具 -> SDK 管理器
- 切换到 SDK 工具 选项卡
- 确保 Google Play 许可库 已安装。如果未安装,请单击复选标记并单击 应用。
- 在该屏幕上您可以看到 Android SDK 位置 。复制该路径:
- 单击文件 -> 新建 -> 导入模块...:
- 粘贴您复制的路径并单击文本输入行右侧的小文件夹图标:
- 点击Android\Sdk\extras\google\market_licensing\library然后点击确定:
- 点击下一步:
- 勾选所有内容并单击 完成:
- 现在你的项目中应该有一个
library
文件夹:
- 右键单击
app
,然后单击 打开模块设置:
- 单击依赖项:
- 单击加号按钮并选择 3 模块依赖关系:
- 选中
library
并单击确定:
- 再次单击确定并等待同步。
- 如果出现错误
The minSdk version should not be declared in the android manifest file. You can move the version from the manifest to the defaultConfig in the build.gradle file.
转到 library > manifests > AndroidManifest.xml 并删除行 <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="15" />
.
- 转到 Gradle 脚本 > build.gradle(模块:库):
- 将
minSdkVersion
更改为 4,并根据需要更改compileSdkVersion
、buildToolsVersion
和targetSdkVersion
,然后单击Sync Now
:
- 现在库已准备就绪,我们需要实际执行许可证检查。转到
MainActivity.kt
。
- 您需要找到您的 Base 64 public 密钥并生成盐,如 this 答案所示。我将引用该答案的必要部分,但将代码翻译成 Kotlin:
1.1 Your Base64 unique application key
How to get it:
a. Go to your developer console. Link.
b. If you haven't already created an application draft for your app, do it now.
c. Once you have created the draft, it is a good idea to upload your
.apk
as Alpha or Beta. Leave it unpublished.d. Click
Services & APIs
e. Scroll down and find
YOUR LICENSE KEY FOR THIS APPLICATION
f. Copy the key into your app like this:
private const val BASE64_PUBLIC_KEY = "YOUR LICENSE KEY FOR THIS APPLICATION";
Make sure that there are no spaces.
1.2 A salt
a. What is a salt?
A salt is random data that is additional input when hashing a password. They are used to defend against dictionary attacks and rainbow table attacks.
b. How do I get one?
This is a good link to generate a random salt. There should be exactly 20 random integers, so put
20
in for the amount of random strings to generate, each string should be2
characters long (used for this example, it doesn't have to be). Check numeric digits, and check Identical strings are allowed. They can be negative numbers too. Try to remove any redundancy, e.g.00 -> 0
, for the sake of consistency.c. Where do I put the salt?
When declaring variables just put this code in, except with your random salt.
private val SALT = byteArrayOf(YOUR RANDOM SALT COMMA SEPARATED 20 INTEGERS)
- 步骤 21 中的变量应添加到您的主 activity class 中。现在,您应该向主 activity 添加一些代码。它大致应该是这样的(注意
// TODO
评论):
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.vending.licensing.*
import kotlin.system.exitProcess
class MainActivity : AppCompatActivity()
{
companion object
{
private const val BASE64_PUBLIC_KEY = "YOUR LICENSE KEY FOR THIS APPLICATION" // TODO replace with your own key
private val SALT = byteArrayOf(YOUR RANDOM SALT COMMA SEPARATED 20 INTEGERS) // TODO replace with your own salt
}
private val deviceId: String by lazy {
Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
}
private lateinit var licenseCheckerCallback: LicenseCheckerCallback
private lateinit var checker: LicenseChecker
private fun doCheck()
{
checker.checkAccess(licenseCheckerCallback)
}
override fun onDestroy()
{
super.onDestroy()
checker.onDestroy()
}
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
// Construct the LicenseCheckerCallback. The library calls this when done.
licenseCheckerCallback = MyLicenseCheckerCallback()
// Construct the LicenseChecker with a Policy.
checker = LicenseChecker(
this,
ServerManagedPolicy(this, AESObfuscator(SALT, packageName, deviceId)),
BASE64_PUBLIC_KEY // Your public licensing key.
)
doCheck()
setContentView(R.layout.activity_main) // TODO Replace with your own layout
}
private fun displayResult(result: String)
{
// TODO you can change this how the info is displayed
Toast.makeText(this, result, Toast.LENGTH_SHORT).show()
}
private inner class MyLicenseCheckerCallback : LicenseCheckerCallback
{
override fun allow(reason: Int)
{
if (isFinishing)
{
// Don't update UI if Activity is finishing.
return
}
// Should allow user access.
}
override fun applicationError(errorCode: Int)
{
// TODO handle the error your own way. Calling `dontAllow` is common.
dontAllow(Policy.NOT_LICENSED)
}
override fun dontAllow(reason: Int)
{
if (isFinishing)
{
// Don't update UI if Activity is finishing.
return
}
if (reason == Policy.RETRY)
{
// If the reason received from the policy is RETRY, it was probably
// due to a loss of connection with the service, so we should give the
// user a chance to retry. So show a dialog to retry.
// TODO handle Policy.RETRY
}
else
{
// Otherwise, the user isn't licensed to use this app.
// Your response should always inform the user that the application
// isn't licensed, but your behavior at that point can vary. You might
// provide the user a limited access version of your app or you can
// take them to Google Play to purchase the app.
// TODO implement goto market
}
displayResult("Not Licensed")
// TODO you may not abort if you have some other way to handle the fail case
abort()
}
}
private fun abort()
{
finishAffinity()
exitProcess(0)
}
}
- 将这些权限添加到您的清单文件:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.vending.CHECK_LICENSE"/>
- 如果您收到异常消息类似于:
Service Intent must be explicit: Intent { act=com.android.vending.licensing.ILicensingService }
应用 this 答案中的修复程序。
- 应该就这些了。有关详细信息,请参阅我之前引用的 answer。我希望这能为其他人节省一些时间。