How Android Studio exports the AAR plugins available for unity
Source: Internet
Author: User
<span id="Label3"></p>Objective<p><p>Project before using the eclipse exported jar file to do with Android interaction, recently because the work requires the use of Android Studio AAR files, the online reference to some articles, but also combined with their own understanding of the specific way to reorganize, By writing a test demo to describe how Android studio created AAR and how Unity uses AAR files, I hope to be able to help and guide the people who just have the need, and if the text is wrong, you may wish to enlighten Me.</p></p>Version information<p><p>Unity 5.3.1f1,</p></p><p><p>Android Studio 2.2.3</p></p>Android Studio exports an AAR plugin for unity to create Android Studio project 1) the first step, click File->new->new project, 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) the second step, select Phone and Tablet, and select the appropriate min Imum SDK (can also be set in the created Build.gradle) 3) third step, select "Empty activity" 4) fourth, Keep the default Activity name and layout name 5) finally, Click "Finish" to create the project two, add Unity's Classes.jar Reference<p><p>1) put the "editor\data\playbackengines\androidplayer\variations\mono\release\classes\classes.jar" in the Unity engine directory Copy files to the Libs directory in the Android studio project</p></p><p><p>2) Right-click the Project view, open "open Module Settings" and add "classes.jar" dependency</p></p><p><p> </p></p>third, write the Android side code (modify the mainactivity code)<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>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 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!"; }}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>Iv. Modify the build.gradle, set the project export to AAR 1) apply plugin: ' com.android.application ' modified to apply plugin: ' com.android.library '<p><p>2) Delete ApplicationID "com.zcode.unityandroidplugindemo"</p></p><p><p>3) the modified Build.gradle is</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>Apply Plugin: ' Com.android.library ' android { compilesdkversion buildtoolsversion "24.0.1" Defaultconfig { minsdkversion targetsdkversion 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 ')}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>V. Modify Androidmanifest.xml 1) to modify the style we need to modify the main style of the application in the Androidmanifest application node as the system style, because the exported AAR file will not have a custom style, in our In the Unity project, there will be errors that the style cannot find when generating the final APK. Also need to delete the Styles.xml file under the Res\values directory 2) add <meta-data> info under the main activity node, otherwise the manifest file error message will be reported when Unity export APK Vi. export of *.aar files for unity use<p><p>1) Click "build-build APK" to generate the Aar file</p></p><p><p>2) since unity will automatically include its own classes.jar when packaging the apk, it is necessary to open the AAR file using the compression software to delete the Classes.jar file in the Libs directory</p></p>Unity Import Plugin and call one, Create Unity Project ii, Import plugin into Unity project 1) new plugins directory with Android subdirectory 2) copy aar file with Androidmanifest.xml file to Android directory<p><p>third, Write the test code</p></p><p><p>1) Create a new script file for "Call.cs"</p></p><p><p>2) write the call Android side code in the "Call.cs" script</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"> </span> </span><pre>Using unityengine;using system.collections; public class Call:monobehaviour {string information_ = null; void Ongui () {//call displays a text of "Hello world!" The Toest if (GUI. Button (new Rect (0, 0, max), "Show Toest-hello World!")) {//unity side calls Android side code using (androidjavaclass JC = new Androidjavaclass ("com.unity3d.player.UnityPl Ayer ")) {using (androidjavaobject jo = jc. Getstatic<androidjavaobject> ("currentactivity")) {//call member Method jo. Call ("showtoast", "Hello world!"); }}}//get The return string if (GUI) on the plugin Side. Button (new Rect (0, max, max, +), "Get Plugin ' s Information")) {//unity side calls Android side code using (ANDROIDJ Avaclass JC = new Androidjavaclass ("com.unity3d.player.UnityPlayer")) {using (androidjavaobject jo = jc. Getstatic<androidjavaobject> ("currentactivity")) {//call static method information_ = Jo. Callstatic<string> ("getinformation"); }}}//displays the returned string GUI. Label (new Rect (screen.width-220, +), information_); }}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>Iv. export apk 1) set bundle Identifier (keep PackageName consistent with Plugin) 2) set appropriate minimum API level (keep with Plugin) v. Test<p><p></p></p>Demo Address<p><p>Http://pan.baidu.com/s/1dFxc7JF</p></p>End<p><p>Android side calls Unity side of the way here not to do more than repeat, online related articles are also many.</p></p><p><p>Bug solution encountered in using Third-party AAR libraries in Android plugin engineering</p></p><p><p>1. You need to manually copy to the Unity Project Plugin/android directory, or you will not find the library caused Noclassdeffounderror errors, Cause the project to cause a real machine outage (ps:unity pack APK does not check if the Android plugin contains the full other AAR libraries)</p></p><p><p>2. Make sure all android:minsdkverion in the AAR library are consistent with android:targetsdkversion</p></p><p><p>How Android Studio exports the AAR plugins available for unity</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service