简体   繁体   中英

Android app exit while loading external library

I'm trying to add opencv4 to my android app, and did everything as in this post.

I added System.loadLibrary to the begining of my kotlin file s below:

class MainActivity : AppCompatActivity() {
    companion object {
        init {
            System.loadLibrary("opencv_java4")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) { }

But the app exit after launching with the below:

09/22 16:08:04: Launching 'app' on Pixel 3a API 29.
$ adb shell am start -n "hasan.tts_mobile/hasan.tts_mobile.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Waiting for process to come online...
Timed out waiting for process to appear on Pixel_3a_API_29 [emulator-5554].

Thanks to this answer , it looks there is something changed in opencv 4 so that all existing tutorials talking about importing java folder as module, while actually the one required to be imported is the skd folder itself.

You can use JavaCV that is a wrapper of OpenCV, or do OpenCV manually as below:

1- From here download OpenCV – 4.1.1 for Android

2- Extract the folder, it will be extracted to OpenCV-android-sdk , that contains the following:

Hasans-Air:OpenCV-android-sdk hasan$ ls
LICENSE     README.android  samples     sdk

3- Head to your project at Android Studio

4- File -> New -> Import Module

5- Select the sdk folder in the extracted folder in point 2, as shown the proposed module name is :sdk but you can rename it like :sdkOpenCV4 , do not forget the : in front of the name

在此处输入图像描述

6- Right click the app , select Open Module Settings , select Dependencies, then click the app module, in the Declared Dependencies tap click + then you'll see the OpenCV module there, click add it, click Apply then Add :

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

7- From OpenCV SDK copy the sdk/native/libs folder, and go to tour application folder src/main and paste it inside it, then rename the libs folder to jnilibs , so that in your application src/main folder you'll be having:

在此处输入图像描述

8- In the OnCreate you can add a check before start using it:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val ocvLoaded = OpenCVLoader.initDebug()
        if (ocvLoaded) {
            Toast.makeText(
                this@MainActivity, "OpenCV loaded",
                Toast.LENGTH_SHORT
            ).show()
        } else {
            Toast.makeText(
                this@MainActivity, "Unable to load OpenCV",
                Toast.LENGTH_SHORT
            ).show()
            Log.d("openCV", "loader: ${OpenCVLoader.initDebug()}")
        }
    }
}

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