Android-runtime: Adding file to Android build at app/src/main/assets for `getAssets()`

Created on 2 Feb 2017  路  4Comments  路  Source: NativeScript/android-runtime

@bradmartin commented on Thu Feb 02 2017

Please, provide the details below:

Trying to build a plugin which is calling the following code:

 public static Cancellable fromAssetFileName(Context context, String fileName, OnCompositionLoadedListener loadedListener) {
        InputStream stream;
        try {
            stream = context.getAssets().open(fileName);
        } catch (IOException e) {
            throw new IllegalStateException("Unable to find file " + fileName, e);
        }
        return fromInputStream(context, stream, loadedListener);
    }

The public method to get to that ^^^ is just accepting a string for the fileName argument. The README for the android lib says to put the file at app/src/main/assets. I've tried creating an assets folder in app_resources which obviously doesn't package up there. I've tried at the root of the app folder and no good. So is there some way to add files (this is a .json file) to that location for an android build with NS. The android code calling getAssets() returns the AssetManager and is calling open() with the given file name. So how do we get files to be accessible with this in a NS app is the question now that I think about it 馃槃

Did you verify this is a real problem by searching Stack Overflow and the other open issues in this repo?

Yes.

Which platform(s) does your issue occur on?

Android

Please provide the following version numbers that your issue occurs with:

NS 2.5 everything - just updated.

Please tell us how to recreate the issue in as much detail as possible.

Create an app, try to create an .apk with a file located in app/src/main/assets at the output I suppose.

Is there code involved? If so, please share the minimal amount of code needed to recreate the problem.

No, just wrapping a lib.


@bradmartin commented on Thu Feb 02 2017

There is another method to go about, but was hoping to access the simplest of them with just passing the file name 馃憤


@bradmartin commented on Thu Feb 02 2017

And opening up platforms/android/src/main/assets and placing the .json file isn't really a good solution for new NS devs or anyone since it'll be blasted during a build/platform clean. Doing this does work, so the native lib is expecting the file to be located in the assets folder.

feature

Most helpful comment

@vjoao , @Pip3r4o ,

I created a small hook to avoid this issue:

https://github.com/rhanb/nativescript-dev-assets/

Hope it can help 馃槃

All 4 comments

Any news on this?

Hi @vjoao

This is part of a more complex task that we need to resolve (https://github.com/NativeScript/android-runtime/issues/561) At this moment, only resource files in App_Resources/Android will be copied in src/main/res.

The feature however won't be available in the upcoming 3.0 release. I'll let you know if there are news around it.

@vjoao , @Pip3r4o ,

I created a small hook to avoid this issue:

https://github.com/rhanb/nativescript-dev-assets/

Hope it can help 馃槃

Addressed by introducing the tns resources update command in the CLI that will migrate the App_Resources/Android subdirectory structure to support user-defined assets, java classes and resources - https://github.com/NativeScript/nativescript-cli/pull/3347

Was this page helpful?
0 / 5 - 0 ratings

Related issues

m-abs picture m-abs  路  3Comments

triniwiz picture triniwiz  路  4Comments

Plamen5kov picture Plamen5kov  路  5Comments

Plamen5kov picture Plamen5kov  路  4Comments

zahid-dapperapps picture zahid-dapperapps  路  3Comments