码迷,mamicode.com
首页 > 移动开发 > 详细

Android NDK STL 库调与 System.load

时间:2015-01-27 00:43:55      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

对于Android可使用的STL库有很多,但gnustl功能无疑是最全面。

百度一下,发现很多人对ndk 使用stl库很不全面,往往gunstl static 过分着墨,因此,我这里之讲述 share库的使用。

技术分享

Application.mk

技术分享

Android.mk

技术分享

ndkstl.cpp

技术分享

预处理一些函数和变量

技术分享


然后执行代码

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<<"查询失败"<<endl;
			 Log::E("STL","查询失败");
		 }else{
			 cout<<"查询成功:"<<result-catlst.begin()<<endl;
			 string s("查询成功");
			 s.append(":所在位置索引=");
			 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 = catlst.size();
		 catlst.push_back("Category_4");
		 if(s1>catlst.size())
		 {
			  Log::I("Vector","删除成功");
		 }else{
			  Log::I("Vector","删除晒白");
		 }
		 catlst.clear();
	}else{
		cout<<"vector数据存储出错"<<endl;
		 Log::E("STL","vector数据存储出错");
	}

}


void testMap()
{
	map<string,string> idMap;
	idMap.insert(pair<string, string>("HX9182", "Zhangsan"));
	idMap["HO8081"] = "王五";
	idMap["HX9192"] =  "Harfter";
}

在java代码中也要加载stl

static{
		System.loadLibrary("gnustl_shared");
		System.loadLibrary("ndkstl");
	}

此外,说道这里,对于jni ndk so容错使用 loadLibrary是有问题

我们在so模块不存在时,可以选择不调用jni方法,解决方法时适用System.load

//jni so位置在 "/data/data/"+getPackageName()+"/lib/目录下"

public class MainActivity extends FragmentActivity {

	private String checkJNISo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		checkJNISo = checkJNISo("libndkstl.so");
		if(!TextUtils.isEmpty(checkJNISo)) 
		{
			System.load(checkJNISo);
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(!TextUtils.isEmpty(checkJNISo)) 
		{
		    `javaMain();
		}
	}
	//用于检测 so模块是否存在,如果不存在,可以不调用so
	private String checkJNISo(String soName)
	{
		File filesDir = getFilesDir();
		if(filesDir!=null)
		{
			String dataPath = filesDir.getParentFile().getAbsolutePath();
			//jni so位置在 "/data/data/"+getPackageName()+"/lib/目录下"
			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");
	}
}

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

错误解决:

stl 库默认不是自动加载的,在项目中可能遇到header文件找不到情况,解决方法

技术分享




Android NDK STL 库调与 System.load

标签:

原文地址:http://my.oschina.net/ososchina/blog/371999

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!