Android Java调用libtwolib-first.a
输入:
新建jni目录,放入libtwolib-first.a、first.h、second.c、Android.mk四个文件
输出:
libtwolib-second.so(打包进apk了)、Caculator.apk
过程:
Android.mk内容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#下面这句加上去
LOCAL_LDFLAGS := $(LOCAL_PATH)/libtwolib-first.a
LOCAL_MODULE := twolib-second
LOCAL_SRC_FILES := second.c
include $(BUILD_SHARED_LIBRARY)
second.c内容如下:
#include "first.h"
#include <jni.h>
jint Java_com_suhan_Caculator_add( JNIEnv* env,
jobject this,
jint x,
jint y )
{
//return x+y;
return first(x,y);
}
Caculator内容如下:
package com.suhan;
public native int add(int x, int y);
static {
System.loadLibrary("hello-jni");
System.loadLibrary("demo-jni");
System.loadLibrary("twolib-second");
}
点击按钮:
int x = 3;
int y = 50;
int z = add(x, y);
String Z = Integer.toString(z);
answerValue.setText(Z);
右击Caculator工程属性,新建并配置一个Builder
Location填:
F:\android-ndk-r9\ndk-build.cmd
Working Directory填:
F:\adt-bundle-windows-x86_64-20130729\workspace001\Caculator
或
${workspace_loc:/Caculator}
点击Project|Clean,选中Caculator,即可生成libtwolib-second.so
点击Run Caculator按钮,即可生成Caculator.apk
Android Java调用libhello-jni.so、libdemo-jni.so
正确的:
HelloJni.java
package com.example.hellojni;
public class HelloJni {
public native String stringFromJNI();
}
F:\adt-bundle-windows-x86_64-20130729\workspace001\Caculator\src\com\example\NativeMD5.java
package com.example;
public class NativeMD5 {
public native String encryptMD5(String text);
}
Caculator.java
package com.suhan;
import com.example.hellojni.HelloJni;
import com.example.NativeMD5;
public class Caculator extends Activity{
private HelloJni lib = new HelloJni();
private NativeMD5 md5 = new NativeMD5();
点击按钮:
String x=lib.stringFromJNI();
answerValue.setText(x);
String x=md5.encryptMD5("123");
answerValue.setText(x);
static {
System.loadLibrary("hello-jni");
System.loadLibrary("demo-jni");
}
20150602测试Android回调Unity:
Android Java部分:
public static void AndroidFunc1(String arg)
{
UnityPlayer.UnitySendMessage("Main Camera","UnityFunc1",arg);
}
public static void AndroidFunc2()
{
UnityPlayer.UnitySendMessage("Main Camera","UnityFunc1","--AndroidFunc2--");
}
Unity C#部分:
if(bflag)
{
ajc.CallStatic("AndroidFunc2");
}
else
{
ajc.CallStatic("AndroidFunc1", new string[]{"Call 韩AndroidFunc1 2"});;
}
bflag=!bflag;
void UnityFunc1(string str)
{
strTest=str;
}
Android Java部分:
/UnityCPP/src/com/Bjoy/UnityCPP/MainActivity.java代码如下:
package com.Bjoy.UnityCPP;
import android.os.Bundle;
import android.view.Menu;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//判断大小
public static int Max(int a,int b)
{
if(a>b)
return a;
return b;
}
}
命令行生成jar:
F:\adt-bundle-windows-x86_64-20130729\workspace001\UnityCPP\bin\classes>jar cvf com.Bjoy.UnityCPP.jar com\
或者
F:\soft\Android Tools\adt-bundle-windows-x86_64-20140702\workspace\UnityCPP\bin\classes>jar cvf com.Bjoy.UnityCPP.jar com
com.Bjoy.UnityCPP.jar放在F:\client\SimplestPluginExample\Unity Project Plugin\Assets\Plugins\Android\bin目录下
AndroidManifest.xml和res放在F:\client\SimplestPluginExample\Unity Project Plugin\Assets\Plugins\Android目录下
Unity C#部分:
private AndroidJavaClass ajc;
private int res = 0;
//通过unity提供的访问java插件的帮助类来找到我们的插件并创建对象
ajc = new AndroidJavaClass("com.Bjoy.UnityCPP.MainActivity");
res =ajc.CallStatic<int>("Max", new object[] { 10, 20 });
GUI.Label(new Rect(450, 420, 900, 120), "Max(10,20)="+res.ToString());
if(GUI.Button(new Rect(100,420,200,100), "ClickShake"))
{
res =ajc.CallStatic<int>("Max", new object[] { 6, 14 });
}
获取小米手机的物理地址:
ac:f7:f3:5b:13:02
查看路由器信息:
http://10.10.10.1/
主机名: MI2S-xiaomishou
ji
IP地址: 10.10.10.106
MAC地址: AC:F7:F3:5B:13:02
Android Java部分:
import android.util.Log;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
public static MainActivity inst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
inst = this;
}
public static String getAndroidMacID()
{
String str = null;
WifiManager wifi = (WifiManager)inst.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
str = info.getMacAddress();
if(str==null)
{
Log.e("获取android mac地址失败", "0000000");
}
Log.e("获取android mac地址 "+str, "00000000");
return str;
}
Unity C#部分:
string strTest;
strTest=ajc.CallStatic<string>("getAndroidMacID");
GUI.Label(new Rect (450, 50, 900, 120),strTest);
AndroidManifest.xml添加:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
请将minSdkVersion的值改为9,如果出现下列错误:
Description Resource Path Location Type
Call requires API level 9 (current min is 8): android.app.NativeActivity#getSystemService MainActivity.java
/UnityCPP/src/com/Bjoy/UnityCPP line 51 Android Lint Problem
使用ndk构建共享库libASimplePlugin.so:
在F:\android-ndk-r9\samples\test-libstdc++下创建一个jni的文件夹(注:不能是其它文件夹名),接着在文件夹中创建代码文件ASimplePlugin.cpp和一个配置文件Android.mk。
先看看C++文件ASimplePlugin.cpp
/*
This is a simple plugin, a bunch of functions that do simple things.
*/
extern "C" {
const char* PrintHello(){
return "Hello";
}
int PrintANumber(){
return 5;
}
int AddTwoIntegers(int a, int b) {
return a + b;
}
float AddTwoFloats(float a, float b) {
return a + b;
}
}
再看看配置文件Android.mk,文件中比较重要的两个变量 LOCAL_MODULE表示生成出的.so的名称 LOCAL_SRC_FILES 表示需要编译的文件,如果是多个C/C++文件中间需要使用 \ 隔开。
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ASimplePlugin
LOCAL_SRC_FILES := ASimplePlugin.cpp
include $(BUILD_SHARED_LIBRARY)
执行ndk-build即可生成so库:
F:\android-ndk-r9\samples\test-libstdc++\jni>F:\android-ndk-r9\ndk-build.cmd
F:/android-ndk-r9/samples/test-libstdc++//jni/Android.mk:10: UILD_SHARED_LIBRARY
: No such file or directory
F:/android-ndk-r9/build/core/build-all.mk:89: Android NDK: WARNING: There are no
modules to build in this project!
make: *** No rule to make target `UILD_SHARED_LIBRARY'. Stop.
F:\android-ndk-r9\samples\test-libstdc++\jni>F:\android-ndk-r9\ndk-build.cmd
"Compile++ thumb : ASimplePlugin <= ASimplePlugin.cpp
SharedLibrary : libASimplePlugin.so
Install : libASimplePlugin.so => libs/armeabi/libASimplePlugin.so
构建了共享库后,必须把共享库复制到unity3d工程中的Assets->Plugins->Android目录下。然后就可以在Unity中使用这个插件了。
Unity中调用F:\client\UnityTest20150509\Assets\Plugins\PluginName.dll、F:\client\SimplestPluginExample\Unity Project Plugin\Assets\Plugins\Android\libASimplePlugin.so:
SomeScript.cs中平台无关代码如下:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class SomeScript : MonoBehaviour {
// This tells unity to look up the function FooPluginFunction
// inside the plugin named "PluginName"
[DllImport ("PluginName")]
private static extern float FooPluginFunction ();
void Awake () {
// Calls the FooPluginFunction inside the PluginName plugin
// And prints 5 to the console
print (FooPluginFunction ());
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Win8下C#调用C++编译的32位标准DLL:
F:\hxhwin7\c#参考\myc#>cl /c PluginName.cpp
F:\hxhwin7\c#参考\myc#>link /DLL /OUT:PluginName.dll PluginName.obj
F:\hxhwin7\c#参考\myc#>csc /platform:x86 PluginName0.cs
Microsoft(R) Visual C# 编译器版本 4.0.30319.33440
用于 Microsoft(R) .NET Framework 4.5
版权所有 (C) Microsoft Corporation。保留所有权利。
F:\hxhwin7\c#参考\myc#>PluginName0
FooPluginFunction()=5
PluginName0.cs代码如下:
using System;
using System.Runtime.InteropServices;
namespace Han
{
class PluginName0
{
// inside the plugin named "PluginName"
[DllImport("PluginName")]
private static extern float FooPluginFunction();
static void Main()
{
Console.WriteLine("FooPluginFunction()="+FooPluginFunction());
}
}
}
托管C++控制台程序命令行编译的例子:
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Console::Read();
return 0;
}
F:\hxhwin7\vcnet>cl /CLR ex20150507.cpp
Microsoft(R) C/C++ 优化编译器版本 16.00.30319.01
用于 Microsoft(R) .NET Framework 版本 4.00.30319.34014
版权所有(C) Microsoft Corporation。保留所有权利。
ex20150507.cpp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:ex20150507.exe
ex20150507.obj
F:\hxhwin7\vcnet>ex20150507
Hello World
C#调用C#编写的DLL:
F:\hxhwin7\vcnet>csc /target:library /out:MyDLL.DLL MySwap.cs MyMaxCD.cs
F:\hxhwin7\vcnet>csc /out:MyClient.exe /reference:MyDLL.DLL MyClient.cs
或
F:\hxhwin7\vcnet>csc /reference:MyDLL.DLL MyClient.cs
F:\hxhwin7\vcnet>MyClient 123 456
The result of swap is num1 = 456 and num2 =123
The MaxCD of 456 and 123 is 3