"Walk through the pit" Android Studio problem solution for JNI calls and running flashback unable to load library

Source: Internet
Author: User

It is believed that many small partners have encountered the various pits that invoke JNI in Android development because we have to use third-party library so files in many places, whereas third-party officials usually only give an integrated approach to the ADT environment, while Google's pro-son Android Studio uses the Gradle method by default, which is very different from the way ADT was edited, so how do you import so files in Andorid studio?

In Android Studio we may use the jar package and so file way, for the jar package may touch more, just need us to convert the project to project display mode, open the Libs folder under the app, import. Then add the jar package for our engineering dependency.

Well, the landlord does not want to digress. For so file is also very simple, only need to build a jnilibs under App/src/main, and then copy our third-party so file into it, it is important to note that we generally have several CPUs, but no longer the previous only ARMV5, there are currently 7 kinds. ARMV5,ARMV7 (since 2010), x86 (from 2011 onwards), MIPS (from 2012 onwards), Armv8,mips64 and x86_64 (from 2014), each of which is associated with a corresponding ABI. On an Android system, each CPU architecture corresponds to a abi:armeabi,armeabi-v7a,x86,mips,arm64-v8a,mips64,x86_64. The names of these packages cannot be changed casually.

While we are in the development, As much as possible, each ABI-optimized. So file should not be mixed, actually providing the corresponding. So file for each ABI is actually what the SDK provider should do, but perhaps you will not be so lucky, perhaps your SDK provider will be like the landlord encountered, just to provide you with a armeabi way. s o file, amount, yes, you write a small demo the function of the SDK may be available, and then when you introduce your demo into your project, you may always encounter such problems, for example, the most common is unsatisfiedlinkerror, Of course you may also encounter dlopen:failed and other forms of crash or low performance. And you may be running on some phones without reporting any errors. For example, the third party to get the SDK, only provided the Armeabi under the so file, the landlord into the project, (the landlord project before the support of arm64-v8a and other ways of ABI) using a relatively old version of the mobile phone run, Ok,no problem! However, when used in the Xiaomi series of any cell phone, run, direct crash, Cause at the time of initialization can not find some. so files, resulting in the inability to use system.loadlabray loading, do not know how to solve the small partners, but so a problem, so that the landlord and some of the same developers are scratching the head, the landlord is aware of its reasons, and do not know its solution, which is the most head It hurts.

Below is an error message from Android Studio.

08-21 11:12:48.413 7971-7971/com.hkyc.shouxinteacher.ischool e/androidruntime:fatal Exception:main Pr Ocess:com.hkyc.shouxinteacher.ischool, PID:7971Java.lang.UnsatisfiedLinkError:dalvik. System. Pathclassloader[dexpathlist[[zip file"/DATA/APP/COM.HKYC.SHOUXINTEACHER.ISCHOOL-2/BASE.APK"],nativelibrarydirectories=[/data/app/                                                                                   Com.hkyc.shouxinteacher.ischool-2/lib/arm64,/vendor/lib64,/system/lib64]] couldn ' t find ' libgnustl_shared.so ' At Java.lang.Runtime.loadLibrary (runtime.java:366) at Java.lang.System.loadLibrary (Sy Stem.java:988) at Com.idtechinfo.shouxiner.App.on Create (App.java:92) at android.app.Instrumentation.cal Lapplicationoncreate (Instrumentation.java:1012) at Android.app.ActivityThread.hand Lebindapplication (Activitythread.java:4580) at Android.app.ActivityThread.acce ss$(activitythread.java:154) at Android.app.activitythread$h.ha Ndlemessage (Activitythread.java:1376) at Android.os.Handler.dispatchMess Age (Handler.java:102) at Android.os.Looper.loop (LOOPER.J Ava:135) at Android.app.ActivityThread.main (Activitythread.java:5283) at Java.lang.reflect.Method.invoke (Native Method) at Java.lang.reflect.Met Hod.invoke (Method.java:372) at Com.android.internal.os.ZygoteI Nit$methodandargscaller.run (Zygoteinit.java:908) at Com.android.internal.os.ZygoteI Nit.main (Zygoteinit.java:703)

Well, really a headache, the landlord adopted a variety of solutions are not resolved, because Android Studio by default will be all the ABI support packaged to the APK, because the landlord gets the third-party SDK is not comprehensive, so it is inevitable to encounter such a wonderful problem.

So, how to solve it?

Landlord through the Internet to provide some solutions, can be added in Gradle configuration as follows:

1 Android {2     ... 3 Splits {4 Abi {5Enabletrue6 Reset ()7Include ' x86 ', ' x86_64 ', ' armeabi-v7a ', ' arm64-v8a '//Select ABIs to build APKs for8universalapktrue //generate an additional APK, contains all the ABIs9         }Ten     } One  A     //map for the version code -Project.ext.versionCodes = [' Armeabi ': 1, ' armeabi-v7a ': 2, ' arm64-v8a ': 3, ' MIPS ': 5, ' Mips64 ': 6, ' x86 ': 8, ' x86_64 ': 9] -  theAndroid.applicationVariants.all {variant- -         //assign different version code for each output -Variant.outputs.each {output- -Output.versioncodeoverride = +Project.ext.versionCodes.get (Output.getfilter (Com.android.build.OutputFile.ABI), 0) * 1000000 +Android.defaultConfig.versionCode -         } +     } A}

But the fact is: can not find the package name!! What the hell is that?

Landlord in a variety of wall after, I hope you do not again in this low-level problem on the beaten, the landlord's solution is through the Build.gradle set let APK package package only Armeabi package. so file,

Add code to:

1  2 3 android{  4  5    ..... 6 7     defaultconfig {  8        ndk {  9             abifilters ' Armeabi ', ' x86 ' Ten         }one}    

Of course, although the opportunistic in the Aremabi can support all CPU models, but no doubt the use of a variety of models specific performance optimization, in order to let it does not flash, landlord can only temporarily adopt such methods. If you have a better way, also want to be able to share in the comment area, thank you.

Reprint please attach this article link in striking position: http://www.cnblogs.com/liushilin/p/5792505.html

"Walk through the pit" Android Studio problem solution for JNI calls and running flashback unable to load library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.