Android Studio: How to export the aar plug-in available for Unity
Preface
Before the project, use the jar file exported by Eclipse to interact with Android. Recently, the aar file of Android Studio needs to be used for work. For more information, see some articles on the Internet, I also reorganized the specific methods based on my own understanding. I wrote a test Demo to describe the process of creating aar in Android Studio and how to use the aar file in Unity, I hope to help some people who have this need.
Version Information
Unity 5.3.1f1,
Android Studio 2.2.3
Android Studio exports the aar plug-in available for Unity. 1. Create an Android Studio Project. 1) click File> New Project to open the "Create New Project" dialog box, select the appropriate Application name and Company Domain to ensure that the Package name is consistent with the Bundle Idenifier in the Unity project. 2) Step 2, select Phone and Tablet, and select the appropriate Minimum SDK (you can also build it after the creation. (3) Step 3, select "Empty Activity" 4) Step 4, keep the default Activity Name and Layout Name. (5) Finally, click "Finish" to create a project. 2. Add the Unity classes. jar reference
1) copy the "Editor \ Data \ PlaybackEngines \ AndroidPlayer \ Variations \ mono \ Release \ Classes \ classes. jar" file in the Unity Engine Directory to the libs directory in the Android Studio project.
2) Right-click "Project" view, Open "Open Module Settings", and add "classes. jar" dependency
3. Compile the Android-side code (modify the MainActivity code)
Import android. OS. bundle; import android. widget. toast; import com. unity3d. player. unityPlayer; import com. unity3d. player. unityPlayerActivity; public class MainActivity extends UnityPlayerActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);} // display the Toast message public void ShowToast (final String message) {runOnUiThread (new Runnable () {@ Override public Void run () {Toast. makeText (getApplicationContext (), message, Toast. LENGTH_LONG ). show () ;}) ;}// returns a String (static method) public static String GetInformation () {return "This is a Plugin's content! ";}}
4. Modify build. gradle and set project export to aar 1) apply plugin: 'com. android. application' to apply plugin: 'com. android. library'
2) Delete applicationId "com. zcode. unityandroidplugindemo"
3) The modified build. gradle is
apply plugin: 'com.android.library' android { compileSdkVersion 24 buildToolsVersion "24.0.1" defaultConfig { minSdkVersion 18 targetSdkVersion 24 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:24.1.1' testCompile 'junit:junit:4.12' compile files('libs/classes.jar')}
5. Modify AndroidManifest. xml 1) to modify the style, you need to modify the main style of the application to the system style at the application node in AndroidManifest, because the exported AAR file will not contain the custom style, when the final apk is generated in our Unity project, the style cannot be found. Delete styles in the res \ Values directory. xml file 2) Add the <meta-data> information under the main activity node. Otherwise, the system reports that the manifest file cannot be found when exporting the APK. 6. exported for Unity *. aar File
1) Click "Build-> Build APK" to generate the aar File
2) Because Unity automatically contains its own classes. jar when packaging the APK, you need to use the compression software to open the aar file and delete the classes. jar file under the libs directory.
Unity import plug-in, and call 1. create Unity project 2. Import the plug-in to Unity project 1) create a Plugins directory and an Android sub-directory 2) copy the aar file and AndroidManifest. xml file to the Android directory
3. Write test code
1) create a new "Call. cs" Script File
2) write the Android-side code in the "Call. cs" script.
Using UnityEngine; using System. Collections; public class Call: MonoBehaviour {string information _ = null; void OnGUI () {// Call to display a text "Hello World !" Toest if (GUI. Button (new Rect (0, 0,200, 20), "Show Toest-Hello World! ") {// The Android-side code using (AndroidJavaClass jc = new AndroidJavaClass (" com. unity3d. player. unityPlayer ") {using (AndroidJavaObject jo = jc. getStatic <AndroidJavaObject> ("currentActivity") {// call the member method jo. call ("ShowToast", "Hello World! ") ;}}// Get the response string if (GUI. button (new Rect (0, 40,200, 20), "Get Plugin's Information ")) {// The Android code using (AndroidJavaClass jc = new AndroidJavaClass ("com. unity3d. player. unityPlayer ") {using (AndroidJavaObject jo = jc. getStatic <AndroidJavaObject> ("currentActivity") {// call the static method information _ = jo. callStatic <string> ("GetInformation") ;}}// the returned string GUI is displayed. label (new Rect (220, 40, Screen. width-220, 20), information _);}}
Iv. Export APK 1) set Bundle Identifier (keep consistent with the plug-in PackageName) 2) set the appropriate Minimum API Level (keep consistent with the plug-in) 5. Test
Demo address
Http://pan.baidu.com/s/1dFxc7JF
End
The Android-side method of calling the Unity-side is not described in detail here, and there are many related articles on the Internet.