豪游海外安卓SDK接入文档
<p>[TOC]</p>
<h1>豪游海外安卓SDK接入文档</h1>
<h2>1、开发环境搭建</h2>
<h3>1.1、将 SDK aar 文件复制到游戏工程的 libs 目录中并引用</h3>
<h3>1.2、复制 assets 目录下的 hysdk.xml 文件到游戏工程的 assets 目录</h3>
<ul>
<li>将assets中的 <code>hysdk.xml</code> 配置文件中的参数进行修改</li>
</ul>
<pre><code class="language-xml">AppsFlyerKey //AppsFlyerKey统计初始化key
google_server_client_id //谷歌登录client id
max_ad_key //max广告初始化key
game //豪游SDK 游戏标识
secretKey //豪游SDK secretKey
serverUrl //豪游SDK服务器域名
logUrl//豪游SDK日志上报域名
sub_chl //分包标识
channel //广告渠道标识
source_id //预留字段,默认传0</code></pre>
<ul>
<li>在游戏values文件夹添加以下第三方SDK参数(参数找运营获取)</li>
</ul>
<pre><code class="language-xml">&lt;string name=&quot;facebook_app_id&quot;&gt;123456&lt;/string&gt;
&lt;string name=&quot;facebook_client_token&quot;&gt;2295a0abf42afxxxxxxx&lt;/string&gt;
&lt;string name=&quot;fb_login_protocol_scheme&quot;&gt;fb123456&lt;/string&gt;
&lt;string name=&quot;server_client_id&quot;&gt;xxxxxxx&lt;/string&gt;</code></pre>
<ul>
<li>manifest下添加</li>
</ul>
<pre><code class="language-xml">&lt;meta-data android:name=&quot;com.facebook.sdk.ApplicationId&quot; android:value=&quot;@string/facebook_app_id&quot;/&gt;
&lt;meta-data android:name=&quot;com.facebook.sdk.ClientToken&quot; android:value=&quot;@string/facebook_client_token&quot;/&gt;
&lt;provider android:authorities=&quot;com.facebook.app.FacebookContentProvider${applicationId}&quot;
android:name=&quot;com.facebook.FacebookContentProvider&quot;
android:exported=&quot;true&quot; /&gt;</code></pre>
<h3>1.3、将 google-services.json 文件复制到工程 app 文件夹下</h3>
<ul>
<li>app\google-services.json</li>
</ul>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=7a74f20997943f5ab8b2211ce93b2c05&amp;file=file.png" alt="" /></p>
<h3>1.4、插件和第三方依赖配置</h3>
<ul>
<li>app\build.gradle</li>
</ul>
<pre><code class="language-java">//旧版写法
apply plugin: &#039;com.google.gms.google-services&#039;
apply plugin: &#039;com.google.firebase.crashlytics&#039;
//新版写法
plugins {
id &#039;com.google.gms.google-services&#039;
id &#039;com.google.firebase.crashlytics&#039;
}
android{
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(include: [&#039;*.jar&#039;,&#039;*.aar&#039;], dir: &#039;libs&#039;)
implementation &#039;com.android.billingclient:billing:6.2.1&#039;
implementation &#039;com.facebook.android:facebook-android-sdk:17.0.1&#039;
implementation &#039;com.appsflyer:af-android-sdk:6.15.1&#039;
implementation &#039;androidx.credentials:credentials:1.2.2&#039;
implementation &#039;androidx.credentials:credentials-play-services-auth:1.2.2&#039;
implementation &#039;com.google.android.libraries.identity.googleid:googleid:1.1.1&#039;
implementation &#039;com.android.installreferrer:installreferrer:2.2&#039;
implementation &#039;com.google.android.gms:play-services-ads-identifier:18.1.0&#039;
implementation &#039;com.applovin:applovin-sdk:13.0.0&#039;
implementation(platform(&quot;com.google.firebase:firebase-bom:33.3.0&quot;))
implementation(&quot;com.google.firebase:firebase-analytics&quot;)
}</code></pre>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=28d9a5057a73f5a9dd6f5a5dcb683663&amp;file=file.png" alt="" /></p>
<ul>
<li>工程根目录下build.gradle</li>
</ul>
<pre><code class="language-java">//旧版写法
buildscript {
dependencies {
classpath &#039;com.google.gms:google-services:4.4.2&#039;
classpath &#039;com.google.firebase:firebase-crashlytics-gradle:3.0.2&#039;
}
}
//新版写法
plugins {
id &#039;com.google.gms.google-services&#039; version &#039;4.4.2&#039; apply false
id &#039;com.google.firebase.crashlytics&#039; version &#039;3.0.2&#039; apply false
}</code></pre>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=b02c831e819e87cdc5e88a4c4b302544&amp;file=file.png" alt="" /></p>
<h2>2、功能接口 (注意:以下所有接口都必须在游戏收到 SDK 初始化完成之后才能调用,所有接口请在主线程调用,SDK的回调不一定是主线程)</h2>
<h3>2.1、继承 HYSdkApplication (必接)</h3>
<p>游戏工程如果没有Application,请在 AndroidManifest.xml 中添加SDK的Application,如下所示:</p>
<pre><code class="language-xml">&lt;application android:name=&quot;com.hy.sdk.HYSdkApplication&quot;&gt;</code></pre>
<p>游戏工程如果有自定义 Application,请继承 SDK 中 HYSdkApplication</p>
<pre><code class="language-java">public class XXXXApplication extends HYSdkApplication {
@Override
public void onCreate() {
super.onCreate();
}
}</code></pre>
<h3>2.2.0、打开调试log</h3>
<pre><code class="language-java">HYSdk.getInstance().setDebug(true);</code></pre>
<h3>2.2.1、设置游戏版本,尽早调用,热更后需设置新的版本,可在初始化调用前调用(必接)</h3>
<pre><code class="language-java">public void setGameVersion(String gameVersion)
HYSdk.getInstance().setGameVersion(&quot;1.0.0&quot;);</code></pre>
<h3>2.2、初始化接口(必接)</h3>
<pre><code> 注意: 初始化接口必须在调用申请权限回调以后再调用
接口说明:
首先在程序开始的地方调用 SDK 的初始化 init 方法,并设置 Activity 对像和初始化完成回调监听
(在初始化失败情况下不再调用其它 SDK 接口方法)
注意:要确保在 SDK 初始化成功后才可调用其它接口</code></pre>
<h4>2.2.1、方法定义</h4>
<pre><code class="language-java">public void init(Activity context, HYSdkCallBack hysdkCallback)</code></pre>
<h4>2.2.2、参数说明</h4>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>context</td>
<td>上下文</td>
</tr>
<tr>
<td>hysdkCallback</td>
<td>回调通知,初始化,登录,充值等回调通知都统一在此</td>
</tr>
<tr>
<td>code</td>
<td>HYSdkCallBack内各个状态</td>
</tr>
<tr>
<td>Object</td>
<td>回调数据详情</td>
</tr>
</tbody>
</table>
<h4>2.2.3、代码示例:</h4>
<pre><code class="language-java">HYSdk.getInstance().init(this, new HYSdkCallBack() {
@Override
public void callback(int code, Object result) {
switch (code) {
case HYSdkCallBack.INIT_SUCCESS:
Toast.makeText(MainActivity.this,&quot;初始化成功 isOpenQuickLogin:&quot;+HYUtils.getInstance().isOpenQuickLogin,Toast.LENGTH_SHORT).show();
break;
case HYSdkCallBack.LOGIN_SUCCESS:
HYLoginResult.ddto userInfo = ((HYLoginResult) result).getD();
String uid = userInfo.getUid();
String token = userInfo.getToken();
String account_name = userInfo.getAccountName();
Toast.makeText(MainActivity.this,&quot;登录成功:&quot;+userInfo.getD().getUid(),Toast.LENGTH_SHORT).show();
break;
case HYSdkCallBack.LOGIN_FAIL:
Toast.makeText(MainActivity.this,&quot;登录失败:&quot;+result.toString(),Toast.LENGTH_SHORT).show();
break;
case HYSdkCallBack.PAY_SUCCESS:
//支付成功 实际发货以服务端通知为准
Toast.makeText(MainActivity.this,&quot;支付成功:&quot;+result.toString(),Toast.LENGTH_SHORT).show();
break;
case HYSdkCallBack.PAY_FAIL:
Toast.makeText(MainActivity.this,&quot;支付失败:&quot;+result.toString(),Toast.LENGTH_SHORT).show();
break;
case HYSdkCallBack.INIT_AD_SUCCESS:
//广告初始化成功,调用SDK初始化接口后异步回调
break;
case HYSdkCallBack.LOAD_AD_SUCCESS:
//广告加载成功
break;
case HYSdkCallBack.INIT_AD_FAIL:
//广告初始化失败
break;
case HYSdkCallBack.LOAD_AD_FAIL:
//广告加载失败
break;
case HYSdkCallBack.SHOW_AD_SUCCESS:
//广告展示成功 可以发放奖励
break;
case HYSdkCallBack.SHOW_AD_FAIL:
//广告展示失败
break;
case HYSdkCallBack.PRE_ORDER:
//游戏收到 预注册用户奖励 回调
//进行自己的创建订单逻辑,传入返回的商品ID调起支付接口即可
String giftid = (String) result;
HYPayInfo payInfo = new HYPayInfo();
payInfo.setAmount(&quot;1&quot;);
payInfo.setProductId(giftid);
payInfo.setCpOrderId(&quot;cp_test_&quot;+System.currentTimeMillis());
payInfo.setProductName(&quot;预注册奖励&quot;);
payInfo.setCustom(&quot;cp透传&quot;);
payInfo.setServerId(&quot;server_1&quot;);
payInfo.setServerName(&quot;服务器1&quot;);
payInfo.setRoleId(&quot;22&quot;);
payInfo.setRoleName(&quot;角色名字1&quot;);
payInfo.setRoleLevel(22);
payInfo.setGameLevel(&quot;关卡1&quot;);
HYSdk.getInstance().pay(MainActivity.this, payInfo);
break;
default:
break;
}
}
});</code></pre>
<h3>2.3、登录接口(必接)</h3>
<p>接口说明:游戏登录时调用</p>
<h4>2.3.1、方法定义</h4>
<pre><code class="language-java">public void login(Activity context)</code></pre>
<h4>2.3.2、参数说明</h4>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>context</td>
<td>上下文</td>
</tr>
</tbody>
</table>
<h4>2.3.3、代码示例</h4>
<pre><code class="language-java">HYSdk.getInstance().login(this);</code></pre>
<h4>2.3.4、登录回调对象 HYLoginResult 参数说明</h4>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>game</td>
<td>游戏标识</td>
</tr>
<tr>
<td>uid</td>
<td>用户UID</td>
</tr>
<tr>
<td>token</td>
<td>用户登录态,用于服务端登录校验注意token有效期为5分钟,服务端需要在5分钟内完成校验</td>
</tr>
<tr>
<td>loginType</td>
<td>登录方式</td>
</tr>
<tr>
<td>account_name</td>
<td>账号名(显示名)</td>
</tr>
<tr>
<td>account_type</td>
<td>0:游客;1:邮箱;2: 脸书; 3:谷歌 ;4: 苹果</td>
</tr>
</tbody>
</table>
<pre><code class="language-java">HYLoginResult userInfo = (HYLoginResult) result;
String uid = userInfo.getD().getUid();
String token = userInfo.getD().getToken();</code></pre>
<h3>2.4、提交角色数据接口(必接)</h3>
<p>接口说明:游戏提交角色数据接口,该接口需要在以下4中情况下调用:</p>
<ul>
<li>角色登录成功</li>
<li>角色升级</li>
<li>创建角色</li>
<li>选服完成</li>
</ul>
<h4>2.4.1、方法定义</h4>
<pre><code class="language-java">public void submitRoleInfo(Activity context, HYRoleInfo roleInfo)</code></pre>
<h4>2.4.2、参数说明</h4>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>context</td>
<td>上下文</td>
</tr>
<tr>
<td>HYRoleInfo</td>
<td>角色实体类对象</td>
</tr>
</tbody>
</table>
<pre><code>HYRoleInfo 实体类字段说明(以下参数都为必传参数,如游戏没有的传 &quot;&quot; 或者 &quot;0&quot;)</code></pre>
<table>
<thead>
<tr>
<th>字段名</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>subType</td>
<td><code>创建角色</code> 调用类型 Type_CreateRole ,<code>角色进入游戏</code> 调用类型 Type_EnterGame ,<code>角色升级</code> 调用类型 Type_RoleUpgrade,<code>选服完成</code> 调用类型 Type_SelectServer</td>
</tr>
<tr>
<td>serverId</td>
<td>服务器ID,长度不超过20字符,建议使用字符串型数值,例如:1</td>
</tr>
<tr>
<td>serverName</td>
<td>服务器名称,长度不超过20字符</td>
</tr>
<tr>
<td>roleId</td>
<td>角色ID,长度不超过20字符,建议使用字符串型数值,例如:1</td>
</tr>
<tr>
<td>roleName</td>
<td>角色名称,长度不超过100字符,不设定角色名称可传 ""</td>
</tr>
<tr>
<td>roleLevel</td>
<td>角色等级,不设定可传 0</td>
</tr>
<tr>
<td>vipLevel</td>
<td>角色vip等级,不是VIP传0,是VIP传正常值</td>
</tr>
<tr>
<td>gameLevel</td>
<td>角色关卡</td>
</tr>
</tbody>
</table>
<h4>2.4.3、代码示例</h4>
<pre><code class="language-java">HYRoleInfo roleInfo = new HYRoleInfo();
roleInfo.setVipLevel(1);
roleInfo.setServerId(&quot;server_1&quot;);
roleInfo.setServerName(&quot;服务器1&quot;);
roleInfo.setRoleId(&quot;22&quot;);
roleInfo.setRoleName(&quot;角色名字1&quot;);
roleInfo.setRoleLevel(22);
roleInfo.setGameLevel(&quot;关卡1&quot;);
roleInfo.setSubType(HYRoleInfo.Type_CreateRole);
HYSdk.getInstance().submitRoleInfo(this, roleInfo);</code></pre>
<h3>2.5、支付接口(必接)</h3>
<h4>2.5.1、方法定义</h4>
<pre><code class="language-java">public void pay(Activity context, HYPayInfo payInfo)</code></pre>
<h4>2.5.2、参数说明</h4>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>context</td>
<td>上下文</td>
</tr>
<tr>
<td>HYPayInfo</td>
<td>支付实体类对象</td>
</tr>
</tbody>
</table>
<pre><code> 支付信息实体类(HYPayInfo)字段说明</code></pre>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>amount</td>
<td>订单金额,单位:分;例如:600</td>
</tr>
<tr>
<td>productName</td>
<td>道具商品名称,长度不超过50字符。</td>
</tr>
<tr>
<td>productId</td>
<td>道具商品ID,长度不超过50字符。需在SDK后台配置</td>
</tr>
<tr>
<td>serverId</td>
<td>服务器ID,长度不超过20字符,建议使用字符串型数值。</td>
</tr>
<tr>
<td>serverName</td>
<td>服务器名称,长度不超过20字符,建议使用字符串型数值。</td>
</tr>
<tr>
<td>roleId</td>
<td>角色ID,长度不超过64字符,建议使用字符串型数值。</td>
</tr>
<tr>
<td>roleName</td>
<td>角色名称,长度不超过100字符,不设定角色名称可传""</td>
</tr>
<tr>
<td>roleLevel</td>
<td>角色等级,不设定可传 0</td>
</tr>
<tr>
<td>custom</td>
<td>透传参数,支付成功的回调中会带上该参数。</td>
</tr>
<tr>
<td>cpOrderId</td>
<td>CP方订单唯一标识,长度不超过64字符,例如:sgbw2020123456789。</td>
</tr>
<tr>
<td>gameLevel</td>
<td>角色关卡</td>
</tr>
<tr>
<td>isTestOrder</td>
<td>boolean 是否测试订单</td>
</tr>
</tbody>
</table>
<h4>2.5.3、代码示例</h4>
<pre><code class="language-java">HYPayInfo payInfo = new HYPayInfo();
payInfo.setAmount(&quot;1&quot;);
payInfo.setProductId(productId[0]);
payInfo.setCpOrderId(&quot;cp_test_&quot;+System.currentTimeMillis());
payInfo.setProductName(&quot;禮包1&quot;);
payInfo.setCustom(&quot;cp透传&quot;);
payInfo.setServerId(&quot;server_1&quot;);
payInfo.setServerName(&quot;服务器1&quot;);
payInfo.setRoleId(&quot;22&quot;);
payInfo.setRoleName(&quot;角色名字1&quot;);
payInfo.setRoleLevel(22);
payInfo.setGameLevel(&quot;关卡1&quot;);
payInfo.setTestOrder(true);
HYSdk.getInstance().pay(MainActivity.this, payInfo);</code></pre>
<h3>2.6、打开切换账号页面,切换新账号成功之后在用户信息在登录回调返回(必接)</h3>
<h4>2.6.1、调用账号注销退出登录 方法定义</h4>
<pre><code class="language-java">public void logout(Activity context)</code></pre>
<h4>2.6.3、代码示例</h4>
<pre><code class="language-java">HYSdk.getInstance().changeAccount(this);</code></pre>
<h3>2.7、谷歌预注册用户奖励发放接入(必接)</h3>
<pre><code>如果玩家为谷歌预注册用户的话,进入游戏之后会回调一个免费的内购商品
游戏会在 init 回调接口 收到类型 HYSdkCallBack.PRE_ORDER 的回调
游戏方收到该回调后可以进行`自己的下单逻辑`然后调用SDK的支付接口</code></pre>
<pre><code class="language-java"> case HYSdkCallBack.PRE_ORDER:
//游戏收到 预注册用户奖励 回调
//进行自己的创建订单逻辑,传入返回的商品ID调起支付接口即可
String giftid = (String) result;
HYPayInfo payInfo = new HYPayInfo();
payInfo.setAmount(&quot;1&quot;);
payInfo.setProductId(giftid);
payInfo.setCpOrderId(&quot;cp_test_&quot;+System.currentTimeMillis());
payInfo.setProductName(&quot;预注册奖励&quot;);
payInfo.setCustom(&quot;cp透传&quot;);
payInfo.setServerId(&quot;server_1&quot;);
payInfo.setServerName(&quot;服务器1&quot;);
payInfo.setRoleId(&quot;22&quot;);
payInfo.setRoleName(&quot;角色名字1&quot;);
payInfo.setRoleLevel(22);
payInfo.setGameLevel(&quot;关卡1&quot;);
HYSdk.getInstance().pay(MainActivity.this, payInfo);
break;</code></pre>
<h3>2.9、加载广告(游戏调用需在收到广告初始化回调 HYSdkCallBack.INIT_AD_SUCCESS 成功后调用)</h3>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>context</td>
<td>上下文</td>
</tr>
<tr>
<td>ad_id</td>
<td>广告位ID,需找运营获取</td>
</tr>
</tbody>
</table>
<h4>2.9.1、加载广告,游戏在用户进入游戏后合适时机调用</h4>
<pre><code class="language-java">HYSdk.getInstance().loadAd(this,&quot;cf18831617aaxxxx&quot;);</code></pre>
<pre><code>成功回调
HYSdkCallBack.LOAD_AD_SUCCESS
失败回调
HYSdkCallBack.LOAD_AD_FAIL</code></pre>
<h4>2.9.3、显示广告,加载广告成功后可以调用</h4>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>context</td>
<td>上下文</td>
</tr>
<tr>
<td>ad_id</td>
<td>广告位ID,需找运营获取</td>
</tr>
</tbody>
</table>
<pre><code class="language-java">HYSdk.getInstance().showAd(this,&quot;cf18831617aaxxxx&quot;);</code></pre>
<pre><code>成功回调
HYSdkCallBack.SHOW_AD_SUCCESS
失败回调
HYSdkCallBack.SHOW_AD_FAIL</code></pre>
<h3>2.9.4、激励广告奖励的发放为收到SDK回调 HYSdkCallBack.SHOW_AD_SUCCESS</h3>
<pre><code class="language-java">String msg = (String) result;
//{&quot;amount&quot;:0,&quot;label&quot;:&quot;&quot;}</code></pre>
<h2>3、生命周期(必接)</h2>
<pre><code class="language-java"> @Override
protected void onDestroy() {
super.onDestroy();
HYSdk.getInstance().onDestroySdk();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
HYSdk.getInstance().onActivityResult(requestCode, resultCode, data);
}</code></pre>
<h2>4.混淆配置 proguard-rules.pro</h2>
<pre><code class="language-java">-keep class com.hy.sdk.bean.** { *; }
-keep class com.hy.sdk.HYSdk { *; }
-keep class com.hy.sdk.track.** { *; }
-keep class com.hy.sdk.HYSdkApplication{ *; }
-keep class com.hy.sdk.HYSdkCallBack{ *; }
-keep class com.hy.sdk.HYPageActivity{ *; }
# Gson
-keepattributes Signature
-keepattributes *Annotation*
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
# Gson specific interfaces
-keep interface com.google.gson.** { *; }
# Gson specific fields
-keepclassmembers class * {
@com.google.gson.annotations.SerializedName &lt;fields&gt;;
}</code></pre>