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 crate::{
15    JObjNew, JObjRef, JType,
16    java::{lang::ClassLoader, nio::ByteBuffer},
17    java_class, java_constructor,
18};
19
20/**
21类加载器,用于从包含 classes.dex 条目的 .jar 和 .apk 文件中加载类。这可用于执行未作为应用程序的一部分安装的代码。
22在 API 级别 26 之前,此类加载器需要一个应用程序私有的可写目录来缓存优化的类。使用 Context.getCodeCacheDir() 创建这样的目录:
23File dexOutputDir = context.getCodeCacheDir();
24不要在外部存储上缓存优化的类。外部存储不提供保护应用程序免受代码注入攻击所需的访问控制。
25*/
26#[java_class(name = "dalvik/system/DexClassLoader")]
27pub struct DexClassLoader;
28
29impl DexClassLoader {
30    /**
31    创建 DexClassLoader,用于查找解释代码和本机代码。解释类位于 Jar 或 APK 文件中包含的一组 DEX 文件中。路径列表使用 path.separator 系统属性指定的字符分隔,默认为 :。
32    `dex_path` 包含类和资源的 jar/apk 文件列表,由 File.pathSeparator 分隔,在 Android 上默认为“:”
33    `optimized_directory` 此参数已弃用,自 API 级别 26 起无效。
34    `library_search_path` 包含本机库的目录列表,由 File.pathSeparator 分隔;可能为 null
35    `parent` 父类加载器
36    */
37    #[java_constructor]
38    pub fn new(
39        dex_path: String,
40        optimized_directory: String,
41        library_search_path: String,
42        parent: &ClassLoader,
43    ) -> Self {
44    }
45}
46
47/**
48从包含 DEX 文件的缓冲区加载类的 ClassLoader 实现。这可用于执行尚未写入本地文件系统的代码。
49*/
50#[java_class(name = "dalvik/system/InMemoryDexClassLoader")]
51pub struct InMemoryDexClassLoader;
52
53impl InMemoryDexClassLoader {
54    /**
55    创建一个新的内存中 DEX 类加载器。
56    `dex_buffer` 包含 buffer.position() 和 buffer.limit() 之间的 DEX 文件内容的缓冲区。
57    `parent` 用于委托的父类加载器。
58    */
59    #[java_constructor]
60    pub fn new(dex_buffer: &ByteBuffer, parent: &ClassLoader) -> Self {}
61}
62
63/// 测试dalvik.system
64#[cfg(feature = "test_dalvik_system")]
65pub fn test() {
66    use crate::android::app::Activity;
67    let context = Activity::fetch().unwrap();
68    let loader = DexClassLoader::new(
69        "c.dex".to_string(),
70        "".to_string(),
71        "".to_string(),
72        &context.get_class_loader(),
73    );
74    assert!(
75        loader
76            .to_string()
77            .starts_with("dalvik.system.DexClassLoader")
78    );
79}