首页 > 解决方案 > jni 库未包含在调试版本中

问题描述

Android 调试版本缺少 jniLibs 文件夹中的 .so 文件。

下面给出的项目级 build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用级别 build.gradle 给出如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.test.mobiocean"
        minSdkVersion 22
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs 'src/main/jniLibs'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

项目文件结构

但是,如果我执行 Build -> Rebuild Project,生成的 apk 具有所需的 .so 文件。但是当我连接到设备并运行应用程序时,apk 不包括所需的 .so 文件。

我检查了 build/intermediates/merged_jni_libs 文件夹,它包含所有情况下所需的文件。

我正在使用 Android Studio 3.5 并使用 Android Gradle Plugin 3.5.0 和 Gradle 版本 5.4.1

标签: androidandroid-studioandroid-gradle-plugin

解决方案


将架构变体显式添加到应用程序等级文件也有助于:

  buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ndk {
                abiFilters "armeabi"
            }
        }
        debug {
            ndk {
                abiFilters "armeabi"
            }
        }
    }

推荐阅读