dimanche 13 mars 2016

Google Classpath Explorer not working in Android

My problem is the same as How to find the package name given a class name?, only in Android. Given a String I'd like use Class.forName() to create an instance of an object of the class represented by the string. (i.e. If the String is "Foo" I'd like to create an Object o = Class.forName("Foo").newInstance();.) Easy if "Foo" is in the same package, but if it's not then for Class.forName() I need to programatically find the package that contains the class (i.e. com.myapplication.packagexyz.Foo) or I get ClassNotFoundException.

http://ift.tt/1V3aprV gives a great answer using Google Classpath Explorer, but as I mention in the comments over there it's not working on Android. It was a snap to get it working in a regular Java application with multiple packages, but on Android the application hangs.

Inside of private void findResources(SortedSet<String> resources, String rootPackageName, ResourceFilter resourceFilter, TreeSet<String> visitedPackages), which is part of Google Classpath Explorer, I added some logging after visitedPackages.add(rootPackageName); and noticed that the recursive method was spinning through an enormous directory structure. So it's running but I'm guessing that it doesn't return because it is going through my entire device.

classpath.findResources() takes a rootPackageName as a parameter, so I tried String[] resources = classpath.findResources("com/myapplicationname", regExpResourceFilter);. The function returned quickly but resources was empty. (I checked that my manifest contained package="com.myapplicationname" and all of the packages in my application start with "com.myapplicationname".)

I'm thinking that perhaps this rootPackageName isn't correct and Google Classpath Explorer isn't finding my application. Taking a look at Get Application Directory I used http://ift.tt/1V3aprZ and got "/data/user/0/com.myapplicationname". Plugging that into String[] resources = classpath.findResources("/data/user/0/com/myapplicationname", regExpResourceFilter); still produces an empty resources, although perhaps because this is just the data folder. http://ift.tt/1QVi4mY gives more information on the folder hierarchy, but even String[] resources = classpath.findResources("data", regExpResourceFilter); returns nothing.

Maybe Google Classpath Explorer simply doesn't work on Android. In any event, I have run out of ideas.





Aucun commentaire:

Enregistrer un commentaire