Added support for asset loading on Android.

Requires passing the activity's AssetManager object from Java to GNUstep by calling +[NSBundle setJavaAssetManager:withJNIEnv:], which then enables the following features:

- NSBundle main bundle resource paths support for Android assets, e.g. for pathForResource:ofType:, URLForResource:ofType: and related methods.
- NSBundle main bundle info dictionary support if Info.plist exists in Android assets.
- -initWithContentsOfFile: and related methods support for reading Android assets from main bundle in various classes (e.g. NSData, NSDictionary, NSArray, etc.).
- NSFileManager fileExistsAtPath:(isDirectory:) and isReadableFileAtPath: return YES for main bundle asset / asset directory paths.
- NSFileHandle support for reading Android assets from main bundle.
- NSDirectoryEnumerator support for enumerating Android assets from main bundle. Note that recursion into subdirectories is currently not supported by the native Android asset manager API (see https://issuetracker.google.com/issues/37002833).

Also adds support for automatic NSProcessInfo initialization on Android with a fake executable path "/data/data/<app identifier>/exe" (as Android apps don't have a real executable path), and tweaks main bundle initialization to allow that path. Main bundle resource paths are prefixed by "/data/data/<app identifier>/Resources".
This commit is contained in:
Frederik Seiffert 2019-05-09 20:16:18 +02:00
parent ecbecbeabd
commit 3b60b1a8be
10 changed files with 440 additions and 46 deletions

View file

@ -36,6 +36,10 @@ extern "C" {
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#ifdef __ANDROID__
#include <android/asset_manager_jni.h>
#endif
@class NSString;
@class NSArray;
@class NSDictionary;
@ -540,6 +544,38 @@ GS_EXPORT NSString* const NSLoadedClasses;
ofType: (NSString*)extension
inDirectory: (NSString*)bundlePath;
/** Cleans up the path cache for the bundle. */
- (void) cleanPathCache;
#ifdef __ANDROID__
/**
* Sets the Java Android asset manager.
* The developer can call this method to enable asset loading via NSBundle.
*/
+ (void) setJavaAssetManager:(jobject)jassetManager withJNIEnv:(JNIEnv *)env;
/**
* Returns the native Android asset manager.
*/
+ (AAssetManager *) assetManager;
/**
* Returns the Android asset for the given path if path is in main bundle
* resources and asset exists.
* The returned object must be released using AAsset_close().
*/
+ (AAsset *)assetForPath:(NSString *)path;
/**
* Returns the Android asset dir for the given path if path is in main bundle
* resources and the asset directory exists.
* The returned object must be released using AAssetDir_close().
*/
+ (AAssetDir *)assetDirForPath:(NSString *)path;
#endif /* __ANDROID__ */
@end
#endif /* GNUSTEP */