I am attempting to run "A very basic Android app example" from Mapsforge
I'm using Eclipse. Android SDK tools are above version 19.
I have done the following: I downloaded the jar files and placed them in the libs/ folder
My project is compiling just fine but when it runs I get the NoClassDefFoundError. I've been searching for an answer. I've tried adding the jars to the build path and tried to follow the suggestion How to fix the “NoClassDefFoundError” with ADT 17 and NoClassDefFoundError with Mapsforge 0.4.0 and Eclipse. Nothing helped.
Any Ideas? Thanks in advance!
The content of the MainActivity.java:
// name of the map file
private static final String MAPFILE = "austria.map";
private MapView mapView;
private TileCache tileCache;
private TileRendererLayer tileRendererLayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidGraphicFactory.createInstance(this.getApplication());
this.mapView = new MapView(this);
setContentView(this.mapView);
this.mapView.setClickable(true);
this.mapView.getMapScaleBar().setVisible(true);
this.mapView.setBuiltInZoomControls(true);
this.mapView.getMapZoomControls().setZoomLevelMin((byte) 10);
this.mapView.getMapZoomControls().setZoomLevelMax((byte) 20);
this.tileCache = AndroidUtil.createTileCache(this, "mapcache",
mapView.getModel().displayModel.getTileSize(), 1f,
this.mapView.getModel().frameBufferModel.getOverdrawFactor());
}
@Override
protected void onStart() {
super.onStart();
this.mapView.getModel().mapViewPosition.setCenter(new LatLong(48.120539, 16.269245));
this.mapView.getModel().mapViewPosition.setZoomLevel((byte) 12);
tileRendererLayer.setMapFile(getMapFile());
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.OSMARENDER);
this.mapView.getLayerManager().getLayers().add(tileRendererLayer);
}
@Override
protected void onStop() {
super.onStop();
this.mapView.getLayerManager().getLayers().remove(this.tileRendererLayer);
this.tileRendererLayer.onDestroy();
}
@Override
protected void onDestroy() {
super.onDestroy();
this.tileCache.destroy();
this.mapView.getModel().mapViewPosition.destroy();
this.mapView.destroy();
AndroidResourceBitmap.clearResourceBitmaps();
}
private File getMapFile() {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), MAPFILE);
return file;
}
My LogCat:
01-17 10:59:33.759: E/AndroidRuntime(29027): FATAL EXCEPTION: main
01-17 10:59:33.759: E/AndroidRuntime(29027): Process: com.example.mapsforgetest, PID: 29027
01-17 10:59:33.759: E/AndroidRuntime(29027): java.lang.NoClassDefFoundError: Failed resolution of: Lorg/mapsforge/map/reader/MapDatabase;
01-17 10:59:33.759: E/AndroidRuntime(29027): at org.mapsforge.map.layer.renderer.TileRendererLayer.<init>(TileRendererLayer.java:54)
01-17 10:59:33.759: E/AndroidRuntime(29027): at com.example.mapsforgetest.MainActivity.onStart(MainActivity.java:67)
01-17 10:59:33.759: E/AndroidRuntime(29027): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
01-17 10:59:33.759: E/AndroidRuntime(29027): at android.app.Activity.performStart(Activity.java:5949)
01-17 10:59:33.759: E/AndroidRuntime(29027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
01-17 10:59:33.759: E/AndroidRuntime(29027): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
01-17 10:59:33.759: E/AndroidRuntime(29027): at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-17 10:59:33.759: E/AndroidRuntime(29027): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-17 10:59:33.759: E/AndroidRuntime(29027): at android.os.Handler.dispatchMessage(Handler.java:102)
01-17 10:59:33.759: E/AndroidRuntime(29027): at android.os.Looper.loop(Looper.java:135)
01-17 10:59:33.759: E/AndroidRuntime(29027): at android.app.ActivityThread.main(ActivityThread.java:5221)
01-17 10:59:33.759: E/AndroidRuntime(29027): at java.lang.reflect.Method.invoke(Native Method)
01-17 10:59:33.759: E/AndroidRuntime(29027): at java.lang.reflect.Method.invoke(Method.java:372)
01-17 10:59:33.759: E/AndroidRuntime(29027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-17 10:59:33.759: E/AndroidRuntime(29027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-17 10:59:33.759: E/AndroidRuntime(29027): Caused by: java.lang.ClassNotFoundException: Didn't find class "org.mapsforge.map.reader.MapDatabase" on path: DexPathList[[zip file "/data/app/com.example.mapsforgetest-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
01-17 10:59:33.759: E/AndroidRuntime(29027): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
01-17 10:59:33.759: E/AndroidRuntime(29027): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-17 10:59:33.759: E/AndroidRuntime(29027): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
01-17 10:59:33.759: E/AndroidRuntime(29027): ... 15 more
01-17 10:59:33.759: E/AndroidRuntime(29027): Suppressed: java.lang.ClassNotFoundException: org.mapsforge.map.reader.MapDatabase
01-17 10:59:33.759: E/AndroidRuntime(29027): at java.lang.Class.classForName(Native Method)
01-17 10:59:33.759: E/AndroidRuntime(29027): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
01-17 10:59:33.759: E/AndroidRuntime(29027): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
01-17 10:59:33.759: E/AndroidRuntime(29027): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
01-17 10:59:33.759: E/AndroidRuntime(29027): ... 16 more
01-17 10:59:33.759: E/AndroidRuntime(29027): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
Mapsforge doesn't seem to do a very good job of explaining which libraries are actually required to get it working. I had the same issue, and your problem is just that you don't have all of the required dependencies installed.
Even though you're not directly using classes from mapsforge-map-reader-0.5.0.jar, it is a dependency of the TileRendererLayer class in mapsforge-map-0.5.0.jar. You'll see this if you pop into the source code of TileRendererLayer.java at line 54. Line 29 lists the import for the MapDatabase class, which is what's throwing the exception.
I think your example will run fine without the other two installable libraries, which are mapsforge-map-writer-0.5.0.jar and mapsforge-map-awt-0.5.0.jar. Including them could benefit you in the future, if you eventually have to use those classes too, but mapsforge-map-writer-0.5.0.jar seems to be a little wonky (it includes classes both from the writer package and the core package). Compiling them yourself would probably be a better option.
All of the library files are available in the same place as the first three, the CI server (release 0.5.0). Once you add them as libraries the same way you added the others, you shouldn't have any trouble here.