Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

android - Accessing APK expansion file with URI (with Google Zip Expansion Library) causes a crash if expansion file version is different from apk versioncode

My apk version code is version 3. with it I am using main expansion file which was loaded with apk version code 1 (file name is similar to main.1.ex.etc.eg.obb). The expansion file downloads fine on a device.

The expansion file has media file, so I using APEZProvider from the Google Zip Expansion Library to play it with VideoView.

Calling VideoView.start() causes an Nullpointer exception.

What I have found so far: In APEZProvider.initIfNecessary() returns Main expansion file version as 3 instead of 1. Thus trying to open ZipResourceFile (mAPKExtensionFile) returns null. APEZProvider.openAssetFile() causes NullPointerException as mAPKExtensionFile is null.

Relevant code from APEZProvider class in Google Zip Expansion Library:

  private boolean initIfNecessary() {
    if ( !mInit ) {
        Context ctx = getContext();
        PackageManager pm = ctx.getPackageManager();
        ProviderInfo pi = pm.resolveContentProvider(getAuthority(), PackageManager.GET_META_DATA);
        PackageInfo packInfo;
        try {
            packInfo = pm.getPackageInfo(ctx.getPackageName(), 0);
        } catch (NameNotFoundException e1) {
            e1.printStackTrace();
            return false;
        }
        int patchFileVersion;
        int mainFileVersion;
        int appVersionCode = packInfo.versionCode;
        if ( null != pi.metaData ) {
            mainFileVersion = pi.metaData.getInt("mainVersion", appVersionCode);
            patchFileVersion = pi.metaData.getInt("patchVersion", appVersionCode);          
        } else {
            mainFileVersion = patchFileVersion = appVersionCode;
        }
        try {
            mAPKExtensionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx, mainFileVersion, patchFileVersion);
            return true;
        } catch (IOException e) {
            e.printStackTrace();                
        }
    }
    return false;       
}



@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode)
        throws FileNotFoundException {
    initIfNecessary();
    String path = uri.getEncodedPath();
    if ( path.startsWith("/") ) {
        path = path.substring(1);
    }
    return mAPKExtensionFile.getAssetFileDescriptor(path);      
}

I am not sure about this line of code in the above: ProviderInfo pi = pm.resolveContentProvider(getAuthority(), PackageManager.GET_META_DATA); Is this correct?

From Android reference for PackageManager.resolveContentProvider().

public abstract ProviderInfo resolveContentProvider (String name, int flags)

Since: API Level 1 Find a single content provider by its base path name. Parameters

name: The name of the provider to find.

flags: Additional option flags. Currently should always be 0.

Can someone confirm if i am doing something wrong or is it a bug.

Edit: everything works as expected when I upload my app for the first time - its only when I update the apk resulting in different version codes that this problem occurs.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Zip file provider class contains metadata that you can place in your AndroidManifest.zip file if the version number of the APK Expansion File does not match the version number of your APK.

<provider android:name=".APEZProvider"
              android:authorities="my.application.authority"
              android:exported="false"
              android:multiprocess="true"
    >
   <meta-data android:name="mainVersion"
              android:value="1"></meta-data>
   <meta-data android:name="patchVersion"
              android:value="2"></meta-data>
</provider>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...