jcricket / gwt-syncproxy

Provides Synchronous and Asynchronous access to GWT-RPC servlets from Java and Android
http://www.blueesoteric.com/open-source/gwt-syncproxy
Apache License 2.0
23 stars 14 forks source link

SyncProxy 0.5 in Android N (7.0) NullPointerException: Attempt to get length of null array #50

Open rvijapurapu opened 8 years ago

rvijapurapu commented 8 years ago

I have identified the issue when running an Application using SynxProxy 0.5 on a Nexus device running Android N. This crashes the application.

Here is what I have found up until now:

String classPath = System.getProperty("java.class.path"); returns "." i.e. root directory of the device. Which seems to be off-limits to the application in Android N.

So, when SyncProxy tries File.list(XXX) methods in RpcPolicyFinder - it always returns null in Android N.

Hence, when searchPolicyFileInDirectory(String path) is called with the following code:

public static Map<String, String> searchPolicyFileInDirectory(String path) {
        Map<String, String> result = new HashMap<String, String>();

        String policyName = null;
        File f = new File(path);
        String[] children = f.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.endsWith(GWT_PRC_POLICY_FILE_EXT)) {
                    return true;
                }
                return false;
            }
        });
// The children is always `null` - in Android N. So accessing the children using:
       for (String child : children) {

// Will cause NullPointerException: Attempt to get length of null array
[...]
}

I have tested this on Android 4.4, Android 5.0.1, Android 5.1.1, Android 6.0 - these versions do not have the same problem. Only Android 7.0

Options to resolve this further is to pass through the application directory to the Library someway and then use this to list the gwt.rpc files.

I have not yet investigated which permissions could resolve the issue. While not an ideal solution, I have used a simple check if(children!=null) to workaround the problem in my code as I do not store the gwt.rpc files in my application.

miniagarwal09 commented 7 years ago

Getting the same error. Can u tell me more precisely about how to work around this error?

rvijapurapu commented 7 years ago

This is how my RpcPolicyFinder.java looks like now in SyncProxy-Android project.. I downloaded the sources and added the check I mentioned above (see code).

There is a possibility that there are File permissions which can be set which avoids this issue. I didn't get a chance to test them out. If you are successful in this approach, we would much appreciate you updating this issue.

public static Map<String, String> searchPolicyFileInDirectory(String path) {
        Map<String, String> result = new HashMap<String, String>();

        String policyName = null;
        File f = new File(path);
        String[] children = f.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.endsWith(GWT_PRC_POLICY_FILE_EXT)) {
                    return true;
                }
                return false;
            }
        });

        if(children!=null) {
            for (String child : children) {
                policyName = child.substring(0, child.length()
                        - GWT_PRC_POLICY_FILE_EXT.length());
                try {
                    result.putAll(parsePolicyName(policyName, new FileInputStream(
                            new File(path, child))));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return result;
    } 
miniagarwal09 commented 7 years ago

Thanks for instant reply. :D I will try it out.

gaurav19643 commented 6 years ago

I have the same problem, It runs below nougat version, please someone help

rvijapurapu commented 6 years ago

Two things to try:

  1. See if you are able to set Files Read permission - <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> in AndroidManifest.xml see if it fixes the issue. If it does, please do report back here.

  2. Try the code above.