Search code examples
androidandroid-studiounity-game-enginegradleapk

Signed APK doesn't work but debug version works fine


I exported my Unity game to android studio because I was using too many DEX files(due to ad network libraries). I can build and run the debug version without any trouble. I can build a signed released version and upload it to the playstore but the game crashes on startup. The logcat says:

FATAL EXCEPTION...
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/Gson

At [java code from an ad network library]

I'm not using proguard and the settings between debug and release are identical apart from having debuggable and Jni debuggable set to false in the release. The scope of the dependencies is Implementation for all(also tried API without any difference).

Some similar questions on SO were saying to clean the project and rebuild but that didn't do anything for me. I'm not sure what else to try. It seems like the libraries are not added to the signed release build for some reason but I don't know why.

EDIT:

Gradle below as requested:

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
buildscript {
   repositories {
      jcenter()
      google()
   }

   dependencies {
      classpath 'com.android.tools.build:gradle:3.2.1'
   }
}

allprojects {
   repositories {
      flatDir {
        dirs 'libs'
      }
       google()
   }
}

apply plugin: 'com.android.application'

dependencies {
   api fileTree(include: ['*.jar'], dir: 'libs')
   api 'com.android.support:multidex:1.0.3'
   implementation(name: 'com.android.support.exifinterface-26.0.1', ext: 'aar')
   implementation(name: 'com.android.support.support-compat-26.0.1', ext: 'aar')
   implementation(name: 'com.android.support.support-core-ui-26.0.1', ext: 'aar')
   implementation(name: 'com.android.support.support-core-utils-26.0.1', ext: 'aar')
   implementation(name: 'com.android.support.support-fragment-26.0.1', ext: 'aar')
   implementation(name: 'com.android.support.support-media-compat-26.0.1', ext: 'aar')
   implementation(name: 'com.android.support.support-v4-26.0.1', ext: 'aar')
   implementation(name: 'com.google.android.gms.play-services-base-11.0.4', ext: 'aar')
   implementation(name: 'com.google.android.gms.play-services-basement-11.0.4', ext: 'aar')
   implementation(name: 'com.google.android.gms.play-services-games-11.0.4', ext: 'aar')
   implementation(name: 'com.google.android.gms.play-services-nearby-11.0.4', ext: 'aar')
   implementation(name: 'com.google.android.gms.play-services-tasks-11.0.4', ext: 'aar')
   implementation(name: 'play-services-ads-11.0.4', ext: 'aar')
   implementation(name: 'play-services-ads-lite-11.0.4', ext: 'aar')
   implementation(name: 'play-services-gass-11.0.4', ext: 'aar')
   implementation(name: 'play-services-location-11.0.4', ext: 'aar')
   api project(':adcolony')
   api project(':appodeal')
   api project(':common_lib')
   api project(':inmobi')
   api project(':native_plugins_lib')
   api project(':ogury')
   api project(':voxelbusters_utility_lib')
}

android {
   compileSdkVersion 28
   buildToolsVersion '28.0.3'

   defaultConfig {
      minSdkVersion 16
      targetSdkVersion 28
      targetSdkVersion 28
      versionCode 10
      versionName "10"
      applicationId '*MY APP ID HERE*'
      multiDexEnabled true
   }

   lintOptions {
      abortOnError false
      disable 'MissingTranslation'
      checkAllWarnings false
      checkReleaseBuilds false
      ignoreWarnings true       // false by default
      quiet true                // false by default
   }

   aaptOptions {
      noCompress '.unity3d', '.ress', '.resource', '.obb'
   }



    signingConfigs {
        release {
            storeFile file('*KEYSTORE PATH HERE*')
            storePassword '*PASSWORD*'
            keyAlias '*My KEY ALIAS*'
            keyPassword '*MY KEY PASSWORD*'
        }
    }
   buildTypes {
      debug {
         minifyEnabled false
         useProguard false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
         jniDebuggable true
         signingConfig signingConfigs.release
      }
      release {
         minifyEnabled false
         useProguard false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'

         signingConfig signingConfigs.release
      }
   }

}

Solution

  • In the gradle file dependencies, add

    implementation 'com.google.code.gson:gson:2.8.5'
    

    Also add

    mavenCentral()
    

    in the repositories.

    Not sure why this isn't a problem in Debug but it is problem in release. I'm guessing one of the ad library has code that uses GSON with a release flag.