Search code examples
androidcordovaandroidxcordova-android

How to add Biometric support API (AndroidX) in my Old Android Project


SO After being an iOS Developer for 8 and half years , I started learning andorid 4 months ago and I was given a Cordova andorid project to work on. Task was to integrate Fingerprint and face ID Login.

What i have in my current project ( very old project ) :

build gradle : 2.3.3 gradle version : 3.3 cordova-android version : 5.1.4

I know these are very old .

What i tried so far in last 10 days :

I tried to upgrade above to latest build gradle and gradle version. Whole bunch of errors appears , one after another.

I have tried adding bio-metric support library in build.gradle and in build-extra.gradle one by one .. but nothing works...

My Qustions:

  1. Why cant i add bio-metric support library When i have everything compile and work fine on build gradle 2.3.3 and gradle 3.3 ?

  2. Do i have to update gradle and every support library version to latest ? If yes then will it cause issues in code ? Lets say i have used Any old UI component which wwill generate compile time errors when i try to merge old support libraries with new biometric API. I tried it.

  3. Can anyone just tell me the step by step of upgrading this project so that even there are compile time errors i can remove those and then once everything works fine i can add biometric support library.

  4. Lastly Biometric support libraries can be integrated into any old project , thats why they are called support libraries. Correct me if i am wrong.

I am using Android studio on MAC OSX

Following are the files :

Build-extra.gradle :

 ext.postBuildExtras = {
    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
        useLibrary 'org.apache.http.legacy'
    }

    dependencies {
        //compile 'com.google.android.gms:play-services:8.4.0'
        compile 'com.android.support:appcompat-v7:23.3.0'
        compile "com.squareup.picasso:picasso:2.4.0"
        compile "com.android.support:design:25.0.0"
        compile "com.google.firebase:firebase-messaging:9.2.0" //FCM Config
        compile 'com.rmtheis:tess-two:6.0.2'
        compile 'com.github.bumptech.glide:glide:3.8.0'
        compile 'com.android.support:support-v4:19.1.0'
    }
}

build.gradle

// GENERATED FILE! DO NOT EDIT!

apply plugin: 'com.android.application'

buildscript {
    repositories {
        jcenter{ url "http://jcenter.bintray.com/" }
    }

    // Switch the Android Gradle plugin version requirement depending on the
    // installed version of Gradle. This dependency is documented at
    // http://tools.android.com/tech-docs/new-build-system/version-compatibility
    // and https://issues.apache.org/jira/browse/CB-8143
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "com.google.gms:google-services:3.0.0" //FCM Config

    }
   }


// Allow plugins to declare Maven dependencies via build-extras.gradle.
repositories {
    mavenCentral()
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.8'
}

// Configuration properties. Set these via environment variables, build-extras.gradle, or gradle.properties.
// Refer to: http://www.gradle.org/docs/current/userguide/tutorial_this_and_that.html
ext {
    apply from: 'CordovaLib/cordova.gradle'
    // The value for android.compileSdkVersion.
    if (!project.hasProperty('cdvCompileSdkVersion')) {
        cdvCompileSdkVersion = null;
    }
    // The value for android.buildToolsVersion.
    if (!project.hasProperty('cdvBuildToolsVersion')) {
        cdvBuildToolsVersion = null;
    }
    // Sets the versionCode to the given value.
    if (!project.hasProperty('cdvVersionCode')) {
        cdvVersionCode = null
    }
    // Sets the minSdkVersion to the given value.
    if (!project.hasProperty('cdvMinSdkVersion')) {
        cdvMinSdkVersion = null
    }
    // Whether to build architecture-specific APKs.
    if (!project.hasProperty('cdvBuildMultipleApks')) {
        cdvBuildMultipleApks = null
    }
    // .properties files to use for release signing.
    if (!project.hasProperty('cdvReleaseSigningPropertiesFile')) {
        cdvReleaseSigningPropertiesFile = null
    }
    // .properties files to use for debug signing.
    if (!project.hasProperty('cdvDebugSigningPropertiesFile')) {
        cdvDebugSigningPropertiesFile = null
    }
    // Set by build.js script.
    if (!project.hasProperty('cdvBuildArch')) {
        cdvBuildArch = null
    }

    // Plugin gradle extensions can append to this to have code run at the end.
    cdvPluginPostBuildExtras = []
}

// PLUGIN GRADLE EXTENSIONS START
// PLUGIN GRADLE EXTENSIONS END

def hasBuildExtras = file('build-extras.gradle').exists()
if (hasBuildExtras) {
    apply from: 'build-extras.gradle'
}

// Set property defaults after extension .gradle files.
if (ext.cdvCompileSdkVersion == null) {
    ext.cdvCompileSdkVersion = privateHelpers.getProjectTarget()
}
if (ext.cdvBuildToolsVersion == null) {
    ext.cdvBuildToolsVersion = privateHelpers.findLatestInstalledBuildTools()
}
if (ext.cdvDebugSigningPropertiesFile == null && file('debug-signing.properties').exists()) {
    ext.cdvDebugSigningPropertiesFile = 'debug-signing.properties'
}
if (ext.cdvReleaseSigningPropertiesFile == null && file('release-signing.properties').exists()) {
    ext.cdvReleaseSigningPropertiesFile = 'release-signing.properties'
}

// Cast to appropriate types.
ext.cdvBuildMultipleApks = cdvBuildMultipleApks == null ? false : cdvBuildMultipleApks.toBoolean();
ext.cdvMinSdkVersion = cdvMinSdkVersion == null ? null : Integer.parseInt('' + cdvMinSdkVersion)
ext.cdvVersionCode = cdvVersionCode == null ? null : Integer.parseInt('' + cdvVersionCode)

def computeBuildTargetName(debugBuild) {
    def ret = 'assemble'
    if (cdvBuildMultipleApks && cdvBuildArch) {
        def arch = cdvBuildArch == 'arm' ? 'armv7' : cdvBuildArch
        ret += '' + arch.toUpperCase().charAt(0) + arch.substring(1);
    }
    return ret + (debugBuild ? 'Debug' : 'Release')
}

// Make cdvBuild a task that depends on the debug/arch-sepecific task.
task cdvBuildDebug
cdvBuildDebug.dependsOn {
    return computeBuildTargetName(true)
}

task cdvBuildRelease
cdvBuildRelease.dependsOn {
    return computeBuildTargetName(false)
}

task cdvPrintProps << {
    println('cdvCompileSdkVersion=' + cdvCompileSdkVersion)
    println('cdvBuildToolsVersion=' + cdvBuildToolsVersion)
    println('cdvVersionCode=' + cdvVersionCode)
    println('cdvMinSdkVersion=' + cdvMinSdkVersion)
    println('cdvBuildMultipleApks=' + cdvBuildMultipleApks)
    println('cdvReleaseSigningPropertiesFile=' + cdvReleaseSigningPropertiesFile)
    println('cdvDebugSigningPropertiesFile=' + cdvDebugSigningPropertiesFile)
    println('cdvBuildArch=' + cdvBuildArch)
    println('computedVersionCode=' + android.defaultConfig.versionCode)
    android.productFlavors.each { flavor ->
        println('computed' + flavor.name.capitalize() + 'VersionCode=' + flavor.versionCode)
    }
}

android {
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }
    }

    defaultConfig {
        //versionCode cdvVersionCode ?: Integer.parseInt("" + privateHelpers.extractIntFromManifest("versionCode") + "0")
        versionCode  Integer.parseInt("" + privateHelpers.extractIntFromManifest("versionCode"))
        applicationId privateHelpers.extractStringFromManifest("package")

        if (cdvMinSdkVersion != null) {
            minSdkVersion cdvMinSdkVersion
        }
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////



    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////

    lintOptions {
        abortOnError false;
        disable "ResourceType"
    }

    compileSdkVersion cdvCompileSdkVersion
    buildToolsVersion cdvBuildToolsVersion

    if (Boolean.valueOf(cdvBuildMultipleApks)) {
        productFlavors {
            armv7 {
                //versionCode defaultConfig.versionCode + 2
                ndk {
                    abiFilters "armeabi-v7a", ""
                }
            }
            x86 {
                //versionCode defaultConfig.versionCode + 4
                ndk {
                    abiFilters "x86", ""
                }
            }
            all {
                ndk {
                    abiFilters "all", ""
                }
            }
        }
    } else if (!cdvVersionCode) {
      def minSdkVersion = cdvMinSdkVersion ?: privateHelpers.extractIntFromManifest("minSdkVersion")
      // Vary versionCode by the two most common API levels:
      // 14 is ICS, which is the lowest API level for many apps.
      // 20 is Lollipop, which is the lowest API level for the updatable system webview.
      if (minSdkVersion >= 20) {
        //defaultConfig.versionCode += 9
      } else if (minSdkVersion >= 14) {
        //defaultConfig.versionCode += 8
      }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }

   if (cdvReleaseSigningPropertiesFile) {
        signingConfigs {
            release {
                // These must be set or Gradle will complain (even if they are overridden).
                keyAlias = ""
                keyPassword = "__unset" // And these must be set to non-empty in order to have the signing step added to the task graph.
                storeFile = null
                storePassword = "__unset"
            }
        }
        buildTypes {
            release {
                signingConfig signingConfigs.release
            }
        }
        addSigningProps(cdvReleaseSigningPropertiesFile, signingConfigs.release)
    }
    if (cdvDebugSigningPropertiesFile) {
        addSigningProps(cdvDebugSigningPropertiesFile, signingConfigs.debug)
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    // SUB-PROJECT DEPENDENCIES START
    debugCompile project(path: "CordovaLib", configuration: "debug")
    releaseCompile project(path: "CordovaLib", configuration: "release")
    // SUB-PROJECT DEPENDENCIES END

    compile 'com.android.support:design:25.0.0'

}

def promptForReleaseKeyPassword() {
    if (!cdvReleaseSigningPropertiesFile) {
        return;
    }
    if ('__unset'.equals(android.signingConfigs.release.storePassword)) {
        android.signingConfigs.release.storePassword = privateHelpers.promptForPassword('Enter key store password: ')
    }
    if ('__unset'.equals(android.signingConfigs.release.keyPassword)) {
        android.signingConfigs.release.keyPassword = privateHelpers.promptForPassword('Enter key password: ');
    }
}

gradle.taskGraph.whenReady { taskGraph ->
    taskGraph.getAllTasks().each() { task ->
        if (task.name == 'validateReleaseSigning') {
            promptForReleaseKeyPassword()
        }
    }
}

def addSigningProps(propsFilePath, signingConfig) {
    def propsFile = file(propsFilePath)
    def props = new Properties()
    propsFile.withReader { reader ->
        props.load(reader)
    }

    def storeFile = new File(props.get('key.store') ?: privateHelpers.ensureValueExists(propsFilePath, props, 'storeFile'))
    if (!storeFile.isAbsolute()) {
        storeFile = RelativePath.parse(true, storeFile.toString()).getFile(propsFile.getParentFile())
    }
    if (!storeFile.exists()) {
        throw new FileNotFoundException('Keystore file does not exist: ' + storeFile.getAbsolutePath())
    }
    signingConfig.keyAlias = props.get('key.alias') ?: privateHelpers.ensureValueExists(propsFilePath, props, 'keyAlias')
    signingConfig.keyPassword = props.get('keyPassword', props.get('key.alias.password', signingConfig.keyPassword))
    signingConfig.storeFile = storeFile
    signingConfig.storePassword = props.get('storePassword', props.get('key.store.password', signingConfig.storePassword))
    def storeType = props.get('storeType', props.get('key.store.type', ''))
    if (!storeType) {
        def filename = storeFile.getName().toLowerCase();
        if (filename.endsWith('.p12') || filename.endsWith('.pfx')) {
            storeType = 'pkcs12'
        } else {
            storeType = signingConfig.storeType // "jks"
        }
    }
    signingConfig.storeType = storeType
}

for (def func : cdvPluginPostBuildExtras) {
    func()
}

// This can be defined within build-extras.gradle as:
//     ext.postBuildExtras = { ... code here ... }
if (hasProperty('postBuildExtras')) {
    postBuildExtras()
}

apply plugin: "com.google.gms.google-services" //FCM Config

allprojects {
    repositories {
        jcenter{ url "http://jcenter.bintray.com/" }
    }
}

Build.gradle(Module: CordovaLib)

buildscript {
    repositories {
        jcenter{ url "http://jcenter.bintray.com/" }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
    }

}

apply plugin: 'com.android.library'

ext {
    apply from: 'cordova.gradle'
    cdvCompileSdkVersion = privateHelpers.getProjectTarget()
    cdvBuildToolsVersion = privateHelpers.findLatestInstalledBuildTools()
}

android {
    compileSdkVersion cdvCompileSdkVersion
    buildToolsVersion cdvBuildToolsVersion
    publishNonDefault true

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
    buildToolsVersion '25.0.0'
}

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

Yes Its a hybrid app. Using Cordova-android.

I have tried searching on google like anything , but could not find a simple demo android ( Cordova android) project which uses the latest biometric support library.

ok so the above link is a working demo android app .

Just one Question bother me : Why cant i add this line in build.gradle (or where ever i need to add this ) or How can i add it so that my current project in AS start using Bio-metric API for face ID login. dependencies { implementation 'androidx.biometric:biometric:1.0.0-alpha03'

NOTE: Bounty will be awarded to the one who will provide step by step solution of how can i integrate biometric support API in my current project which uses old support libraries as shown in my build-extra.gradle file .


Solution

  • So finally after 14 days of struggle i made my 3 year old code working fine.

    here are the detailed steps in case anyone is looking for same thing.

    1. So I had a Cordova android hybrid app old code . using Android studio 3.2 . Build gradle version was very old , 2.8 and gradle version was 3.3.

    2. I first of all upgraded Android studio to latest version 3.3.2

    3. Now i decided to migrate the whole project to androidX. Remember it wont even let me do that with the previous version of Android studio, i dont know why.

    4. When i clicked on Refactor -> Migrate to AndroidX. A pop up appeared saying "Upgrade the gradle version. So now I updated gradle version to 4.10.1 , it is still giving me error if i upgrade it to 5.2 ( i dont know why , I am still new to Android). Also updated build gradle to 3.3.2

    5.My build.gradle (Module : App) looks like this :

    apply plugin: 'com.android.application'
    
    buildscript {
        repositories {
            jcenter{ url "http://jcenter.bintray.com/" }
            google()
        }
    
        // Switch the Android Gradle plugin version requirement depending on the
        // installed version of Gradle. This dependency is documented at
        // http://tools.android.com/tech-docs/new-build-system/version-compatibility
        // and https://issues.apache.org/jira/browse/CB-8143
        dependencies {
            classpath 'com.android.tools.build:gradle:3.3.2'
            classpath "com.google.gms:google-services:3.0.0" //FCM Config
    
        }
       }
    
    1. Now App is syncing fine , Build ok. I again tried Refactor -> Migrate to androidX. This time Android studio started refactoring the code and provided me 70 code change suggestions .

    2. These code changes are mainly the header file changes like : import "" . So I opened this link - https://developer.android.com/jetpack/androidx/migrate and changed every import statement to the equal androidx statment.

    3. After copy pasting all the changes I again compiled and synced the code . after 3 resources and code compilation error , I was able to build the code . This whole process took 1.2 hours .

    4. Finally i was able to import the biometric support API in build-extras.gradle (Module : app) , look at the file :

          dependencies {
              api 'androidx.appcompat:appcompat:1.0.2'
              api "com.squareup.picasso:picasso:2.4.0"
              api "com.google.android.material:material:1.1.0-alpha04"
              api "com.google.firebase:firebase-messaging:9.2.0" //FCM Config
              api 'com.rmtheis:tess-two:6.0.2'
              api 'com.github.bumptech.glide:glide:3.8.0'
              api 'androidx.legacy:legacy-support-v4:1.0.0'
      
              api "androidx.biometric:biometric:1.0.0-alpha03"
          }
      }
      
    5. Finally , I was able to build the complete code and sync it . So happy finally did it. Now i just have to use biometric API functions to integrate it into my code ( notice this code was written 3 years ago and given to me for integrating latest biometric API).

    Yes I needed step by step answer like this one.

    Still thanks to all who helped.