Unity构建APK报错:重复类com/google/vr/cardboard/AndroidNCompat.class
Hey there, let's work through this duplicate class error you're hitting when building your APK. This issue happens because the AndroidNCompat.class from Google VR is being included multiple times in your project's dependencies, which breaks the APK packaging step. Here's how to troubleshoot and fix it:
1. Pinpoint the duplicate source
First, you need to figure out exactly which dependencies are bringing in the duplicate class:
- In Android Studio, open the Gradle tool window (View > Tool Windows > Gradle), then navigate to
app > Tasks > android > androidDependencies. Run this task to generate a full dependency tree. - Alternatively, run this command in your project's root directory via terminal:
./gradlew app:dependencies
Looking at your build.gradle, you have several local GVR-related AARs (gvr.aar, gvr_android_common.aar, unitygvr.aar, unitygvractivity.aar). It’s highly likely two or more of these AARs contain the same AndroidNCompat.class file.
2. Remove redundant dependencies
Once you’ve identified which AARs are duplicating the class, remove the redundant ones:
- If
gvr.aaralready includes all the classes fromgvr_android_common.aar, delete this line from your dependencies:compile(name: 'gvr_android_common', ext: 'aar') - If you’re unsure, you can inspect the contents of each AAR: rename the
.aarfile to.zip, extract it, and check ifclasses.jarcontains thecom/google/vr/cardboard/AndroidNCompat.classpath. Keep only one AAR that provides this class.
3. Check for transitive dependencies
Even if you’ve commented out the remote GVR SDK lines, some of your other dependencies (like Unity-related libraries or Firebase/Play Services) might be pulling in GVR classes transitively. Use the dependency tree from step 1 to spot these, and exclude the conflicting transitive dependency if needed. For example:
compile 'some.dependency:here:version' { exclude group: 'com.google.vr', module: 'sdk-base' }
4. Clean and rebuild
After adjusting your dependencies, always:
- Run Build > Clean Project in Android Studio to wipe old build artifacts.
- Then run Build > Rebuild Project to generate a fresh build. This ensures no leftover duplicate classes are still present in the build cache.
5. Sync Unity and Android project settings
Since you’re working with Unity, double-check if Unity’s export process added any hidden GVR dependencies that conflict with your manual Gradle config. Sometimes Unity includes its own versions of VR libraries, so you might need to adjust Unity’s Player Settings (under XR Plug-in Management) to ensure consistency with your Gradle dependencies.
内容的提问来源于stack exchange,提问作者Däñish Shärmä




