Stephen McCormick
Stephen McCormick

Reputation: 1796

Android Studio build works, Gradle command line fails

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

Upvotes: 5

Views: 4219

Answers (3)

Viroth
Viroth

Reputation: 692

In android studio Android Studio Giraffe | 2022.3.1 Mac

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

Upvotes: 1

mokagio
mokagio

Reputation: 17461

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.

Upvotes: 2

Stephen McCormick
Stephen McCormick

Reputation: 1796

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.

Upvotes: 8

Related Questions