Unity introduces a custom aar library, and the aar library depends on other third-party libraries. How to break it?

I recently developed a VR project. This project wants to integrate an SDK on the cloud, but this SDK only released the JAVA version of the aar library, and there is no Unity version. Then you can only find a way to integrate the aar library into the Unity project.

During this integration process, it was found that the aar library also depends on third-party libraries, so when the Unity version of the apk runs, it will report an error Unity AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/ GsonBuilder et al.

2021/10/20 10:17:08.306 19418 19488 Error Unity AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/GsonBuilder;
2021/10/20 10:17:08.306 19418 19488 Error Unity java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/GsonBuilder;
2021/10/20 10:17:08.306 19418 19488 Error Unity 	at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
2021/10/20 10:17:08.306 19418 19488 Error Unity 	at com.unity3d.player.UnityPlayer.access$300(Unknown Source:0)
2021/10/20 10:17:08.306 19418 19488 Error Unity 	at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:95)
2021/10/20 10:17:08.306 19418 19488 Error Unity 	at android.os.Handler.dispatchMessage(Handler.java:106)
2021/10/20 10:17:08.306 19418 19488 Error Unity 	at android.os.Looper.loop(Looper.java:219)
2021/10/20 10:17:08.306 19418 19488 Error Unity 	at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
2021/10/20 10:17:08.306 19418 19488 Error Unity Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.gson.GsonBuilder" on path: DexPathList[[zip file "/data/app/com.huawei.WorkRoom-5GZ9jB2tFHRPCXPv_uItfQ==/base.apk"]

The reason for this error is that the aar library depends on the third library com.google.code.gson. We will not automatically put this package into the apk when compiling, resulting in an error at runtime.

How to import the dependent jar library when Unity is packaged?

The first stupid way is to download the dependent jar library yourself and put it in the Assets\Plugins\Android directory. This method can solve the problem of dependent packages. But this method is rather stupid.

Today I'm talking about how to let gradle manage dependencies.

1. Configure gradle on Unity, Edit->Project Setting->Player->Publishing Setting, drop down to the Build section, and check Custom Main Gradle Template and Custom Launcher Gradle Template.

2. Go to the Assets\Plugins\Android directory, open the mainTemplate.gradle file, and add the following to apply plugin: 'com.android.library'.

buildscript {
    repositories {
        maven { url 'http://wlg1.artifactory.cd-cloud-artifact.tools.huawei.com/artifactory/cbu-maven-public/' }
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.2'
    }
}

allprojects {
   repositories {
      maven { url 'http://wlg1.artifactory.cd-cloud-artifact.tools.huawei.com/artifactory/cbu-maven-public/' }
      google()
      jcenter()
      flatDir {
        dirs 'libs'
      }
   }
}

 Note: maven { url 'http://wlg1.artifactory.cd-cloud-artifact.tools.huawei.com/artifactory/cbu-maven-public/' } This block can configure your own maven library.

Then modify the dependencies under apply plugin: 'com.android.library' and add your own dependency library as follows.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
	implementation 'com.google.code.gson:gson:2.8.6'
	
**DEPS**}

3. Open the launcherTemplate.gradle file and add the following to apply plugin: 'com.android.library'.

buildscript {
    repositories {
        maven { url 'http://wlg1.artifactory.cd-cloud-artifact.tools.huawei.com/artifactory/cbu-maven-public/' }
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.2'
    }
}

allprojects {
   repositories {
      maven { url 'http://wlg1.artifactory.cd-cloud-artifact.tools.huawei.com/artifactory/cbu-maven-public/' }
      google()
      jcenter()
      flatDir {
        dirs 'libs'
      }
   }
}

In this way, Unity can configure the dependency library of your aar package. Is this method still simple? Please pay attention.

Guess you like

Origin blog.csdn.net/grace_yi/article/details/120860670