首页 > 解决方案 > 为什么外部共享库没有链接到我的 APK?

问题描述

我有一个带有 CMake 文件的小型 Android 项目,该文件可以引入外部本机 .so 文件。它使用了我用于其他项目的相同技术,并且这些项目没有问题。

在这种情况下,“library.so”文件按预期复制到“cmake/debug”目录中,但没有链接到实际的 APK。是否有一些隐藏设置必须设置才能与 APK 链接?

这是cmake文件。

cmake_minimum_required(VERSION 3.4.1)
add_library( native-lib SHARED ./native-lib.cpp )
find_library( log-lib log )
 
add_library( libMyLib SHARED IMPORTED )
set ( MYLIB_SO_DIR C:/MYLIB/ARM64/ )
set_target_properties( libMyLib PROPERTIES IMPORTED_LOCATION ${MYLIB_SO_DIR}/libMyLib.so )
target_link_libraries( native-lib libMyLib ${log-lib} )

这是 Android 清单。

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.PackagingOnlyProject">
 
<application
    android:label="@string/app_name"
    android:extractNativeLibs="true">
 
  <activity android:name="android.app.NativeActivity"
      android:label="@string/app_name">
 
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
 
    <meta-data android:name="android.app.lib_name"
        android:value="libMyLib" />
   </activity>
</application>
</manifest>

这是 build.gradle。

plugins {
    id 'com.android.application'
}
 
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"
    android.ndkPath "C:/Development/Android/NDK"
 
    defaultConfig {
        applicationId "com.PackagingOnlyProject"
        minSdkVersion 27
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        ndk.abiFilters 'arm64-v8a'
 
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++14"
            }
        }
    }
 
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
 
        debug {
            debuggable true
            jniDebuggable true
            minifyEnabled false
            renderscriptDebuggable true
        }
    }
 
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
 
    packagingOptions{
        doNotStrip "*/arm64-v8a/*.so"
    }
 
    sourceSets {
        main {
            jniLibs.srcDirs += 'native-lib/src'
        }
    }
 
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
}
 
dependencies {
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
}

标签: android-studioandroid-ndk

解决方案


推荐阅读