LiveRock
LiveRock

Reputation: 1033

Flutter Android Build Failed : com.android.tools.r8.CompilationFailedException: Compilation failed to complete

Been trying for days; googling and trying all sorts of 'fixes' and 'dependencies' etc but still not able to build APK for release. Debug is ok.

My command: flutter build apk --release error: AILURE: Build failed with an exception.

BUILD FAILED in 2m 1s
Running Gradle task 'assembleRelease'...
Running Gradle task 'assembleRelease'... Done 147.3s (!) [!] The shrinker may have failed to optimize the Java bytecode. To disable the shrinker, pass the --no-shrink flag to this command. To learn more, see: https://developer.android.com/studio/build/shrink-code Gradle task assembleRelease failed with exit code 1

build.grade (app):

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
    throw new GradleException("versionCode not found. Define flutter.versionCode in the local.properties file.")
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'4
    throw new GradleException("versionName not found. Define flutter.versionName in the local.properties file.")
}

apply plugin: 'com.android.application'
// 20/2/20
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
// 20/2/20
apply plugin: 'com.google.gms.google-services'
// Add the Fabric plugin. 10 March 2020
apply plugin: 'io.fabric'


def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

def kotlin_version = '1.3.21'

android {
    compileSdkVersion 28

    // 20/2/20
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "net.hotsource.peribuddy"
        minSdkVersion 24
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        //testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        // Enabling multidex support.
        multiDexEnabled true


    }

    signingConfigs {
    release {
        keyAlias keystoreProperties['keyAlias']
        keyPassword keystoreProperties['keyPassword']
        storeFile file(keystoreProperties['storeFile'])
        storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {

            minifyEnabled true
            //shrinkResources true


            //useProguard true
            //proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            signingConfig signingConfigs.release

            ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a'

            }

            }
        debug {
            // minifyEnabled true
            }

        }

    }


flutter {
    source '../..'
}

dependencies {

    // 28 May 2019
    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'androidx.test:runner:1.2.0'

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'androidx.appcompat:appcompat:1.1.0' // 1.0.2


    implementation 'com.facebook.android:facebook-android-sdk:[5,6)'

    implementation 'com.android.support:multidex:1.0.3'

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

    // 10 March 2020
    // (Recommended) Add the Google Analytics dependency.
    implementation 'com.google.firebase:firebase-analytics:17.4.2' // 17.2.3

    // Add the Firebase Crashlytics dependency.
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

    implementation 'com.google.firebase:firebase-messaging:20.2.0'
    implementation "com.android.support:support-v4:28.0.0"
    implementation "com.android.support:support-fragment:28.0.0"

    implementation 'com.google.firebase:firebase-firestore:21.4.3'
    implementation 'com.google.firebase:firebase-firestore-ktx:21.4.3'
}

// firebase_crashlytics
apply plugin: 'io.fabric'
apply plugin: 'com.google.gms.google-services'

build.gradle (project):

version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()

        // For Crashlytics SDK
        maven {
            url 'https://maven.fabric.io/public'

        }


    }

    dependencies {
        classpath 'com.android.tools:r8:1.6.84'  // Must be before the Gradle Plugin for Android.
        classpath 'com.android.tools.build:gradle:3.4.2' // 3.1.2 3.3.0-alpha13 4.10.2 330 3.3.2 3.6.3
        // classpath 'com.google.gms:google-services:4.2.0'   // 3.2.1 4.0.1 4.1.0 4.3.3
        classpath 'com.google.gms:google-services:4.2.0'

        // Add the Fabric Crashlytics plugin. 10 march 2020 1.31.2
        classpath 'io.fabric.tools:gradle:1.31.2'

        //classpath 'io.fabric.tools:gradle:1.+' // new
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // Add fabric classpath
        //classpath 'io.fabric.tools:gradle:1.26.1'
    }
}

allprojects {


    repositories {
        google()
        jcenter()



    }


    configurations.matching { it.name == '_internal_aapt2_binary' }.all { config ->
        config.resolutionStrategy.eachDependency { details ->
            details.useVersion("3.5.0-alpha03-5252756")
        }
    }


}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Upvotes: 6

Views: 7987

Answers (4)

Alex
Alex

Reputation: 76

I was able to solve this problem by adding the following elements r8 maven and classpath to the android/build.gradle file:

buildscript {
    ext.kotlin_version = '1.9.10'
    repositories {
        google()
        mavenCentral()
        maven {
            // r8 maven
            url = uri("https://storage.googleapis.com/r8-releases/raw")
        }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2'
        // START: FlutterFire Configuration
        classpath 'com.google.gms:google-services:4.4.0'
        // END: FlutterFire Configuration
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.android.tools:r8:8.2.33"
    }
}

Upvotes: 0

Anand A L
Anand A L

Reputation: 177

In My case, after i run with -verbose

flutter build apk -v

"MainActivity is defined multiple times" after removed one of that apk build successful.

MainActivity found in both kotin & java subfolder in android folder

this happens when i add external plugin

Upvotes: 0

vahitdurmus
vahitdurmus

Reputation: 39

the problem, in Android studio related with jdk version. you can go through project structure and change Gradle's jdk. wish it will solve.

Upvotes: 0

Slow T
Slow T

Reputation: 51

face the similar issue. what I did is just disable R8. as in your terminal :

flutter build apk --no-shrink

or in your app level build.gradle:

minifyEnabled false
shrinkResources false

then build apk as normal

by the way, my apk works without R8, because I don't know how to stop it deleting my files

Upvotes: -1

Related Questions