Gradle android build for different processor architectures

前端 未结 4 1309
孤街浪徒
孤街浪徒 2020-12-01 03:53

I want to build 4 separate apks for 4 different Android CPU processor architectures (armeabi armeabi-v7a x86 mips) using Gradle.

I have native OpenCV libraries buil

4条回答
  •  北海茫月
    2020-12-01 04:09

    The split ABI APK solution for gradle is the simplest I have found so far. @withoutclass has a good writeup here: https://stackoverflow.com/a/26129447/254573 I had to reference the Android documentation since this is a new feature that can still change: http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits

    However, I ended up having to abandon this simple implementation since I needed to support both a fat build and architecture specific builds. You might encounter this same issue if you support both the Google Play store (which supports architecture specific APKs) and the Amazon Appstore (which supports only fat APKs).

    It might be possible to do this with split APKs if you can add a flavor component, but as of now split+flavor is not yet supported: https://code.google.com/p/android/issues/detail?id=76469

    I ended up using the abiFilter, see sample code below:

    android {
        flavorDimensions "abi"
    
        productFlavors {
            fat {
                flavorDimension "abi"
                ndk {
                    abiFilters "x86", "armeabi-v7a", "armeabi"
                    versionCode = 0;
                }
            }
            arm {
                flavorDimension "abi"
                ndk {
                    abiFilter "armeabi"
                    versionCode = 1;
                }
            }
            armv7a {
                flavorDimension "abi"
                ndk {
                    abiFilter "armeabi-v7a"
                    versionCode = 3;
                }
            }
            x86 {
                flavorDimension "abi"
                ndk {
                    abiFilter "x86"
                    versionCode = 6;
                }
            }
        }
    }
    
    // Each APK needs a different version code when submitted to Google,
    // bump the versionCode we set in defaultConfig
    android.applicationVariants.all { variant ->
        // Ugly hard coded flavorDimensions position
        // If you have more than one flavorDimension, make sure to target the position for "abi"
        def abiVersion = variant.productFlavors.get(0).versionCode
    
        variant.mergedFlavor.versionCode = abiVersion * 1000 + android.defaultConfig.versionCode
    }
    

    Update Using universalApk set to true solves this solution, simply adds time to build each apk.

    android {
        // Rest of Gradle file
            splits {
                abi {
                enable true
                reset()
                include 'armeabi', 'armeabi-v7a', 'x86'
                universalApk true
            }
        }
    }
    
    //Ensures architecture specific APKs have a higher version code
    //(otherwise an x86 build would end up using the arm build, which x86 devices can run)
    ext.versionCodes = [armeabi:1, 'armeabi-v7a':3, x86:6]
    
    android.applicationVariants.all { variant ->
        // assign different version code for each output
        variant.outputs.each { output ->
            int abiVersionCode = project.ext.versionCodes.get(output.getFilter(OutputFile.ABI)) ?: 0
            output.versionCodeOverride = (abiVersionCode * 1000) + android.defaultConfig.versionCode
        }
    }
    

提交回复
热议问题