Android project combat (39): Android integrated Unity3D project (detailed graphic and text)

Original: Android project combat (39): Android integrated Unity3D project (detailed graphic and text)

  need:

  Unity3D is generally used for making games and is cross-platform. The original design is that Android application terminal A (native development) performs some business processing, and finally A opens Android application terminal B (Unity3D game development) for game operations.

 

  Problems caused by this: 

  1. Application A opens application B, and the transfer of data between them is inconvenient

  2. Normal shutdown, abnormal shutdown interaction and technical processing troubles

  3. Version iteration needs to be considered separately

  4. Poor experience

  5. ......................

  Finally, it will be merged into an Android application to improve the user experience. The implementation idea is that the Android project inherits (introduces) the Unity3D project (the dependent Android project generates a class library).

  

  Start the detailed steps below:

  1. The Unity3D engineer generates a class library for the project

    Not much to say, let Unity3D engineers do it, we only need this class library

  

     Second, the Android project import class library

    

     

    Note: All three must be selected

        

 

       After the import is successful, there will be some errors, which need to be modified

    

 3. Modification

  (1), find the build.gradle file in the class library directory

       Change the first line:  apply plugin: 'com.android.application'  to:  apply plugin: 'com.android.library' 

       Change compileSdkVersion and buildToolsVersion to be consistent with build.gradle in the app directory 

      Delete the applicationId line under defaultConfig{}

                   Sync Now...

      It ends up as follows:

      

 

    

 (2), find the AndroidManifest.xml file in the class library directory

     Delete several properties under application

    android:debuggable="false"

    android:label=""

    android:theme=""

    android:icon="@mipmap/ic_launcher"

    

    Find the main entry Activity that has <intent-filter></intent-filter> , add an attribute: android:process="e.unity3d" , this is to let the main Activity of Unity3D exit and return to the Android part, otherwise There will be abnormal problems

     Then delete all the parts inside the <intent-filter>. This part marks the Activity as the main entry and is displayed on the mobile phone desktop. If it is removed, it will not be displayed on the desktop. Because the Android part calls this to enter the Unity3D part, it has the main entry by default. function.

    The effect is as follows:

  <application android:allowBackup="true"
      android:name="com.xxxxx.SensorS.XApplication"
      android:supportsRtl="true"
      android:isGame="true">
    <activity
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:name="com.xxxxx.SensorS.MainActivity"
        android:process="e.unity3d"
        android:screenOrientation="landscape">
    </activity> 
  
  //... Note
  // Opening the MainActivity interface is in a new process, see the parameter android:process="e.unity3d"
  // If other services or activity interfaces need to be in the same process Need to add android:process="e.unity3d"
  </application>

 

    

   (3), rely on the library

      

       Select class library dependencies

      

   

  (4), the Android part of the call to open the Unity3D main Activity, that is to open an Activity in the class library

Intent intent = new Intent();
// unity3d部分的主activity                              
intent.setClass(TaskListActivity.this,MainActivity.class);
startActivity(intent);

 

  

  Also: possible errors

  1. Your hardware does not support it! 

  

  The probability of this occurrence is relatively high. First, take a look at the file directory of the unity3d class library. There are only two folders, armeabi-v7a and x86.

  

   So what we need to solve is to check the libs folder or jniLibs folder under the app directory and all class libraries, and there can only be these two folders, many need to be deleted

    Also note:

   If your app directory or the build.gradle folder in the module directory has the following code: 

   ndk {
             // Select the .so library of the corresponding cpu type to be added. 
            abiFilters   ' armeabi-v7a ' , ' x86 ' 
        }

   Remember that there must be only these two, and more must be deleted.

   As long as it is consistent, the problem is solved.

  

  2. The problem of jar package conflict, this is a common problem, just keep one in the class library and the app, not much to say.

    

  3. BuildConfig.class file conflict

Error:Execution failed for task ':ipark2:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/xxxxx/xxxxx/xxxx/BuildConfig.class

 

   This means that the BuildConfig.class file in your com/xxxxx/xxxxx/xxxx/ directory conflicts. Usually in the class library, there is such a file under the jar package, but the original project does not have it, it is generated by the system when the class library is generated.

     The solution is to delete the file from the jar package.

     Example:

   1. Now there is a jar package 

 

   2. Open the directory where the file is located, press Shift + right mouse button and select 'Open command window from here'

      Enter the command:  jar xf SensorS .jar    Note: SensorS is the name of your jar package

     

      After executing the command, you will find that there is a decompressed jar file in the directory. Find the BuildConfig.class file in the directory and delete it.

    3. Re-compress the processed folder into a jar package  

    jar cvf SensorSSS .jar com     Note: SensorSSS is the name of the regenerated jar package, and com is the folder to be compressed into the jar package (that is, the file extracted from the original jar package and delete the BuildConfig.class file)

     

 

 

     Finally, replace the processed jar package with the original one

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325112855&siteId=291194637