Unable to add VersionCode to Test APK

48 Views Asked by At

I am trying to build an initial apk for a test suite that we intend to use for our Clover Payment Device development. Part of the requirements for this upload is a v1 signed apk (solved) with a versionCode (unsolved). For some reason, I can't compile a test apk that has a versionCode built into it. I am building the apk with gradlew assembleAndroidTest I am validating the apk with aapt dump badging <path to apk>

Here is the relevant snippet from my gradle file:

    defaultConfig {
        minSdkVersion 17
        //noinspection ExpiredTargetSdkVersion
        targetSdkVersion 25
        versionCode 11 //project.bambooVersion.toInteger()
        versionName "1.0.1" //project.bambooVersionCode
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        buildConfigField("String", "PROTO_VERSION", '"' + versions.ethorProto + '"')
        buildConfigField("String", "CLOVER_SDK", '"' + clover_sdk + '"')
        buildConfigField("String", "VERSION_NAME", '"' + bambooVersionCode + '"')
    }

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

                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile file(keystoreProperties['storeFile'])
                storePassword keystoreProperties['storePassword']

                enableV1Signing true
                enableV2Signing false
            }
        }
    }

I was very hopeful with this solution: AndroidManifest in androidTest directory being ignored but making a separate "debug" manifest hasn't resolved the issue.

Any insight into this would be greatly appreciated.

1

There are 1 best solutions below

0
On

Posting a "work-around" instead of an actual solution-- i.e. this works but it's ugly and it's not something I'd want to do on a routine basis.

  1. Remove the signing code in gradle (you'll sign it at the end).
  2. Build the test apk using gradlew assembleAndroidTest
  3. Use apktool to decode the apk with apktool d <apkfile>
  4. In the decoded directory, edit the AndroidManifest.xml and put in your android:versionCode and android:versionName data
  5. Rebuild your apk with apktool b <decoded apk directory>
  6. Build your keystore but with "cert" as the alias (see here)
  7. zipalign your apk first zipalign -p -f -v 4 <source-apk> <dest-apk>
  8. sign your apk (make sure you're using a recent version of apksigner! this caught me up) with apksigner.bat sign --ks <keystore_file> --v1-signing-enabled=true --v2-signing-enabled=false --v3-signing-enabled=false --v4-signing-enabled=false --in <dest-apk>