本文档描述了 Android 开发者如何集成 Game SDK(后面简称为 GameSdk),通过集成 GameSdk 为 App 引入 H5 游戏、App 游戏的聚合服务。
AppId:应用 id,18位 hex 字符串。【注意】调试时请使用测试 appId:ba0063bfbc1a5ad878(H5 游戏、App 游戏)、ba01793105db67e03f(只有 H5 游戏);外发版本请替换成正式 appId,否则不会产生收益。
UserId:用户 id,接入方定义的用户唯一标识,userId 和用户游戏积分、奖励信息相关联。
通过在 Android Studio 工程的 build.gradle 配置脚本中添加 maven 依赖,导入最新版本 GameSdk。
allprojects {
repositories {
maven {
credentials {
username 'iqLuKm'
password 'pomH01oYcR'
}
url 'https://repo.rdc.aliyun.com/repository/117933-release-sPkE7F/'
}
}
}
【注意】加在 allprojects 中,不要加在 buildscript 中哦。
dependencies {
// 非 androidx 依赖
// implementation 'com.android.support:support-v4:28.0.0'
// implementation 'com.android.support:appcompat-v7:28.0.0'
// androidx 依赖
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'com.mob.sdk:oaid-sdk:1.0.27' // 依赖 oaid
implementation 'com.mob.sdk:gamesdk:1.1.1'
}
将 NewsSdk 的 aar 包复制到 Application Module 的 libs 目录下(无此目录需手动创建), 并在工程的 build.gradle 中添加如下配置:
repositories {
flatDir {
dirs 'libs'
}
}
depedencies {
// 非 androidx 依赖
// implementation 'com.android.support:support-v4:28.0.0'
// implementation 'com.android.support:appcompat-v7:28.0.0'
// androidx 依赖
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation(name: 'oaid-sdk', ext: 'aar') // 依赖 oaid
implementation(name: 'gamesdk-1.1.1', ext: 'aar')
}
开发者需要在 Application 实例的 onCreate() 方法中调用以下代码来初始化 GameSdk。
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
GameSdk.getInstance().init(getApplicationContext(),
new GameConfig.Builder()
.appId(APP_ID)
.userId(userId) // 未登录可不设置 userId,登录时再设置,userId 关联用户积分信息
.debug(BuildConfig.DEBUG)
.build(),
null);
}
}
初始化配置参数说明:
private String appId; // 应用程序 id
private String userId; // 用户 id
private boolean debug; // 是否 debug 模式,是则输出日志
登录时请设置 userId:
GameSdk.getInstance().setUserId(userId);
退出登录请重置 userId:
GameSdk.getInstance().setUserId(null);
H5GameFragment 封装了 H5 游戏聚合,接入方只需将 H5GameFragment 放置在对应的 container 容器中即可,具体调用方式可以参考 Demo 中的 H5GameActivity。
public class H5GameActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_h5_game);
final H5GameFragment fragment = H5GameFragment.newInstance();
getSupportFragmentManager() // fragment 嵌套替换成 getChildFragmentManager()
.beginTransaction()
.replace(R.id.container, fragment)
.commitNowAllowingStateLoss();
findViewById(R.id.nav_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fragment.canGoBack()) {
fragment.goBack();
} else {
finish();
}
}
});
}
}
AppGameFragment 封装了 App 游戏聚合,接入方只需将 AppGameFragment 放置在对应的 container 容器中即可,具体调用方式可以参考 Demo 中的 AppGameActivity。
public class AppGameActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_game);
final AppGameFragment fragment = AppGameFragment.newInstance();
getSupportFragmentManager() // fragment 嵌套替换成 getChildFragmentManager()
.beginTransaction()
.replace(R.id.container, fragment)
.commitNowAllowingStateLoss();
findViewById(R.id.nav_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fragment.canGoBack()) {
fragment.goBack();
} else {
finish();
}
}
});
}
}