1

I am using Android Studio 2.1 with NDK. But I am facing issues about how to configure for versionCode. From this androidbycode blog link and this answer from Stackoverflow I tried to edit my original bulid.gradle, but it shows error on syncing.

Build.gradle (it's working fine, when there is no version merging)

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"

        defaultConfig {
            applicationId "com.mytestapp"
            minSdkVersion.apiLevel 15
            targetSdkVersion.apiLevel 23
            versionCode 101
            versionName "1.0.1"
        }

        buildTypes {
            release {
                minifyEnabled true
                proguardFiles.add(file("proguard-rules.pro"))
                signingConfig = $("android.signingConfigs.myConfig")
            }
            debug {
                debuggable true
            }
        }

        productFlavors {
            create("armeabi") {
                ndk {
                    abiFilters.add("armeabi")
                    versionCode 1
                }
            }
            create("armeabi-v7a") {
                ndk {
                    abiFilters.add("armeabi-v7a")
                    versionCode 3
                }
            }
            create("x86") {
                ndk {
                    abiFilters.add("x86")
                    versionCode 6
                }
            }
            create("fat") {
                ndk {
                    abiFilters.add("armeabi")
                    abiFilters.add("armeabi-v7a")
                    abiFilters.add("x86")
                    versionCode 0
                }
            }
        }
    }

    // You can modify the NDK configuration for each variant.
    components.android {
        binaries.afterEach { binary ->
            binary.mergedNdkConfig.cppFlags.add(
                    "-DVARIANT=\"" + binary.name + "\"")
        }
    }


    android.signingConfigs {
        create("myConfig") {
            storeFile "MyKeyStoreLocation"
            storePassword "MyStorePassword"
            keyAlias "mykeyAlias"
            keyPassword "mykeyPassword"
            storeType "jks"
        }
    }

    android.ndk {
        moduleName = "settings-jni"
    }

}

repositories {
    mavenCentral()
    maven {
        url  "http://dl.bintray.com/lukaville/maven"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.google.android.gms:play-services-ads:8.4.0'
    compile 'net.zetetic:android-database-sqlcipher:3.4.0@aar'
    compile 'com.nbsp:library:1.08'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'com.android.support:cardview-v7:23.4.0'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.mcxiaoke.volley:library:1.+'
    compile 'com.daimajia.numberprogressbar:library:1.2@aar'
    compile 'com.github.paolorotolo:appintro:3.4.0'
}

I couldn't find a way to merge the code snippet for ABI versioning, as suggested in above mentioned SO answer

applicationVariants.all { variant ->
        // get the version code of each flavor
        def abiVersion = variant.productFlavors.get(0).versionCode

        // set the composite code
        variant.mergedFlavor.versionCode = abiVersion * 100000 + defaultConfig.versionCode
    }

Or, as suggested in above mentioned blog link, with my build.gradle -

project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]

    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.versionCodeOverride =
                project.ext.versionCodes.get(output.getFilter(
                    com.android.build.OutputFile.ABI), 0) * 10000000 + android.defaultConfig.versionCode
        }
    }
Community
  • 1
  • 1
Dr. Atul Tiwari
  • 1,085
  • 5
  • 22
  • 46

1 Answers1

1

you can follow this bug that corresponds to the issue you're having: https://code.google.com/p/android/issues/detail?id=192943

I gave a solution that will still work for you as you're using flavors - define a versionCodeBase and use it to generate the version code for each flavor:

model {
    def versionCodeBase = 11;
    def versionCodePrefixes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9];

    //...

    android.productFlavors {
        create ("armv7") {
            ndk.abiFilters.add("armeabi-v7a")
            versionCode = versionCodePrefixes.get("armeabi-v7a", 0) * 1000000 + versionCodeBase
        }
        create ("x86") {
            ndk.abiFilters.add("x86")
            versionCode = versionCodePrefixes.get("x86", 0) * 1000000 + versionCodeBase
        }
    }
}
ph0b
  • 14,353
  • 4
  • 43
  • 41
  • Thank you, your answer worked (although I have to modify it, a bit). I have some doubts - 1. I was unable to define `versionCodeBase` and `versionCodePrefixes` from inside `model`. Defining them outside of `model` is working fine. Why is it so and would it cause any future problem? 2. Since the version is now calculated from these 2 variables only, can and should I remove `versionCode` from my `defaultConfig`? (.. one more question, character limit occured) – Dr. Atul Tiwari May 17 '16 at 18:39
  • 3. As mentioned in my OP, I am using a `fat` release too. Should I specify it in `versionCodePrefixes`. At present I am just using following line in my `fat` productFlavor :=> `versionCode = versionCodePrefixes.get("fat", 0) * 1000000 + versionCodeBase` But this will output the plain `versionCodeBase` (11 in eg), as I have not defined any value for `fat` release. Is using this method for versioning of `fat` correct? as it would output version in 2 digits while others will be released in 7 digits?(Upvoted you, Will mark it as the answer, as soon as my doubts get cleared at least to some extent) – Dr. Atul Tiwari May 17 '16 at 18:44
  • For #1, if you need to know, I am using `gradle-experimental:0.7.0` – Dr. Atul Tiwari May 17 '16 at 18:46
  • 1
    1. It's ok. 2. You can set the global *versionCode* to *versionCodeBase* to play on the safe side. 3. This method to set the version code for the fat flavor is perfectly fine. You can also remove *versionCode* tuning for the fat flavor so it will use the *versionCode* set globally (equal to *versionCodeBase*) – ph0b May 17 '16 at 20:29
  • Thank you for your explanations. Please help me one more time, how to set "global versionCode to versionCodeBase" (for #2 & #3)? It appears, I am in very primitive stage in learning gradle, which looked to be the easiest thing. – Dr. Atul Tiwari May 18 '16 at 04:44
  • I meant you should do this: `model { android { defaultConfig { versionCode versionCodeBase } }` – ph0b May 25 '16 at 17:51