我想知道如何构建我的 APK,以便我的库安装/复制到/data/data/<package name>/lib
安装 APK 时。
-
我使用的是Android Studio 4.0.1
-
我构建了一个 APK,并在提取它时确认我有一个lib/arm64-v8a
包含我的 jni 库的文件夹。
-
我希望在安装 APK 后,同一设备上的其他一些应用程序可以访问我在lib/arm64-v8a
. The /data/data/<package name>
文件夹似乎是正确的位置,因此我的问题;如果有另一种方法可以让我的库在设备上访问,我对此持开放态度。
-
I set android.bundle.enableUncompressedNativeLibs=false
in my gradle.properties
文件基于这次讨论 https://github.com/google/bundletool/issues/39甚至
尽管我没有看到将它与 true 或 false 一起使用有任何效果。
-
I set android:extractNativeLibs="true"
在我的清单中
但是,当我 adb 安装我的 apk 时,/data/data/<package name>/
我的设备上的文件夹仅包含cache
and code_cache
文件夹,两者都是空的。
我确实看到了/data/app/
包含我的库的文件夹,但它们包含每次安装 APK 时随机设置的字符串:
/data/app/~~sFE8-eNknFAkhKJ-1S03lg==/<package name>-rsDLNgpi4vult7yVBrPNOQ==/
base.apk
lib/arm64/
<my .so in there>
这些随机字符串阻止我从任何其他应用程序可靠地访问我的库。 (如果有办法不将这些随机字符串作为名称的一部分并且/data/app
是我的库与其他应用程序共享的推荐位置,我对这种替代方法感兴趣。)
我还安装了其他预构建的 APK,通过这些,我确实看到了下面的库/data/data/<package name>/lib
就像我想要的那样。对我来说,这表明我的问题与我如何使用 AS 4.0.1 构建 APK 有关,而不是与我的设备、其 Android 版本或如何安装 APK 有关。
这是我的 gradle 文件:
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。