我正在尝试设置一个依赖于使用“.so”文件的库模块的项目 .

到现在为止我有:

库模块

  • .so文件到jniLibs:armeabi,armeabi-v7a和x86 .

  • 这是用于配置的Android.mk文件:

# Copyright (C) 2010 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)

LOCAL_MODULE := my-module
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/my-lib.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
  • 这是build.gradle文件:
apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets.main.jni.srcDirs = [] //disable automatic ndk-build call

    // By default set the publish flavor to
    defaultPublishConfig "X86Debug"
    publishNonDefault true

    productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        arm {
            ndk {
                abiFilter("armeabi")
            }
        }
        arm7 {
            ndk {
                abiFilter("armeabi-v7a")
            }
        }
        x86 {
            ndk {
                abiFilter("x86")
            }
        }
        // This is the fat apk which includes all the ABIs
        fat
    }
}

dependencies {
    testCompile 'junit:junit:4.12'

    compile 'com.j256.ormlite:ormlite-android:4.48'
}

应用程序模块

  • 这是build.gradle:
...

apply plugin: 'com.android.application'

...

android {

    ...

    // In case more than one filter should be applied for the flavors, the order is important
    flavorDimensions "api", "abi"

    productFlavors {
        // Define separate dev and prod product flavors.
        dev {
            dimension "api"
            // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
            // to pre-dex each module and produce an APK that can be tested on
            // Android Lollipop without time consuming dex merging processes.
            minSdkVersion 21
        }

        prod {
            dimension "api"
            // The actual minSdkVersion for the application.
            minSdkVersion Integer.parseInt((String) project.ANDROID_BUILD_MIN_SDK_VERSION)
        }

        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        arm {
            dimension "abi"
            ndk {
                abiFilter("armeabi")
            }
        }
        arm7 {
            dimension "abi"
            ndk {
                abiFilter("armeabi-v7a")
            }
        }
        x86 {
            dimension "abi"
            ndk {
                abiFilter("x86")
            }
        }
    }

    ...

    buildTypes {
        release {
            // Disable all components for debug
            debuggable false
            // Use the updated proguard rules
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            // Obfuscate, clean and optimize project code
            minifyEnabled true
            // Enable zip align for optimizations
            zipAlignEnabled true

            //Only use the release key on a release buildType
            signingConfig signingConfigs.prod
        }

        debug {
            debuggable true

            minifyEnabled false

            zipAlignEnabled false
        }
    }

    ...

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

configurations {
    // x86 versions
    devX86DebugCompile
    prodX86DebugCompile
    devX86ReleaseCompile
    prodX86ReleaseCompile

    // arm versions
    devArmDebugCompile
    prodArmDebugCompile
    devArmReleaseCompile
    prodArmReleaseCompile

    // arm7 versions
    devArm7DebugCompile
    prodArm7DebugCompile
    devArm7ReleaseCompile
    prodArm7ReleaseCompile
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    devX86DebugCompile project(path: ':my-module-lib', configuration: 'x86Debug')
    prodX86DebugCompile project(path: ':my-module-lib', configuration: 'x86Debug')
    devX86ReleaseCompile project(path: ':my-module-lib', configuration: 'x86Release')
    prodX86ReleaseCompile project(path: ':my-module-lib', configuration: 'x86Release')
    devArmDebugCompile project(path: ':my-module-lib', configuration: 'armDebug')
    prodArmDebugCompile project(path: ':my-module-lib', configuration: 'armDebug')
    devArmReleaseCompile project(path: ':my-module-lib', configuration: 'armRelease')
    prodArmReleaseCompile project(path: ':my-module-lib', configuration: 'armRelease')
    devArm7DebugCompile project(path: ':my-module-lib', configuration: 'arm7Debug')
    prodArm7DebugCompile project(path: ':my-module-lib', configuration: 'arm7Debug')
    devArm7ReleaseCompile project(path: ':my-module-lib', configuration: 'arm7Release')
    prodArm7ReleaseCompile project(path: ':my-module-lib', configuration: 'arm7Release')

    ...
}

这似乎目前有效(至少在编译时),但我觉得很难像这样维护它,我将不得不添加很多依赖和配置(最后我想支持所有ABI,使每个人的apk) .

有没有更简单,更清洁的方式来做这样的事情?

此外,在生成的APK中,包括所有ABI . 我为此做错了什么?

LE

我已经修复了包含在APK中的所有ABI的问题 . 我只需要在应用程序模块的productFlavors中使用abiFilter . 我已经更新了我的代码以反映这一点 . 我注意到只有一个小问题:库模块的结果仍然包括所有ABI,即使它已经为每种风格正确设置了abiFilter .