优先看谷歌文档,看完后大概了解了下流程,集成Module,请求权限、下载、解压、启动
集成Model中遇到的坑太多了
1、不要忘记集成解压的Module,下载完obb需要解压到 getExternalFilesDir() 下的,也就是 unity中的 PersistentDataPath 文件路径
2、其中 download 和 google licensing 中用的还是aphache的请求库,需要在配置中的android大括号内加入
useLibrary 'org.apache.http.legacy'
3、download module中的notification过期需要如下代码代替,
onDownloadStateChanged中过期的notification我给注释了,我觉得不需要通知
@Override
public void onDownloadProgress(DownloadProgressInfo progress) {
mProgressInfo = progress;
if (null != mClientProxy) {
mClientProxy.onDownloadProgress(progress);
}
// fix bug
if (Build.VERSION.SDK_INT >= 26) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(LOGTAG, LOGTAG, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(notificationChannel);
}
if (progress.mOverallTotal <= 0) {
// we just show the text
//mNotification.tickerText = mCurrentTitle;
//mNotification.icon = android.R.drawable.stat_sys_download;
//mNotification.setLatestEventInfo(mContext, mLabel, mCurrentText, mContentIntent);
mNotification = new Notification.Builder(mContext)
.setAutoCancel(true)
.setContentTitle(mCurrentTitle)
.setContentText(mCurrentText)
.setContentIntent(mContentIntent)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setWhen(System.currentTimeMillis())
.build();
mCurrentNotification = mNotification;
} else {
mCustomNotification.setCurrentBytes(progress.mOverallProgress);
mCustomNotification.setTotalBytes(progress.mOverallTotal);
mCustomNotification.setIcon(android.R.drawable.stat_sys_download);
mCustomNotification.setPendingIntent(mContentIntent);
mCustomNotification.setTicker(mLabel + ": " + mCurrentText);
mCustomNotification.setTitle(mLabel);
mCustomNotification.setTimeRemaining(progress.mTimeRemaining);
mCurrentNotification = mCustomNotification.updateNotification(mContext);
}
mNotificationManager.notify(NOTIFICATION_ID, mCurrentNotification);
}
4、google licensing module中 com.google.android.vending.licensing.LicenseChecker类的checkAccess()中,有这样一段代码:
boolean bindResult = mContext.bindService(new Intent(new String(Base64.decode("Y29tLmFuZHJvaWQudmVuZGluZy5saWNlbnNpbmcuSUxpY2Vuc2luZ1NlcnZpY2U="))),
this, // ServiceConnection.
Context.BIND_AUTO_CREATE);
这段在代码在Android5.0上会抛出IllegalArgumentException,需要替换成如下代码:
Intent serviceIntent = new Intent(new String(Base64.decode("Y29tLmFuZHJvaWQudmVuZGluZy5saWNlbnNpbmcuSUxpY2Vuc2luZ1NlcnZpY2U=")));
serviceIntent.setPackage("com.android.vending");
boolean bindResult = mContext.bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
在com.google.android.vending.expansion.downloader.DownloaderClientMarshaller类的connect()方法中,有这样一段代码:
if ( !c.bindService(bindIntent, mConnection, Context.BIND_DEBUG_UNBIND) ) {
if ( Constants.LOGVV ) {
Log.d(Constants.TAG, "Service Unbound");
}
} else {
mBound = true;
}
这里会导致绑定的服务在某些情况下无法启动,服务不启动,IDownloaderClient接口的onServiceConnected()方法就不会执行,mRemoteService为null,从而导致NullPointerException。虽然在使用mRemoteService前增加对其是否为null的判断可以避免crash,但是下载过程仍然无法监控,无法得到下载的结果。需要将这段代码替换成如下代码。也就是将BIND_DEBUG_UNBIND替换成BIND_AUTO_CREATE。
if ( !c.bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE) ) {
if ( Constants.LOGVV ) {
Log.d(Constants.TAG, "Service Unbound");
}
} else {
mBound = true;
}
其他注意事项
1、obb中的空文件的value 要跟 安卓工程中的 "unity.build-id 一致,否则会卡在主界面
<meta-data
android:name="unity.build-id"
android:value="3d754641-fd7b-477d-9b5e-5713dd680a7e" />
2、权限要申请
3、解压成功后保存个状态,当data被清除时再次从obb解压、否则会出现进不去游戏的问题
4、使用Snackbar请求权限时需要在manifest文件中的activity中设置
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
5、解压我用的task,遇到延迟2分钟才调用doInBackground的情况,估计是因为其他的地方也在占用task,这时设置
task.executeOnExecutor(Executors.newCachedThreadPool());
6、publick key 要填写正确,obbversion 和 obbfilesize 大小要一样,否则会触发下载
参考文档:
https://blog.csdn.net/ithouse/article/details/84103847
https://developer.android.com/google/play/expansion-files
https://7dot9.com/2017/10/12/google-play-obb-fundamental-pitfall/