jni 库未包含在调试版本中

jni library not getting included in debug build

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'
}

但是,如果我执行构建 -> 重建项目,生成的 apk 具有所需的 .so 文件。但是当我连接到设备和 运行 应用程序时,apk 不包括所需的 .so 文件。

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

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

只是一个解决方法:

  1. 运行 "Build" 在 AS
  2. 在您的设备上安装 apk 并运行它
  3. 将调试器附加到进程

我认为是AS的一个bug。我遇到了同样的问题:"Build" 或 运行 "gradlew :app:assembleDebug" 有效,而 运行 调试无效。如果 Gradle 插件的 Gradle 有错误,那么 "Build" 或 "gradlew :app:assembleDebug" 应该也会失败。

将架构变体明确添加到应用程序等级文件也有帮助:

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