Android NDK STL Library tuning and System.load

Source: Internet
Author: User

There are a lot of STL libraries available for Android, but the Gnustl feature is undoubtedly the most comprehensive.

Baidu, found a lot of people on the NDK using STL Library is not comprehensive, often gunstl static over-inking, therefore, I am here to tell the use of Share library.

Application.mk

Android.mk

Ndkstl.cpp

Preprocessing some functions and variables


Then execute the Code

Int testvector () {Vector<string>  catlst;int i = 0;char temp[max_ buffer_size];for  (i = 0; i < 10; ++i) {memset (temp,0,max_buffer_size); sprintf (temp, "category_%d", (i+1)); String s (temp); Catlst.push_back (temp);} if (!catlst.empty ()) { vector<string>::iterator result; result = find ( Catlst.begin (), Catlst.end (), "Category_4"),  if (Result==catlst.end ())  { cout<< "Query failed" < <endl; log::e ("STL", "Query Failed"); }else{ cout<< "Query succeeded:" <<result-catlst.begin () << Endl; string s ("Query succeeded");  s.append (": Index of location ="),  char buf[16]; sprintf (buf, "%d", Result-catlst.begin ());  s.append (BUF);  log::e ("STL", S.c_str ()); }/*for  (i = 0;  i < catlst.size ();  ++i) {string item = catlst[i];outprint (item);} */ for_each (Catlst.begin (), Catlst.end (), outprint);  int s1&nbsP;= catlst.size ();  catlst.push_back ("Category_4");  if (S1>catlst.size ())  {   Log::i ("vector", "delete succeeded"),  }else{  log::i ("vector", "delete sunning");  } catlst.clear ();} else{cout<< "Vector data store Error" <<endl; log::e ("STL", "Vector data store Error");}} Void testmap () {Map<string,string> idmap;idmap.insert (pair<string, string> ("HX9182",   "Zhangsan"), idmap["HO8081"] =  "Harry"; idmap["HX9192"] =   "Harfter";}

The STL is also loaded in Java code

Static{system.loadlibrary ("gnustl_shared"); System.loadlibrary ("Ndkstl");}

Furthermore, it is problematic to use LoadLibrary for JNI NDK so fault tolerance.

We can choose not to invoke the Jni method when the so module is not present, and the workaround is applicable system.load

JNI so location in "/data/data/" +getpackagename () + "/lib/directory"

public class mainactivity extends fragmentactivity {private string  Checkjniso, @Overrideprotected  void oncreate (bundle savedinstancestate)  {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Checkjniso = checkjniso ("libndkstl.so"); if (! Textutils.isempty (Checkjniso))  {system.load (Checkjniso);}} @Overrideprotected  void onresume ()  {super.onresume (); if (! Textutils.isempty (Checkjniso))  {     ' Javamain ();}} Used to detect if the  so module exists, and if it does not exist, you can not call Soprivate string checkjniso (string soname) {File filesdir  = getfilesdir (); if (filesdir!=null) {string datapath = filesdir.getparentfile (). GetAbsolutePath ();//jni so position in   "/data/data/" +getpackagename () + "/lib/directory" file f = new  file (datapath+ "/lib/", soName);//"libndkstl.so"); if (F!=null && f.exists ()) {return  F.getabsolutepath ();}} Return null;} Private native void javamain (); Static{    system.loadlibrary ("gnustl_shared ");//system.loadlibrary (" Ndkstl ");}}

[-------------------------------------------------------------]

Error Resolution:

STL libraries are not automatically loaded by default, you may encounter a header file in your project that is not found, workaround




Android NDK STL Library tuning and System.load

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.