AndroidStudio导出aar文件给Unity使用
本文参考 :http://www.devacg.com/?post=548
Demo地址:https://github.com/JulyNine/AndroidToUnity
一、用Android Studio创建个空工程 注意:包名要与Unity中工程的包名不一致,不然打包时会报错multi dex
二、把classes.jar拷到libs下
C:\Soft\UnityHub\2019.2.0f1\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar(自己Unity安装路径)
拷到
C:\Users\July\AndroidStudioProjects\Fakemiss\app\libs\classes.jar(新建的android工程路径)
注意:以上是我的安装目录和工程目录。
三、把classes.jar添加进Dependencies
四、删除res下多余的资源(重要,不然打包的时候会报错:link resource error)
删除layout文件夹 以及values文件夹下的style.xml
五、修改build.gradle文件才能生成aar
修改后的文件
//apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
//applicationId "com.zwwx.game.gamesupport"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile files('libs/classes.jar')
}
六、修改AndroidManifest.xml
修改后的文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zwwx.game.gamesupport">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.NoTitleBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>
七、修改MainActivity.java
package com.zwwx.game.gamesupport;
import android.os.Bundle;
import android.widget.Toast;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// 显示Toast消息
public void ShowToast(final String message){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
}
八、生成arr
九、删除arr文件libs下的classes.jar(特别注意别删错了,是libs文件夹下的classes.jar,不是根目录下的)
十、把aar和AS工程中的AndroidManifest.xml拷到Unity工程下
十一、创建test.cs
using UnityEngine;
public class Test : MonoBehaviour {
void OnGUI()
{
if (GUI.Button(new Rect(200, 200, 200, 100), "Show Toest - Hello World!"))
{
AndroidHelper.Call("ShowToast", "Hello World!");
}
}
}
十二、设置好Player Settings,然后打包测试
十三、最后赠送一个Unity中调用java方法的辅助工具类
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 与Android交互的辅助类
/// </summary>
public class AndroidHelper
{
#if UNITY_ANDROID && !UNITY_EDITOR
#region 获得主活动类
public static AndroidJavaClass MainActivity
{
get
{
try
{
AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
return player;
}
catch (Exception)
{
return null;
}
}
}
#endregion
#region 获得当前Android活动
public static AndroidJavaObject currentActivity
{
get {
try
{
AndroidJavaObject context = MainActivity.GetStatic<AndroidJavaObject>("currentActivity");
return context;
}
catch (Exception)
{
return null;
}
}
}
#endregion
#region 获得Java类
public static AndroidJavaClass JavaClass(string className)
{
try
{
AndroidJavaClass java_class = new AndroidJavaClass(className);
return java_class;
}
catch (Exception)
{
return null;
}
}
#endregion
#region 获得Java对象
public static AndroidJavaObject JavaObject(string className, params object[] args)
{
try
{
AndroidJavaObject java_object = new AndroidJavaObject(className, args);
return java_object;
}
catch (Exception)
{
return null;
}
}
#endregion
#endif
#region 调用Java类的实例方法
public static void Call(string methodName, params object[] args)
{
#if UNITY_ANDROID && !UNITY_EDITOR
currentActivity.Call(methodName, args);
#endif
}
public static T Call<T>(string methodName, params object[] args)
{
#if UNITY_ANDROID && !UNITY_EDITOR
return currentActivity.Call<T>(methodName, args);
#endif
return default(T);
}
public static void CallClass(string className, string methodName, params object[] args)
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaObject java_object = JavaObject(className);
java_object.Call(methodName, args);
#endif
}
public static T CallClass<T>(string className, string methodName, params object[] args)
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaObject java_object = JavaObject(className);
return java_object.Call<T>(methodName, args);
#endif
return default(T);
}
#endregion
#region 调用Java类的静态方法
public static void CallStatic(string methodName, params object[] args)
{
#if UNITY_ANDROID && !UNITY_EDITOR
MainActivity.CallStatic(methodName, args);
#endif
}
public static T CallStatic<T>(string methodName, params object[] args)
{
#if UNITY_ANDROID && !UNITY_EDITOR
return MainActivity.CallStatic<T>(methodName, args);
#endif
return default(T);
}
public static void CallClassStatic(string className, string methodName, params object[] args)
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass java_class = JavaClass(className);
java_class.CallStatic(methodName, args);
#endif
}
public static T CallClassStatic<T>(string className, string methodName, params object[] args)
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass java_class = JavaClass(className);
return java_class.CallStatic<T>(methodName, args);
#endif
return default(T);
}
#endregion
}
来源:CSDN
作者:子胤
链接:https://blog.csdn.net/yinfourever/article/details/103489945