简体   繁体   中英

GeoNames and JDOM request java.rmi.RemoteException after shrinkResources

I'm using the GeoNames Java Client to autocomplete place names in an Android app: the user types some characters and a list of suggestions appears.
The library jdom-1.0.jar is also required, to parse the XML results that GeoNames fetch.

Everything works fine except when I add shrinkResources true in build.gradle .
At this point some critical class get lost, I don't know which one... All I know is that WebService.search() fails, but I can't understand why does it fail. In fact java.rmi.RemoteException is requested here, but the RMI library is not supported in Android, therefore a ClassNotFoundException is thrown instead of an exception stack trace.

PlaceFinder.java:

public class PlaceFinder extends AppCompatAutoCompleteTextView {
    ToponymSearchCriteria searchCriteria;
    public PlaceFinder( Context context, AttributeSet attributeSet ) {
        super( context, attributeSet );
        Adapter adapter = new Adapter(context, android.R.layout.simple_spinner_dropdown_item);
        setAdapter(adapter);
        WebService.setUserName("demo");
        searchCriteria = new ToponymSearchCriteria();
        searchCriteria.setMaxRows(3);
    }
    class Adapter extends ArrayAdapter<String> implements Filterable {
        List<String> places;
        Adapter( Context context, int layout ) {
            super( context, layout );
            places = new ArrayList<>();
        }
        @Override
        public int getCount() {
            return places.size();
        }
        @Override
        public String getItem(int index) {
            return places.get(index);
        }
        @Override
        public Filter getFilter() {
            return new Filter() {
                @Override
                protected FilterResults performFiltering( CharSequence constraint ) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint != null) {
                        searchCriteria.setNameStartsWith(constraint.toString());
                        try {
                            ToponymSearchResult searchResult = WebService.search(searchCriteria); // search() fails
                            places.clear();
                            for( Toponym toponym : searchResult.getToponyms() ) {
                                places.add( toponym.getName() + ", " + toponym.getCountryName() );
                            }
                            filterResults.values = places;
                            filterResults.count = places.size();
                        } catch( Exception e ) {
                            e.printStackTrace(); // The class "java.rmi.RemoteException" is requested but missing
                        }
                    }
                    return filterResults;
                }
                @Override
                protected void publishResults( CharSequence constraint, FilterResults results ) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }
            };
        }
    }
}

The fatal exception (retraced):

FATAL EXCEPTION: Filter
    Process: com.example, PID: 32084
    java.lang.NoClassDefFoundError: Failed resolution of: Ljava/rmi/RemoteException;
        at com.example.PlaceFinder$Adapter$1.performFiltering(:57)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.os.HandlerThread.run(HandlerThread.java:61)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "java.rmi.RemoteException" on path: DexPathList[[zip file "/data/app/com.example-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at org.jdom.JDOMException.getNestedException(:294)
        at org.jdom.JDOMException.getMessage(:144)
        at java.lang.Throwable.getLocalizedMessage(Throwable.java:188)
        at java.lang.Throwable.toString(Throwable.java:355)
        at java.lang.Throwable.printStackTrace(Throwable.java:315)
        at java.lang.Throwable.printStackTrace(Throwable.java:282)
        at org.jdom.JDOMException.printStackTrace(:216)
        at java.lang.Throwable.printStackTrace(Throwable.java:236)
        at org.jdom.JDOMException.printStackTrace(:189)
        at com.example.PlaceFinder$Adapter$1.performFiltering(:57)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.os.HandlerThread.run(HandlerThread.java:61)
        Suppressed: java.lang.ClassNotFoundException: java.rmi.RemoteException
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                        ... 15 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

How to solve this double layer problem?

I still don't know which essential class or method is removed by shrinkResources , but the solution I found after hours of attempts is simply add to proguard-rules.pro the rule:

-keep class org.jdom.input.* { *; }

That's it.


Meanwhile I also found that is possible to upgrade JDOM to a more recent version, that is compatible with GeoNames too. Instead of using jdom-1.0.jar provided by GeoNames, add to build.gradle dependencies:

implementation 'org.jdom:jdom:1.1.3'

Also In this case the above ProGuard rule is needed.

JDOM 2 seems instead no more compatible with GeoNames 1.1.14.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM