azesmway/react-native-unity

Build the unityLibrary Module as an AAR and Link it with the react-native-unity Plugin

Opened this issue · 1 comments

Since it's possible to Export and Build the unityLibrary Module as an AAR file, does anyone know which are the steps to include such a library in the ReactNative Plugin?. This would not require to setup and build using the NDK on the ReactNative project, speeding up build time and simplifying the integration.
Has anyone experience in such an integration?

@jgrunig yes, you can. But you need to modify many thing

Modify this lib - android/build.gradle

In short, you need to modify node_modules/@azesmway/react-native-unity/android/build.gradle inside your react native folder

dependencies {
  // For < 0.71, this will be from the local maven repo
  // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-native:+"
  implementation project(':unityLibrary')
  
  //// comment out this line, because unity-class.jar is built into aar file
  //// so if you want to keep that line, you need to use project instead
  // implementation files("${project(':unityLibrary').projectDir}/libs/unity-classes.jar")
}

Modify unityLibrary before build AAR

Remove intent filter which link your unity library with unity launcher, follow official document

<!-- <intent-filter>
  <category android:name="android.intent.category.LAUNCHER" />
  <action android:name="android.intent.action.MAIN" />
</intent-filter> -->

So now you can build AAR using gradle

# this line will download gradle library and create gradlew file inside export project
gradle wrapper

# this line will build unity AAR lib from exports project, but not dependencies
# because of this, you need to copy final AAR file on build folder and libs folder inside unityLibrary into unity/builds/android folder
./gradlew assemble

Now all you need is integrate. there are some file you need to modify in order to integrate without error, I will list all step I need to do (may be different for you, I am not sure)

Some step to do

1. Create local unity project inside unity/builds/android

Create folder unityLibrary, copy/move unityLibrary-release.aar and libs folder inside unityLibrary
Create build.gradle inside unityLibrary with below content

configurations.maybeCreate("default")
artifacts.add("default", file('unityLibrary-release.aar'))

You also need to modify settings.gradle like official document

include ':unityLibrary'
project(':unityLibrary').projectDir=new File('..\\unity\\builds\\android\\unityLibrary')

2. Modify some file inside android/app

This step used to implement all file AAR in libs folder
Modify android/app/build.gradle

dependencies {
    // add this line -- I don't know but I cannot use flatDir like document, so I add here
    implementation fileTree(dir: "${project(':unityLibrary').projectDir}/libs", include: '*.aar')
}

You also need to add string to android/app/src/main/res/values/strings.xml, don't forget

<string name="game_view_content_description">GameView</string>

May be done and you can build right now

I don't know if my workaround is worked for you. I'm happy if you can try and feedback for me 👍