Gradle android 为不同的处理器架构构建

Gradle android build for different processor architectures(Gradle android 为不同的处理器架构构建)
本文介绍了Gradle android 为不同的处理器架构构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想使用 Gradle 为 4 个不同的 Android CPU 处理器架构(armeabi armeabi-v7a x86 mips)构建 4 个单独的 apk.

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

我在 libs 文件夹中有为 4 个 CPU 架构构建的原生 OpenCV 库.

I have native OpenCV libraries built for 4 CPU architectures in the libs folder.

libs
    -armeabi
    -armeabi-v7a
    -x86
    -mips

我希望每个 apk 只包含对应正确 CPU 架构的 OpenCV 库.

I want to each apk only contains the OpenCV library corresponding to the correct CPU architecture.

当前构建脚本如下:

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':workspace:OpenCV4Android:sdk:java')
}

android {
    compileSdkVersion 11
    buildToolsVersion "18.1.0"

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

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')

        flavorGroups "abi", "version"
        productFlavors {
            x86 {
                flavorGroup "abi"
            }
            arm {
                flavorGroup "abi"
            }
            mips {
                flavorGroup "abi"
            }
        }

    }
}

有人可以帮我解决这个问题吗?

Can someone help me to resolve this please?

干杯,

推荐答案

从 Android Gradle 插件版本 13 开始,您现在可以使用新的拆分"机制生成单独的 APK.你可以阅读它这里.

As of Android Gradle Plugin version 13 you can now generate seperate APK's using the new "split" mechanism. You can read about it here.

放置 .so 文件的默认文件结构是:

The default file structure for placing your .so files is:

src
-main
  -jniLibs
    -armeabi
      -arm.so
    -armeabi-v7a
      -armv7.so
    -x86
      -x86.so
    -mips
      -mips.so

请注意,.so 文件的名称并不重要,只要它具有 .so 扩展名.

Note that the name of the .so file is unimportant as long as it has the .so extension.

然后在你的 Gradle 构建文件中:

Then in your Gradle build file:

android {
...
splits {
abi {
  enable true
  reset()
  include 'x86', 'armeabi-v7a', 'mips', 'armeabi'
  universalApk false
  }
 }
}

// map for the version code
ext.versionCodes = ['armeabi-v7a':1, mips:2, x86:3]

import com.android.build.OutputFile

android.applicationVariants.all { variant ->
    // assign different version code for each output
    variant.outputs.each { output ->
        output.versionCodeOverride =
            project.ext.versionCodes.get(output.getFilter(OutputFile.ABI)) * 1000000 + android.defaultConfig.versionCode
    }
}

请注意,上面 ext.versionCodes 中的版本代码在很大程度上是无关紧要的,这是为每个 ABI 类型添加唯一的偏移量,因此版本代码不会冲突.

Note that the version codes above in ext.versionCodes are largely irrelevant, this is here to add a unique offset for each ABI type so version codes do not clash.

这篇关于Gradle android 为不同的处理器架构构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)