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

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

问题描述

我想知道如何构建APK,以便在安装APK时在/data/data/<程序包名称>/lib 下安装/复制我的库.

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.

  • 我正在使用Android Studio 4.0.1

  • I am using Android Studio 4.0.1

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

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

我希望在安装APK后,同一设备上的其他一些应用程序可以访问 lib/arm64-v8a 中的库./data/data/< package name> 文件夹似乎是执行此操作的正确位置,因此,我的问题是:如果还有另一种方法可以使我的图书馆在设备上访问,则我愿意接受它.

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.

我设置了 android.bundle.enableUncompressedNativeLibs = false 在我基于此讨论 gradle.properties 文件中尽管使用true或false都看不到效果.

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.

我在清单中设置了 android:extractNativeLibs ="true"

但是,当我adb安装我的apk时,设备上的/data/data/< package name>/文件夹仅包含 cache code_cache 文件夹,两者都为空.

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.

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

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>

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

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.)

我还安装了其他预构建的APK,并随心所欲地看到了/data/data/< package name>/lib 下的库.对我来说,这表明我的问题与我如何使用AS 4.0.1构建APK无关,而与设备,其Android版本或安装APK的方式有关.

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.

这是我的gradle文件:

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'

}

我尝试过的其他事情:我手动创建了一个src/main/jniLibs.arm64-v8a文件夹,在其中放置了一个.so.当我解压缩该.so时,它会出现在我的apk中,但是我的/data/data/< package name>/文件夹仍然没有lib.

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.

推荐答案

我认为您需要添加此代码以将libs添加到适当的文件夹中,例如/data/data/< package name>/lib

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

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

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

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

将此文件保存到 CMakeList.txt 文件

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

按照 developer.android.com

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

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天全站免登陆