Building a maven project in android studio

  android, android-studio, dependencies, java, maven

After having built built the jars of dain/leveldb (pure java implementation of leveldb (key/value db used to store local storage in chronium)):

https://github.com/dain/leveldb

using java 8, and importing them into a stub android project (build/install were successful), I got the following exception:

com.example.stubproject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.stubproject, PID: 25695
    java.lang.NoSuchMethodError: No virtual method keySet()Ljava/util/concurrent/ConcurrentHashMap$KeySetView; in class Ljava/util/concurrent/ConcurrentHashMap; or its super classes (declaration of 'java.util.concurrent.ConcurrentHashMap' appears in /system/framework/core-oj.jar)
        at org.iq80.leveldb.util.Finalizer.destroy(Finalizer.java:104)
        at org.iq80.leveldb.impl.TableCache.close(TableCache.java:107)
        at org.iq80.leveldb.impl.DbImpl.close(DbImpl.java:270)
        at com.example.stubproject.MainActivity.onCreate(MainActivity.java:49)
        at android.app.Activity.performCreate(Activity.java:7327)
        at android.app.Activity.performCreate(Activity.java:7318)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3095)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3258)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7073)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

This happens when I try to close the database:

// Make sure you close the db to shutdown the
// database and avoid resource leaks.
try {
    db.close(); // <= EX THROWN HERE
} catch (IOException e) {
    e.printStackTrace();
}

Full code (in onCreate() of MainActivity):

    Options options = new Options();
    options.createIfMissing(true);
    DB db = null;
    try {
        String cwd = getApplicationInfo().dataDir;
        Log.d(TAG, "cwd: " + cwd);
        String dirName = "leveldb_demo";
        File directory = new File(cwd + File.separator + dirName);
        if (! directory.exists()){
            directory.mkdir();
            // If you require it to make the entire directory path including parents,
            // use directory.mkdirs(); here instead.
        }
        db = factory.open(new File(cwd + File.separator + dirName), options);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        Log.d(TAG, "writing to db:");
        db.put(bytes("Tampa"), bytes("rocks"));
        Log.d(TAG, "reading from db:");
        String value = asString(db.get(bytes("Tampa")));
        Log.d(TAG, "read: " + value);
    } finally {
        // Make sure you close the db to shutdown the
        // database and avoid resource leaks.
        try {
            db.close(); // <= EX THROWN HERE
        } catch (IOException e) {
            e.printStackTrace();
        }

After some research,

Android app building with the wrong JDK(?) somehow

How can it be, that ConcurrentHashMap has no keySet() method?

"Undefined reference: .. ConcurrentHashMap.keySet()" when building in Java 8

Which Android versions run which Java versions?

the takeaway idea is that there are differences between the jdk and android sdk, and that the library should be built purely in android.
I copied the sources of leveldb to another directory and opened it with android studio. Setup looks like:

enter image description here

(jdk 1.8 replaced with API 26).

How can I add the dependencies inside the pom and export the build as a jar?

Source: Android Questions

LEAVE A COMMENT