5

I have ported an app from Eclipse to Android Studio, implemented my needed flavors and have it allow working in Android Studio. Due to the number of flavors I would prefer to build it using command line Gradle. However when I run the following command to build my release APKs

gradlew.bat assembleRelease 

I get the following error:

FAILURE: Build failed with an exception. * Where: Build file 'C:\whatever\build.gradle' line: 1 * What went wrong: A problem occurred evaluating project ':MyCompany'. > java.lang.UnsupportedClassVersionError: com/android/build/gradle/AppPlugin : Unsupported major.minor version 52.0 * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED 

I have run with --stacktrace and --debug and it really does not offer any other information, other than pointing the error to be with the first line of the build.gradle:

apply plugin: 'com.android.application' 

I have attempted the following based on other stackoverflow responses to this error:

  1. Upgraded to JavaVersion.VERSION_1_8
  2. Added Jack the build.gradle (and that slowed everything down, but it was required for 1.8, even though it is deprecated?)
  3. Increased memory for heap (org.gradle.jvmargs=-Xmx4096m) and dex (javaMaxHeapSize "4g").

I am running Android Studio 2.3.3

Here is my build.gradle file. I have left only a couple of the flavors to save space, and renamed some of the stuff to protect the innocent. I have left in the dependencies in case that might be the problem? The only funkiness is that I am renaming the APK and pushing it to a different folder, but that is all working when I run the release build directly out of Android Studio. The build.gradle:

apply plugin: 'com.android.application' apply plugin: 'io.fabric' buildscript { repositories { maven { url 'https://maven.fabric.io/public' } } dependencies { classpath 'io.fabric.tools:gradle:1.+' } } repositories { maven { url 'https://maven.fabric.io/public' } } android { signingConfigs { config { keyAlias 'whatever' keyPassword 'imnotgoingtotellyou' storeFile file('C:/whereever/mycompany.keystore') storePassword 'yeps' } } compileSdkVersion 25 buildToolsVersion '25.0.3' defaultConfig { jackOptions { enabled true additionalParameters('jack.incremental': 'true') } applicationId "com.mycompany.default" minSdkVersion 14 targetSdkVersion 23 versionCode 44 versionName "2.1.44" compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } signingConfig signingConfigs.config } dexOptions { javaMaxHeapSize "4g" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' signingConfig signingConfigs.config } } productFlavors.whenObjectAdded { flavor -> flavor.ext.set('directoryPath', '') flavor.ext.set('apkName', '') } productFlavors { Flavor1 { signingConfig signingConfigs.config directoryPath = 'flavor1' } Flavor2 { applicationId 'com.mycompany.flavor2' signingConfig signingConfigs.config directoryPath = 'flavor2' } applicationVariants.all { variant -> variant.outputs.each { output -> def path = "C:/AndroidBuilds/MyBuild.Build/" + variant.productFlavors[0].directoryPath + "/" logger.error("Path = " + path) def SEP = "-" def apkName = variant.productFlavors[0].apkName def flavor = variant.productFlavors[0].name if (apkName != '') flavor = apkName; def version = variant.versionCode def newApkName = path + version + SEP + flavor logger.error("newApkName = " + newApkName) output.outputFile = new File(newApkName + ".apk") } } } dependencies { compile project(':androidpdfview100') compile 'com.android.support:support-v13:25.3.1' compile 'com.google.code.gson:gson:2.7' compile 'joda-time:joda-time:2.5' compile 'com.google.android.gms:play-services-maps:11.0.4' compile files('libs/httpmime-4.2.5.jar') compile files('libs/itextg-5.4.4.jar') compile files('libs/js.jar') compile files('libs/logentries-android-2.1.1.jar') compile files('libs/universal-image-loader-1.8.6.jar') compile files('libs/xmlworker-5.4.4.jar') compile files('libs/zbar.jar') compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') { transitive = true; } } 

UPDATE

The project structure looks like this:

It was generated automatically by Android Studio when I ported the code from Eclipse:

enter image description here

When you drill into the App itself it looks like this:

enter image description here

2
  • what is the structure of the project and where from is the command ran? btw using non-static versions is kind of scary. After a while, it might stop working, as the API could change. Commented Aug 21, 2017 at 8:45
  • I have added the project structure (changing the product name and such for security reasons). As for the non-static versions, I assume you mean the fabric reference - that was generated by the Fabric plugin, nothing I did. In the following link it was recommended to leave that way : github.com/twitter/twitter-kit-android/issues/42. But I do not think it is the reason for the failure (it works in the IDE). Commented Aug 21, 2017 at 16:24

3 Answers 3

8

For anyone facing this issue in the future, for the command line build make sure your JAVA_HOME is set to the same version as Android Studio is using. E.g. JDK v8:

JAVA_HOME=C:\Program Files\Java\jdk1.8.0_112 

Or wherever your 1.8 JDK is. It is a beast and sucks up memory, but it was the only way to get around this build error. I would NOT recommend that you build in Android Studio with 1.8 since it seems to require Jack which is just plain slow. And sadly it is deprecated to boot.

Sign up to request clarification or add additional context in comments.

1 Comment

I think ZSH on macOS, as @mokagio alluded to, is the culprit. On two machines only the one with ZSH needed this variable exported.
2

I work on macOS with Zsh as my shell and have just downloaded Android Studio 3.6.2.

I added the following to my ~/.zshenv file, and everything worked.

export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home" 

I believe updating ~/.profile would do it as well, but I haven't tried it.

Comments

1

In android studio Android Studio Giraffe | 2022.3.1 Mac

export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.