androidandroid-productflavorsbuild-variant

Including build variants in android project fails to update production apk on previous version release


My Android App project did not have any build variants earlier and I used to sign the production apk with simply the keystore credentials created by me and installed directly without publishing on play store as an apk file.

But now as the client had requirement that some of the users need to have both UAT and production apks on the same device separately, I introduced build variant and product flavors in the build.gradle file.

The UAT version is with different package name and the production one is kept with same package name as before.

The UAT version is getting installed as it is a new apk with new package name, but the production apk is not getting updated on the current one even though it is with same package name and throwing an error as "App not installed as package appears to be invalid"

Below given is the gradle file code without flavors and with flavors.

Old code without flavors:

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
    // Add the Crashlytics Gradle plugin
    id 'com.google.firebase.crashlytics'
}

android {
    compileSdk 32
    android.defaultConfig.vectorDrawables.useSupportLibrary = true
    defaultConfig {
        applicationId "com.cropinsurance.krishak"
        minSdk 21
        targetSdk 32
        versionCode 14 // play store version 13
        versionName "1.3.6" // play store version 1.3.5 release
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/proguard/androidx-annotations.pro'
        exclude 'META-INF/LICENSE.md'
        exclude 'META-INF/NOTICE.md'
    }
    lintOptions {
        disable 'MissingTranslation'
        checkReleaseBuilds false
    }
    flavorDimensions "dimension_less"
    productFlavors {
        typeBuildSudesiUat {}
        typeBuildSbiUat {}
        typeBuildProd {}
    }

}

dependencies {

    implementation "androidx.autofill:autofill:1.1.0"

    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'com.intuit.sdp:sdp-android:1.0.4'
    implementation 'com.android.volley:volley:1.2.1'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.squareup.retrofit2:retrofit:2.8.1'
    implementation "com.squareup.retrofit2:converter-gson:2.8.1"
    implementation 'com.squareup.okhttp3:logging-interceptor:4.6.0'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'


    //firebase
    implementation platform('com.google.firebase:firebase-bom:31.1.0')
    implementation 'com.google.firebase:firebase-analytics'


        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:31.1.0')
        // Add the dependencies for the Crashlytics and Analytics libraries
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation 'com.google.firebase:firebase-crashlytics'
        implementation 'com.google.firebase:firebase-analytics'

    implementation 'com.google.android.play:core:1.10.3'

}

New code with product flavors :

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
    // Add the Crashlytics Gradle plugin
    id 'com.google.firebase.crashlytics'
}

android {
    signingConfigs {

    /*    debug {
            storeFile file('filepath')
            storePassword 'mypassword'
            keyAlias 'key0'
            keyPassword 'mypassword'
        }
*/

        release {
           storeFile file('filepath')
            storePassword 'mypassword'
            keyAlias 'key0'
            keyPassword 'mypassword'
        }
    }
    compileSdk 32
    android.defaultConfig.vectorDrawables.useSupportLibrary = true
    defaultConfig {
        applicationId "com.cropinsurance.krishak"
        minSdk 21
        targetSdk 32
        versionCode 14 // play store version 13
        versionName "1.3.6" // play store version 1.3.5 release
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        signingConfig signingConfigs.release
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            debuggable false
        }

        debug{
            debuggable true
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/proguard/androidx-annotations.pro'
        exclude 'META-INF/LICENSE.md'
        exclude 'META-INF/NOTICE.md'
    }
    lintOptions {
        disable 'MissingTranslation'
        checkReleaseBuilds false
    }
    flavorDimensions "dimension_less"
    productFlavors {
//        typeBuildSudesiUat {}
        typeBuildSbiUat {
            applicationId 'com.cropinsurance.krishak.uat'
            manifestPlaceholders = [ appLabel : "UAT_Krishak"]
            buildConfigField("String", "URL_BASE_MASTER", '""')
            buildConfigField("String", "URL_BASE_UP", '""')
            buildConfigField("String", "URL_BASE_DOWN", '""')
            buildConfigField("String", "URL_BASE_IMAGE", '""')
        }
        typeBuildProd {
            applicationId 'com.cropinsurance.krishak'
            manifestPlaceholders = [ appLabel : "Krishak"]
            buildConfigField("String", "URL_BASE_MASTER", '""')
            buildConfigField("String", "URL_BASE_UP", '""')
            buildConfigField("String", "URL_BASE_DOWN", '""')
            buildConfigField("String", "URL_BASE_IMAGE", '""')
        }
    }

}

dependencies {

    implementation "androidx.autofill:autofill:1.1.0"

    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'com.intuit.sdp:sdp-android:1.0.4'
    implementation 'com.android.volley:volley:1.2.1'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.squareup.retrofit2:retrofit:2.8.1'
    implementation "com.squareup.retrofit2:converter-gson:2.8.1"
    implementation 'com.squareup.okhttp3:logging-interceptor:4.6.0'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'


    //firebase
    implementation platform('com.google.firebase:firebase-bom:31.1.0')
    implementation 'com.google.firebase:firebase-analytics'


        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:31.1.0')
        // Add the dependencies for the Crashlytics and Analytics libraries
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation 'com.google.firebase:firebase-crashlytics'
        implementation 'com.google.firebase:firebase-analytics'

    implementation 'com.google.android.play:core:1.10.3'

}

Please can anyone suggest what am I doing wrong here? I need the production release flavor to install on the previous production apk as I have kept the same package for production release.

I cannot ask the users to un-install and install the apk as they will loose their data.

Thanks in advance.


Solution

  • Create a signed apk right away will cause the APP NOT INSTALLED),

    YOU SHOULD CLEAN THE PROJECT before creating signed bundle/apk

    If this doesn't work, INVALIDATE CACHE AND RESTART and create a Signed bundle/apk.