visual studio 2017 创建 android 本地共享库(.so) 并从 C# android 项目中调用
Developing Xamarin Android Native Applications
Xamarin has quickly become a popular way for creating cross-platform mobile applications promoting true code-reuse while allowing developers to create native* (native here means natural to the platform) user interfaces, providing them access to full spectrum of functionality exposed by the underlying platform/device and guaranteeing native* performance by allowing applications to leverage platform specific hardware acceleration. To add on, Xamarin integrates fully into Visual Studio and allows development of iOS, Android and Windows applications alike.
So what does this mean for our true native (C/C++) developers? Well if you take a look at Xamarin’s current approach for the android platform, it allows for leveraging android native (C/C++) libraries when building end-end Xamarin Android applications.
This approach currently however involves mucking around with the Xamarin Android project files in Visual Studio and adding the Item-Group pointing to the prebuilt dynamic shared library mentioned here in the Xamarin Android guides. Typically this native dynamic shared library is developed in another IDE (commonly, Eclipse), which adds further complexity given developers now have to worry about using two IDE’s and absorbing the fun that comes from the added complexity to edit, build and debug their application specially when working in the native (C/C++) library layer.
With Visual Studio 2015 CTP 6, this is no longer the case and developers can now create even this native (C/C++) library in Visual Studio, we (the C++ cross-platform mobile team) have been working with Xamarin in order to provide a smooth experience when developing Xamarin Android Native applications. This blog highlights a few of the enablement made available with Visual Studio 2015 CTP 6 and while all the work has not been finished yet, we are excited for you try it out and provide us feedback.
Getting Started
As a pre-requisite for being able to follow this blog, developers need to installVisual Studio 2015 CTP6 with Visual C++ cross-platform mobile bits and Xamarin optional packages. Once these bits have been installed you have everything you need to start developing Xamarin Android Native applications entirely in Visual Studio. Let us start with creating a Xamarin Android Project. For purposes of this blog let’s choose the simplest of Xamarin Android templates ‘Blank App (Android)’ from the File->New project menu and create a new project called ‘XamarinNativeExample’.

Next to the existing solution let’s add a dynamic shared library project from the ‘ Visual C++ – Cross-Platform node ’ and call it ‘NativeLibrary’.

Once we are done, our solution should have two projects (a Xamarin Android project and a C++ Dynamic Shared Library project) in solution explorer.
Referencing Android Native libraries from a Xamarin Android project
As mentioned in the abstract of this blog traditionally developers leveraging native android binaries would have to go build them in other IDE’s and then to leverage these pre-built android native binaries within a Xamarin Android project they would have to muck around with the Xamarin Android project. With the work we have done referencing the native binary is as simple as adding a project reference between the ‘XamarinNativeExample’ and the ‘NativeLibrary’ projects. The process is illustrated in the images below:


Once the reference has been added, under the references section for the ‘XamarinNativeExample’ project the ‘NativeLibrary’ entry should be found. One would notice that this reference is currently not resolved, this is a known issue and even though the reference doesn’t resolve at design time the application package file (.apk) that is built would contain the native library.
Next let us go ahead and add some code in our native library and invoke it from our Xamarin Android project. In the ‘NativeLibrary.cpp’ source file within the ‘NativeLibrary’ project, add the following function. This function will be invoked every time the button defined in our Xamarin Android project is clicked.
 int clickCount = 1; 
                                int
                                getClickCount() {
                            
                                   return
                                clickCount++;
                            
}
To call this function from the Xamarin Android
                            project ‘XamarinNativeExample’, add
                            the following code in ‘MainActivity.cs’ file.
                                [DllImport(“libNativeLibrary.so“)]
                            
                                public extern static int
                                getClickCount(); 
                            
This Dllimport attribute will load the android
                            native library at application runtime. For using this attribute the ‘using
                            System.Runtime.InteropServices;’ namespace will also be required in your source.
                            Next let us modify the existing source line in the ‘OnCreate’ function. OnCreate
                            method is the first method to be called when the activity is created. This
                            modification in the OnCreate function will allow us to call the ‘getClickCount’
                            function from the native library whenever the user clicks the button.
                                button.Click +=
                                    delegate
                                 { button.Text = string.Format(“
                                    {0}
                                    clicks!
                                “, getClickCount()); };
                            
At this point we are almost done but before we go
                            ahead and build our solution and test out our new Xamarin Android Native
                            Application, the active solution platform needs to be setup appropriately (ARM
                            or x86 depending upon the choice of your debug target) as shown in the figure
                            below.

Lastly let us make sure that we have named our
                            ‘NativeLibrary’ correctly by viewing the ‘Target Name’ property in the
                            NativeLibrary project property pages.

At this point we are all set to go ahead and build/deploy our application to
                            an Android device or emulator. In case you had any issues following this
                            blog, the solution being presented in this blog can be found here.
Debugging
                                an Android Native Library in a Xamarin Android Application
In addition to being able to consume and build a
                            native android library easily in a Xamarin Android application. The Visual
                            studio experience will also allow developers to easily debug their native
                            android library code being consumed within the Xamarin Android application.
                            To demonstrate this, continuing with our example bring up the property pages for
                            the ‘XamarinNativeExample’ project
                            and choose the ‘Native’ debugger option in the ‘Android Options’ menu as shown
                            in the figure below.

Next let us bring up the ‘NativeLibrary.cpp’
                            source and set a breakpoint in our ‘getClickCount()’ function and start
                            debugging (F5). Once the application starts up, on every button click the
                            break-point we set should be hit.

While this is a trivial example, all other debug
                            features such as expression evaluation, watch window, auto window work with this
                            experience as well.
Wrap
                                Up
This blog should provide you insight into the work
                            that the Visual Studio team has done in partnership with Xamarin to enable a
                            smooth experience when building Xamarin Android Native applications. We would
                            love you folks to try this experience out and provide your feedback.
                            Additionally If you would like us to talk about a specific topic on our blogs
                            please let us know.  As a reminder the goal with this CTP release is for us
                            to collect feedback and learn from you. We are considering future support for
                            other platforms too and your feedback is critical in shaping the direction of
                            our product.
Sharing feedback is easy! Make feature suggestions
                            on UserVoice,
                            log bugs you find on our Connect site
                            and send us
                                a smile or frown from inside the IDE. You can also leave comments
                            below. In addition to this if you are
                            looking to have a directed conversation with our product
                            team on this topic, please reach out to us through email (aasthan@microsoft.com). We
                            would love to know more and connect with you. 
                
            
        
visual studio 2017 创建 android 本地共享库(.so) 并从 C# android 项目中调用的更多相关文章
- Visual Studio 2017创建XAML文件
		
Visual Studio 2017创建XAML文件 在Visual Stuido 2015中,在已经创建好的项目中添加XAML文件,只要右击项目,单击“添加”|“新建项”命令,然后从“添加新项” ...
 - 如何使用visual studio 2017创建C语言项目
		
使用visual studio 2017创建一个C语言项目,步骤如下: (1)打开Visual Studio 2017环境后出现欢迎界面,如图1所示. 图1 Visual Studio 2017欢迎 ...
 - Visual Studio 2017 创建Winfrom工程
		
1.打开Visual Studio 2017,出现界面点击-创建新项目 2.选择-Window桌面,选择windows 窗体应用(.NET Framework) 3.完成窗体程序创建,可在左边工具栏里 ...
 - Visual Studio 2017创建.net standard类库编译出错原因
		
正式版上个月已经Release了,从那时到现在经常会收到更新提示,估计问题还不少吧!其中最吸引我的当然是.net standard与.net core. 刚好最近接触.net standard项目,新 ...
 - Visual Studio 2017 + Python3.6安装scipy库
		
Windows10下安装scipy很麻烦,直接在命令行下使用pip install scipy无法安装,但可以借助VS2017的集成环境来安装. (1)首先在Visual Studio Install ...
 - Visual Studio 2017 离线安装方式
		
Visual Studio, 特别是Visual Studio 2017 通常是一个在线安装程序,如果你在安装过程中失去连接,你可以遇到问题.但是,由于法律原因,微软没有提供完整的可下载的ISO镜像. ...
 - 使用 Visual Studio 2017 部署 Azure 应用服务的 Web 应用
		
本快速入门介绍了如何使用 Visual Studio 2017 创建并部署 Azure Web 应用.在本教程中完成的所有操作均符合1元试用条件. 本快速入门介绍了如何使用 Visual Studio ...
 - 三、Android NDK编程预备之Java jni入门创建C/C++共享库
		
转自: http://www.eoeandroid.com/thread-264971-1-1.html 应网友回复,答应在两天前要出一篇创建C/C++共享库的,但由于清明节假期,跟朋友出去游玩,丢手 ...
 - Visual Studio 2017中使用Libman管理客户端库
		
什么是Libman 微软在Visual Studio 2017 15.8版本中内嵌了一个新的工具Library Manager. Library Manager(简称Libman)是一个客户端库管理工 ...
 
随机推荐
- equals方法中变量在前和在后的区别
			
对于变量:String str1; 使用str1.equals("null"); 对于变量str1,如果str1是null,空是没有equals方法的,会抛出空指针异常 使用&qu ...
 - .net正则匹配
			
char[] weixin = txtweixinhao.Text.Trim().ToCharArray(); for (int i = 0; i < weixin.Length; i++) i ...
 - python笔记18-高阶函数
			
高阶函数: 如果一个函数的入参是一个函数名的话,那这个函数就是一个高阶函数 函数即变量 # def hello(name):# print(name)# new_hello = hello#hello ...
 - 使用  <embed> 标签显示 flash文件(swf)格式 ,如何设置 width 和 height 宽度,高度.
			
1. embed 标签 支持 .swf 格式. .flv 的不支持. 2. 通常情况下, 网站中上传 多个 flash文件. 它的默认大小是不一样的. 而且 可以 宽度 大于 高度(横向的) ...
 - 服务器配置+wordpress建站(小白)
			
一. 安装好centos7.2系统后,登录centos系统输入如下命令: yum install -y wget && wget -O install.sh http://downlo ...
 - Map、Set、List区别
			
转:https://www.cnblogs.com/jing99/p/6947549.html 提到集合之前,先说说数组Array和集合的区别: (1)数组是大小固定的,并且同一个数组只能存放类型 ...
 - Android BLE dfu升级
			
dfu升级适用于nordic nRF51 nRF52 的系统,github上提供了相关升级的库https://github.com/NordicSemiconductor/Android-DFU- ...
 - L2-001 紧急救援 (25 分)
			
L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快 ...
 - linux下安装jdk 详细步骤(一条命令即可安装)
			
转载:https://blog.csdn.net/luochangchen/article/details/72804426 作为Java开发人员,在Linux下安装一些开发工具是必备技能,本文以安装 ...
 - 一个python小白的学习之路
			
本人是个网管,在佛山工作,现在已经学习了一段时间python了,还是学开基础,但近段时间有一点的突破出来了,找到了一个很好的自学视频,等自己有能力了就想找一个特训班试试.已经看了视频两个星期了,有小小 ...
 
			
		