安装APK时缺少lib文件夹 [英] lib folder missing when installing APK

查看:33
本文介绍了安装APK时缺少lib文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何构建我的 APK,以便在安装 APK 时将我的库安装/复制到 /data/data//lib 下.

  • 我使用的是 Android Studio 4.0.1

  • 我构建了一个 APK 并在我提取它时确认我有一个lib/arm64-v8a 包含我的 jni 库的文件夹.

  • 我希望在我安装 APK 之后,同一设备上的其他一些应用程序可以访问我在 lib/arm64-v8a 中的库./data/data/ 文件夹似乎是这个的正确位置,因此我的问题;如果有另一种方法可以让我的图书馆在设备上访问,我愿意接受.

  • 我设置了 android.bundle.enableUncompressedNativeLibs=false在我基于这个讨论gradle.properties文件中,甚至虽然我没有看到使用 true 或 false 有任何效果.

  • 我在 Manifest 中设置了 android:extractNativeLibs="true"

然而,当我 adb 安装我的 apk 时,我设备上的 /data/data// 文件夹只包含一个 cachecode_cache 文件夹,两个都是空的.

我确实在 /data/app/ 下看到了一个包含我的库的文件夹,但它们包含每次安装 APK 时随机设置的字符串:

/data/app/~~sFE8-eNknFAkhKJ-1S03lg==/<包名>-rsDLNgpi4vult7yVBrPNOQ==/基础.apk库/arm64/<我的.so在那里>

这些随机字符串阻止我从任何其他应用程序可靠地访问我的库.(如果有办法不将这些随机字符串作为名称的一部分,并且 /data/app 是我的库与其他应用程序共享的推荐位置,我对这种替代方法很感兴趣.)

我还安装了其他预构建的 APK,通过这些,我确实在 /data/data//lib 下看到了我想要的库.这对我来说表明我的问题与我如何使用 AS 4.0.1 构建 APK 相关,而不是与我的设备、其 Android 版本或我如何安装 APK.

这是我的 gradle 文件:

import java.util.regex.Matcher导入 java.util.regex.Pattern应用插件:'com.android.application'安卓 {编译SDK版本30buildToolsVersion "30.0.2";lintOptions {checkReleaseBuilds falseabortOnError false}sourceSets.main {jniLibs.srcDirs = ['libs']jni.srcDirs = []}默认配置{applicationIdcom.example.myexample"minSdk 版本 16目标SDK版本30版本代码 1版本名称1.0"testInstrumentationRunnerandroidx.test.runner.AndroidJUnitRunner"外部原生构建{制作{版本3.17.0"cppFlags ""参数-DANDROID_STL=c++_shared";}}ndk{模块名称myexample"abiFilterarm64-v8a"ldLibs日志"}包装选项{排除lib/arm64-v8a/libassets.so";}}构建类型{释放 {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}外部原生构建{制作{版本3.17.0"路径src/main/cpp/CMakeLists.txt";//每个项目只能有一个 CMAKE:https://developer.android.com/studio/projects/gradle-external-native-builds}}}任务复制文件(类型:复制){description = '测试手动复制操作(确认有效)...'来自文件(../dsp/external/ship/libmyexample_skel.so")进入文件($buildDir/res/raw/")}项目.afterEvaluate {preBuild.dependsOn copyFiles}依赖{实现文件树(目录:libs",包括:[*.jar"])实现 'androidx.appcompat:appcompat:1.2.0'实现 'androidx.constraintlayout:constraintlayout:2.0.1'testImplementation 'junit:junit:4.12'androidTestImplementation 'androidx.test.ext:junit:1.1.2'androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'}

我尝试过的其他方法:我手动创建了一个 src/main/jniLibs.arm64-v8a 文件夹,在其中放置了一个 .so.当我提取它时,那个 .so 出现在我的 apk 中,但是我的 /data/data// 文件夹仍然没有 lib.

解决方案

我认为你需要添加这个代码来在适当的文件夹中添加 libs 像 /data/data//lib

将此添加到应用的 build.gradle 文件到 android{} 标记

sourceSets.main {jniLibs.srcDirs = ['libs']jni.srcDirs = []}

这个到CMakeList.txt文件

cmake_minimum_required(VERSION 3.4.1)find_library( # 设置路径变量的名称.日志库日志)设置(distribution_DIR ${CMAKE_SOURCE_DIR}/libs)add_library(你的LibName共享...//jni 文件夹中的 c/c++ 文件列表)target_link_libraries(yourLibName ${log-lib})

根据 developer.android.com<块引用>

注意:更改 Gradle 配置时,请确保通过单击工具栏中的同步项目来应用您的更改.此外,在更改 CMake 或 ndk-build 脚本时在您已经将文件链接到 Gradle 之后,您应该同步通过选择 Build > 更改 Android Studio刷新链接菜单栏中的 C++ 项目.

I would like to know how to build my APK so that my libraries are installed/copied under /data/data/<package name>/lib when installing the APK.

  • I am using Android Studio 4.0.1

  • I build an APK and confirm when I extract it that I have a lib/arm64-v8a folder that contains my jni libs.

  • I want that after I install my APK, some other applications on the same device can access the libs that I have in the lib/arm64-v8a. The /data/data/<package name> folder seems to be the right place for this, hence my question; if there is another approach for making my libraries accessible on device, I am open to it.

  • I set android.bundle.enableUncompressedNativeLibs=false in my gradle.properties file based on this discussion even though I didn't see any effect using it with true or false.

  • I set android:extractNativeLibs="true" in my Manifest

However, when I adb install my apk, the /data/data/<package name>/ folder on my device only contains a cache and code_cache folder, both of which are empty.

I do see under /data/app/ a folder with my libraries but they include strings that are randomly set each time I install the APK:

/data/app/~~sFE8-eNknFAkhKJ-1S03lg==/<package name>-rsDLNgpi4vult7yVBrPNOQ==/
   base.apk
   lib/arm64/
       <my .so in there>

These random strings prevent me from accessing reliably my libraries from any other application. (If there is a way to not have these random strings as part of the name and /data/app is the recommended location for my libraries to be shared with other applications, I am interested in that alternate approach.)

I have also installed other prebuilt APK and with these, I do see the libraries under /data/data/<package name>/lib like I want. This to me indicates that my issue is related to how I build my APK with AS 4.0.1, not to my device, its Android version, or how I install the APK.

Here is my gradle file:

import java.util.regex.Matcher
import java.util.regex.Pattern

apply plugin: 'com.android.application'

android {


    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    sourceSets.main {
        jniLibs.srcDirs = ['libs']
        jni.srcDirs = []
    }

    defaultConfig {
        applicationId "com.example.myexample"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                version "3.17.0"
                cppFlags ""
                arguments "-DANDROID_STL=c++_shared"
            }
        }
        ndk {
            moduleName "myexample"
            abiFilter "arm64-v8a"
            ldLibs "log"
        }
        packagingOptions {
            exclude "lib/arm64-v8a/libassets.so"
        }

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            version "3.17.0"
            path "src/main/cpp/CMakeLists.txt"
            // There can be only one CMAKE per project: https://developer.android.com/studio/projects/gradle-external-native-builds
        }
    }
}


task copyFiles(type: Copy) {
    description = 'Testing a manual copy operation (confirmed it works)...'
    from file("../dsp/external/ship/libmyexample_skel.so")
    into file("$buildDir/res/raw/")
}

project.afterEvaluate {
    preBuild.dependsOn copyFiles
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

Something else I tried: I created manually a src/main/jniLibs.arm64-v8a folder in which I place a .so. That .so appears in the my apk when I extract it, but my /data/data/<package name>/ folder doesn't have a lib nevertheless.

解决方案

I think you need to add this code to add libs in the appropriate folder like /data/data/<package name>/lib

add this to app's build.gradle file into android{} tag

sourceSets.main {
    jniLibs.srcDirs = ['libs']
    jni.srcDirs = []
}

this to CMakeList.txt file

cmake_minimum_required(VERSION 3.4.1)


find_library( # Sets the name of the path variable.
        log-lib
        log)

set(distribution_DIR ${CMAKE_SOURCE_DIR}/libs)


add_library(yourLibName
        SHARED
        ...//list of your c/c++ files here from jni folder
)

target_link_libraries(yourLibName ${log-lib})

as per developer.android.com

Note: When making changes to the Gradle configuration, make sure to apply your changes by clicking Sync Project in the toolbar. Additionally, when making changes to your CMake or ndk-build script file after you have already linked it to Gradle, you should sync Android Studio with your changes by selecting Build > Refresh Linked C++ Projects from the menu bar.

这篇关于安装APK时缺少lib文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆