使用ABI拆分启用externalNativeBuild

时间:2016-10-24 09:38:30

标签: android android-studio android-ndk android-gradle build.gradle

我已从cmake切换到ndk-build构建系统。但无法获得APK拆分功能。

这是我的应用build.gradle文件

构建的结果只有一个文件 - foo.bar-armeabi-v7a-release-1.5.1.apk 如果我正在为x86模拟器构建应用程序,则结果为foo.bar-x86-release-1.5.1.apk

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileOptions.encoding = 'ISO-8859-1'
    compileSdkVersion CompiledSdkVersion
    buildToolsVersion BuildToolsVersion

    defaultConfig {
        applicationId "foo.bar"
        minSdkVersion MinSdkVersion
        targetSdkVersion TargetSdkVersion
        versionCode VersionCode
        versionName VersionName
        vectorDrawables.useSupportLibrary = true

        externalNativeBuild {
            cmake {
                cppFlags "-fexceptions", "-std=c++11"
            }
        }

        ndk {
            abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
        }
    }

    signingConfigs {
        release {
            storeFile file("..\\release.keystore")
        }
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
        }
    }

    splits {
        abi {
            enable true
            reset()
            include "armeabi", "armeabi-v7a", "arm64-v8a", "mips", "mips64", "x86", "x86_64"
            universalApk true
        }
    }

    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.FilterType.ABI), 0) * 1000000 + android.defaultConfig.versionCode
            output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
        }
    }

    externalNativeBuild {
        cmake {
            path 'src/main/jni/CMakeLists.txt'
        }
    }
}

crashlytics {
    enableNdk false // too many reports for third-party modules
    androidNdkOut '.externalNativeBuild/cmake/release'
    androidNdkLibsOut 'src/main/libs'
}

dependencies {
    compile project(':common-sources')
    compile project(':chess-board-library')
    compile project(':number-picker')

    compile files('libs/kxml2-2.3.0.jar')
    compile files('libs/StartADLib-1.0.1.jar')

    compile Dependencies.appCompat
    compile Dependencies.cardView
    compile Dependencies.firebaseAds
    compile Dependencies.googleAnalytics
    compile Dependencies.googlePlus
    compile Dependencies.googleGames

    compile(Dependencies.crashlytics) {
        transitive = true
    }
    compile(Dependencies.crashlyticsNdk) {
        transitive = true
    }
}

apply plugin: 'com.google.gms.google-services'

P.S。如果您可以重现错误,请投票my issue in Google's bug tracker,但无法找到解决方案。

2 个答案:

答案 0 :(得分:2)

看起来这是构建工具中的一个错误。即使使用旧的ndk-build,ABI Split也不起作用。

在研究期间,我找到了一个很好的解决方法 - 从命令行启动构建。

gradlew assembleDebug

gradlew assembleRelease

需要从项目的根目录启动命令。

非常感谢this sudden comment:)

答案 1 :(得分:1)

看起来您的问题出在您的variants.outputs.each代码中。首先,getFilter只接受一个参数。 (参见Output class实施。)

请参阅this post,了解为何不使用com.android.build.OutputFile.ABI(请改用OutputFile.FilterType.ABI)。此问题导致该海报拆分APK的类似失败。