简体   繁体   中英

Cannot get to work .so shared library in android project

Absolutely new to android development and java so problem may be trivial, but I already confused about what to try or read or google next so please help. Trying to create an application that should be doing some batch processing on selected set of images. Found a library that perfectly suit my needs, connected it and wrote a simple test, that had to check out if everything works as it should:

if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK && null != data) {
        Uri selectedImageUri = data.getData();
        m_ImagePath = getRealPathFromUri(selectedImageUri);

        try {
            m_MagickImage = new MagickImage(new ImageInfo(m_ImagePath));
            m_EffectImage = m_MagickImage.rotateImage(90);
            m_Bitmap = MagickBitmap.ToReducedBitmap(m_EffectImage, 1000);
        } catch (MagickException e) {
            Log.d(LOGTAG, e.toString());
        }

        ImageView imageview = (ImageView) findViewById(R.id.imageView);
        imageview.setImageBitmap(m_Bitmap);
        //imageview.setImageURI(selectedImageUri);

        openFileButton.setVisibility(View.GONE);
    }

Project builds completely fine with no warnings, but when I deployed it on real hardware and clicked button assigned to written test, I got the following error in logcat:

JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring
    in call to GetStringUTFChars
    from void magick.ImageInfo.setFileName(java.lang.String)
    "main" prio=5 tid=1 Runnable

And 200 strings more of system information and warnings (don't think they are important but can provide everything needed on demand).

Also after that run i got plenty of red warnings like

cannot resolve corresponding jni function Java_magick_ImageInfo_setFileName

In almost every java file related to Android-Imagemagick.

Library was connected as .jar file, following instructions I found in this thread. Here's my setup:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'
    testCompile 'junit:junit:4.12'
}

and

sourceSets {
        main {
            java.srcDirs = ['src/main/java']
        }
    }

In build.gradle.

armeabi.jar with .so files in 'libs' directory in root of project, 'fakeawt' and 'magick' folders with wrapper classes in 'src/main/java', and

static {
        System.loadLibrary("imagemagick");
    }

At the beginning of Main.java.

Any idea what might go wrong and how to fix it properly?

I suggest if your using Android studio follow the project structure: App |->src |->jnilibs here copy all the folders same as from https://github.com/paulasiimwe/Android-ImageMagick/tree/master/libs and paste into jnilibs(libs) than add follow line in app build.gradle

sourceSets.main {
jniLibs.srcDir 'src/main/libs' // use the jni .so compiled from the manual ndk-build command
jni.srcDirs = [] //disable automatic ndk-build call
}

sync gradle

don't copy jni files in your project ,It is for your custom changes in library. Set NDK path from the settings.

Sorry, the issue wasn't relevant to ndk or shared library. It was just deprecated getRealPathFromUri() function, that always returns NULL value when used with modern android 5+ image chooser.

Also passing NULL pointer to JNI method always causes segfault for some reason, so these values must be verified in advance.

Solved by replacing appropriate functions according to this answer and creating if-else statement to check if returned real Path to file is not NULL.

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