APK Expansion Files / Obb 接入介绍

APK Expansion Files / Obb 接入介绍

APK Expansion Files 是谷歌官方提供的APK分包下载策略, 依赖工程已经包含在SDK中.

官方资料


使用方法


1. 导入依赖工程

依赖工程放在SDK文件夹中/SDK/extras/google/文件夹下, 需要依赖market_apk_expansionmarket_licensing.

说明1: market_apk_expansionmarket_licensing有依赖

说明2: 如果出现Android App crashes on Lollipop - Service Intent must be explicit: [duplicate]类似的报错, 请参照链接修改工程代码licensing工程BUG

2. 打包OBB文件
3. 客户端接入

OBB是通过谷歌后台public key与包名进行资源匹配, 请确保这两项正确.

  1. 继承service, receiver

    • 继承 BroadcastReceiver

      用于在游戏运行的时候检查是否需要下载资源文件, 重写`onReceive(Context, Intent)`方法, 示例
          
          @Override
          public void onReceive(Context context, Intent intent) {
              try {
                  DownloaderClientMarshaller.startDownloadServiceIfRequired(context,
                  intent, SampleDownloaderService.class);
              } catch (PackageManager.NameNotFoundException e) {
                  e.printStackTrace();
              }
          }
    • 继承DownloaderService

      新建一个class, 继承`DownloaderService`, 重写了里面的三个方法, 
          
      * `getPublicKey()`, 需要在return中返回游戏的`public key`.
      * `getSALT()`, 需要在return中返回SALT
          
          需要返回一个随机字节数组, 格式如下
          
              public static final byte[] SALT = new byte[] { 1, 42, -12, -1, 54, 98, -100, -12, 43, 2, -8, -4, 9, 5, -106, -107, -33, 45, -1, 84};
      * `getAlarmReceiverClassName()`
          
          返回之前继承`BroadcastReceiver`的className, 示例
          
              @Override
              public String getAlarmReceiverClassName() {
                  return SampleAlarmReceiver.class.getName();
                  }
  2. AndroidManifest.xml相关配置

    声明权限:

    <!-- Required to access Google Play Licensing -->
       <uses-permission android:name="com.android.vending.CHECK_LICENSE" />
    
       <!-- Required to download files from Google Play -->
       <uses-permission android:name="android.permission.INTERNET" />
    
       <!-- Required to keep CPU alive while downloading files
       (NOT to keep screen awake) -->
       <uses-permission android:name="android.permission.WAKE_LOCK" />
    
       <!-- Required to poll the state of the network connection
       and respond to changes -->
       <uses-permission
       android:name="android.permission.ACCESS_NETWORK_STATE" />
    
       <!-- Required to check whether Wi-Fi is enabled -->
       <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    
       <!-- Required to read and write the expansion files on shared storage -->
       <uses-permission
       android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
       
    声明service, receiver:
    
        <service android:name=".SampleDownloaderService" />
       <receiver android:name=".SampleAlarmReceiver" />
    1. 下载Activity的配置

      • onCreate中添加初始化代码

        示例
        @Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try{
        Intent notifierIntent = new Intent(this, MainActivity.class);
        notifierIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notifierIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // 如果需要, 开始下载service
        int startResult =
                DownloaderClientMarshaller.startDownloadServiceIfRequired(this,
                        pendingIntent, SampleDownloaderService.class);
        if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
            // 开始下载
            mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this,
                    SampleDownloaderService.class);
            return;
            }
        }catch(Throwable e) {
        e.printStackTrace();
        }
        }
        
* 接入接口
    
    Activity中implements`IDownloaderClient`.
    
    * `onServiceConnected()`
        
        当service连接的时候会调用此方法, 请添加如下逻辑, 获得service对象
        
            mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);
            mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
            
    * `onDownloadStateChanged()`
        
        下载状态发生变化的时候, 会调用此方法
        
    * `onDownloadProgress()`
        
        下载进度进行中会不断调用此方法, 用于更新下载的状态
 * service其他API
 
     * `requestPauseDownload()`
         
         暂停下载
         
     * `requestContinueDownload()`
     
         继续下载
         
     * `setDownloadFlags()`
     
         下载标识设置. 目前只提供一个标识, `IDownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR`, 如果设置, 玩家可以通过移动网络下载, 否则只能在WIFI状态进行下载

相关推荐