droid_wrap/dalvik/
system.rs

1/*
2 * Copyright (c) 2024. The RigelA open source project team and
3 * its contributors reserve all rights.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and limitations under the License.
12 */
13
14use droid_wrap_derive::{java_class, java_constructor};
15
16use crate::{
17    java::{lang::ClassLoader, nio::ByteBuffer},
18    JObjNew, JObjRef, JType,
19};
20
21/**
22类加载器,用于从包含 classes.dex 条目的 .jar 和 .apk 文件中加载类。这可用于执行未作为应用程序的一部分安装的代码。
23在 API 级别 26 之前,此类加载器需要一个应用程序私有的可写目录来缓存优化的类。使用 Context.getCodeCacheDir() 创建这样的目录:
24File dexOutputDir = context.getCodeCacheDir();
25不要在外部存储上缓存优化的类。外部存储不提供保护应用程序免受代码注入攻击所需的访问控制。
26*/
27#[java_class(name = "dalvik/system/DexClassLoader")]
28pub struct DexClassLoader;
29
30impl DexClassLoader {
31    /**
32    创建 DexClassLoader,用于查找解释代码和本机代码。解释类位于 Jar 或 APK 文件中包含的一组 DEX 文件中。路径列表使用 path.separator 系统属性指定的字符分隔,默认为 :。
33    `dex_path` 包含类和资源的 jar/apk 文件列表,由 File.pathSeparator 分隔,在 Android 上默认为“:”
34    `optimized_directory` 此参数已弃用,自 API 级别 26 起无效。
35    `library_search_path` 包含本机库的目录列表,由 File.pathSeparator 分隔;可能为 null
36    `parent` 父类加载器
37    */
38    #[java_constructor]
39    pub fn new(
40        dex_path: String,
41        optimized_directory: String,
42        library_search_path: String,
43        parent: &ClassLoader,
44    ) -> Self {
45    }
46}
47
48/**
49从包含 DEX 文件的缓冲区加载类的 ClassLoader 实现。这可用于执行尚未写入本地文件系统的代码。
50*/
51#[java_class(name = "dalvik/system/InMemoryDexClassLoader")]
52pub struct InMemoryDexClassLoader;
53
54impl InMemoryDexClassLoader {
55    /**
56    创建一个新的内存中 DEX 类加载器。
57    `dex_buffer` 包含 buffer.position() 和 buffer.limit() 之间的 DEX 文件内容的缓冲区。
58    `parent` 用于委托的父类加载器。
59    */
60    #[java_constructor]
61    pub fn new(dex_buffer: &ByteBuffer, parent: &ClassLoader) -> Self {}
62}
63
64/// 测试dalvik.system
65#[cfg(feature = "test_dalvik_system")]
66pub fn test() {
67    use crate::android::{app::Activity, content::Context};
68    let context: Context = (&Activity::fetch()).into();
69    let loader = DexClassLoader::new(
70        "c.dex".to_string(),
71        "".to_string(),
72        "".to_string(),
73        &context.get_class_loader(),
74    );
75    assert!(loader
76        .to_string()
77        .starts_with("dalvik.system.DexClassLoader"));
78}