Nativescript-cli: openFile hangs

Created on 22 Jul 2019  ยท  4Comments  ยท  Source: NativeScript/nativescript-cli

After upgrading to latest version of nativescript and node openFile hangs with the following log:

Error: Error in openFile: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:605) androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579) androidx.core.content.FileProvider.getUriForFile(FileProvider.java:417) com.tns.Runtime.callJSMethodNative(Native Method) com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1242) com.tns.Runtime.callJSMethodImpl(Runtime.java:1122) com.tns.Runtime.callJSMethod(Runtime.java:1109) com.tns.Runtime.callJSMethod(Runtime.java:1089) com.tns.Runtime.callJSMethod(Runtime.java:1081) com.tns.gen.java.lang.Object_vendor_14095_32_ClickListenerImpl.onClick(Object_vendor_14095_32_ClickListenerImpl.java:18) android.view.View.performClick(View.java:6600) android.view.View.performClickInternal(View.java:6577) android.view.View.access$3100(View.java:781) android.view.View$PerformClick.run(View.java:25912) android.os.Handler.handleCallback(Handler.java:873) android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:193) android.app.ActivityThread.main(ActivityThread.java:6923) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870) [file:///data/data/org.nativescript.tester/files/app/vendor.js] Error: Error in openFile: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:605) androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579) androidx.core.content.FileProvider.getUriForFile(FileProvider.java:417) com.tns.Runtime.callJSMethodNative(Native Method) com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1242) com.tns.Runtime.callJSMethodImpl(Runtime.java:1122) com.tns.Runtime.callJSMethod(Runtime.java:1109) com.tns.Runtime.callJSMethod(Runtime.java:1089) com.tns.Runtime.callJSMethod(Runtime.java:1081) com.tns.gen.java.lang.Object_vendor_14095_32_ClickListenerImpl.onClick(Object_vendor_14095_32_ClickListenerImpl.java:18) android.view.View.performClick(View.java:6600) android.view.View.performClickInternal(View.java:6577) android.view.View.access$3100(View.java:781) android.view.View$PerformClick.run(View.java:25912) android.os.Handler.handleCallback(Handler.java:873) android.os.Handler.dispatchMessage(Handler.java:99) android.os.Looper.loop(Looper.java:193) android.app.ActivityThread.main(ActivityThread.java:6923) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)

I tested with a newly created project and test program for android:
TS:
`import { EventData } from "tns-core-modules/data/observable";
import { Page } from "tns-core-modules/ui/page";
import {FileSystemAccess} from "tns-core-modules/file-system/file-system-access"
const fsa=new FileSystemAccess()

import { openFile} from "tns-core-modules/utils/utils"

class ViewModel {
onTest(){
let path="/storage/emulated/0/Download/test.txt"
fsa.writeText(path,"tester")
console.log(fsa.fileExists(path))
openFile(path)
}
}

export function navigatingTo(args: EventData) {
const page = args.object;
page.bindingContext = new ViewModel()
}
`

XML:
<Page navigatingTo="navigatingTo"> <Button text="Test" tap="{{onTest}}"/> </Page>

tns doctor shows:

โˆš Your ANDROID_HOME environment variable is set and points to correct directory. โˆš Your adb from the Android SDK is correctly installed. โˆš The Android SDK is installed. โˆš A compatible Android SDK for compilation is found. โˆš Javac is installed and is configured properly. โˆš The Java Development Kit (JDK) is installed and is configured properly. โˆš Local builds for iOS can be executed only on a macOS system. To build for iOS on a different operating system, you can use the NativeScript cloud infrastructure. โˆš Getting NativeScript components versions information... โˆš Component nativescript has 6.0.1 version and is up to date. โˆš Component tns-core-modules has 6.0.1 version and is up to date. โˆš Component tns-android has 6.0.0 version and is up to date. โˆš Component tns-ios has 6.0.1 version and is up to date.

question android

Most helpful comment

I was having this problem too. Make sure you have a file provider defined in your AndroidManifest.xml that helped me:

        <provider android:name="androidx.core.content.FileProvider" android:authorities="<YOUR PACKAGE NAME GOES HERE>.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
        </provider>

^^ that is the androidx version (nativescript 6+) if you are using nativescript < 6 use something like this

        <provider android:name="android.support.v4.content.FileProvider" android:authorities="<YOUR PACKAGE NAME GOES HERE>.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
        </provider>

don't forget to fill in the <YOUR PACKAGE NAME GOES HERE>

All 4 comments

I was having this problem too. Make sure you have a file provider defined in your AndroidManifest.xml that helped me:

        <provider android:name="androidx.core.content.FileProvider" android:authorities="<YOUR PACKAGE NAME GOES HERE>.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
        </provider>

^^ that is the androidx version (nativescript 6+) if you are using nativescript < 6 use something like this

        <provider android:name="android.support.v4.content.FileProvider" android:authorities="<YOUR PACKAGE NAME GOES HERE>.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
        </provider>

don't forget to fill in the <YOUR PACKAGE NAME GOES HERE>

@DanLatimer, thanks for the hint.

My manifest file starts with:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="__PACKAGE__"

Hence I used "__PACKAGE__" as the (placeholder) for the package name.
In the manifest file entered your code inside tags application:

<application...>
...
<provider android:name="androidx.core.content.FileProvider" android:authorities="__PACKAGE__.provider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
        </provider>
        </provider>

</application>

When building I get an error that xml/provider_paths.xml is missing., I added this file Android/main/res/xml/provider_paths.xml with the following content:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
    <files-path name="files" path="."/>
</paths>

Now it worked. I hope this can help other people.

I suggest your and mine additions are added to the plugin ?!
Thx for your reaction.

@jokro, @DanLatimer

Thank you for sharing this!

I'm closing this thread, you can log a separate issue in tns-core-modules and discuss if your additions can be added.

Also, if you are saving files to temp() folder (cache folder) you need to also whitelist it

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
    <files-path name="files" path="."/>
    <cache-path name="cache" path="." />
</paths>

dp

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kefahB picture kefahB  ยท  3Comments

Fatme picture Fatme  ยท  3Comments

trodellez picture trodellez  ยท  3Comments

leevigraham picture leevigraham  ยท  3Comments

NickIliev picture NickIliev  ยท  3Comments