transistorsoft / flutter_background_geolocation

Sophisticated, battery-conscious background-geolocation & geofencing with motion-detection
https://www.transistorsoft.com/shop/products/flutter-background-geolocation
Other
651 stars 242 forks source link

Failed to build apk in release mode #1306

Closed bluestar712 closed 5 months ago

bluestar712 commented 5 months ago

Your Environment

flutter build apk --release

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':background_fetch:verifyReleaseResources'.
> A failure occurred while executing com.android.build.gradle.tasks.VerifyLibraryResourcesTask$Action
   > Android resource linking failed
     ERROR:D:\Flutter_Example\Sample2\30_RangerGuard\rangerguard\build\background_fetch\intermediates\merged_res\release\values\values.xml:2520: AAPT: error: resource android:attr/lStar not found.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

It's my build.gradle in app

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

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

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

apply plugin: 'com.google.gms.google-services' 
Project background_geolocation = project(':flutter_background_geolocation')
apply from: "${background_geolocation.projectDir}/background_geolocation.gradle"

android {
    namespace "com.example.rangerguard"
    compileSdkVersion 34
    ndkVersion flutter.ndkVersion

    kotlinOptions {
        jvmTarget = '1.8'
    }

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

    defaultConfig {       
        applicationId "com.epi.rangerguard"       
        minSdkVersion 21
        targetSdkVersion 34
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {        
        coreLibraryDesugaringEnabled true        
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    signingConfigs {
        release {
            if (keystorePropertiesFile.exists()) {
                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile file(keystoreProperties['storeFile'])
                storePassword keystoreProperties['storePassword']
            }
        }
    }

    buildTypes {
        release {            
            // signingConfig signingConfigs.debug
            signingConfig signingConfigs.release
            minifyEnabled true
            shrinkResources false
            proguardFiles "proguard-rules.pro"
            proguardFiles "${background_geolocation.projectDir}/proguard-rules.pro"
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    implementation platform('com.google.firebase:firebase-bom:32.1.1')
    implementation 'com.google.firebase:firebase-analytics-ktx'

    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
    implementation 'androidx.window:window:1.0.0'
    implementation 'androidx.window:window-java:1.0.0'
}

It's my build.gradle in android

ext {
    appCompatVersion    = "1.4.2"
    playServicesLocationVersion = "21.1.0"
    hmsLocationVersion  = "6.12.0.300" 
    removeBackgroundGeolocationDebugSoundsInRelease = false
}

buildscript {
    ext.kotlin_version = '1.8.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:8.3.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.15'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url "${project(':flutter_background_geolocation').projectDir}/libs" }
        maven { url 'https://developer.huawei.com/repo/' }
        maven { url "${project(':background_fetch').projectDir}/libs" }
    }
}

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

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}
christocracy commented 5 months ago

Post your gradle dependencies:

$ cd android 
$ ./gradlew app:dependencies
christocracy commented 5 months ago

Also review the Android Setup Instructions where it was recommended to use gradle ext vars use ext.compileSdkVersion, ext.targetSdkVersion rather than hard-coding in your app/build.gradle (where 3rd party plug-ins cannot be aware of):

compileSdkVersion 34

christocracy commented 5 months ago

There are plenty of post about this in SO:

https://stackoverflow.com/questions/69033022/message-error-resource-androidattr-lstar-not-found

bluestar712 commented 5 months ago

Thank you so much! I fixed this issue.

Added these codes on app/build.gradle

android {

    compileSdkVersion rootProject.ext.compileSdkVersion

    buildTypes {
        release {            
            signingConfig signingConfigs.debug
            // signingConfig signingConfigs.release
            minifyEnabled true
            shrinkResources false
            proguardFiles "proguard-rules.pro"
            proguardFiles "${background_geolocation.projectDir}/proguard-rules.pro"
        }
    }

    configurations.all {
        resolutionStrategy {
            force 'androidx.core:core:1.6.0'
            force 'androidx.core:core-ktx:1.6.0'
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'androidx.core:core-ktx:1.6.0' 
}

Removed gradle dependencies on android/build.gradle


ext {
    compileSdkVersion   = 34
    appCompatVersion    = "1.4.2"
    playServicesLocationVersion = "21.1.0"
    hmsLocationVersion  = "6.12.0.300" 
    removeBackgroundGeolocationDebugSoundsInRelease = false
}

buildscript {
    ext.kotlin_version = '1.8.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        // classpath "com.android.tools.build:gradle:8.3.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.15'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url "${project(':flutter_background_geolocation').projectDir}/libs" }
        maven { url 'https://developer.huawei.com/repo/' }
        maven { url "${project(':background_fetch').projectDir}/libs" }
    }
}

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

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

By my test, this command only working on debug mode. (signingConfig signingConfigs.debug)

flutter build apk --release

And this command is working well on release mode. ( signingConfig signingConfigs.release)

flutter build appbundle

Hope it will help to others that facing this issue... Thank you for a great support!

christocracy commented 5 months ago

I do not like this:

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.0'
}

You should not need to manually add that (and certainly not an old version 1.3.0 from May 2021.

The latest version of appcompat is 1.7.0

bluestar712 commented 5 months ago

Yes, you are right. Thank you!