gpt4 book ai didi

android - 将文件结构表示为 JSON

转载 作者:行者123 更新时间:2023-12-02 13:29:45 24 4
gpt4 key购买 nike

我想用 JSON 或 XML 代码表示当前文件结构,但有一些条件。

  • 我希望我的代表不要提及中间的空目录。
  • 我只想列出以 Activity.* 结尾的文件

  • 例如:
    对于这个结构
    my_project
    ├── feature1
    │ └── src
    │ └── java
    │ └── com
    │ └── directory
    │ ├── MainActivity.kt
    │ └── MainFragment.kt
    └── feature2
    └── src
    └── java
    └── com
    └── directory
    ├── Feature2Activity.kt
    └── Feature2Fragment.kt

    应该生成
    {
    "my_project": {
    "feature1": {
    "directory": ["MainActivity"]
    },
    "feature2": {
    "directory": ["Feature2Activity"]
    }
    }
    }

    如果我不能在运行时执行此操作也没关系,将其视为我将在目录上使用的脚本。不在应用程序运行时。

    最佳答案

    源文件编译成JVM字节码,然后编译成可以在Android平台上运行的Dex(Dalvik Executable)。

    您可以使用上下文可以访问的应用程序的 Dex 文件。

    val activityFiles = mutableListOf<String>()

    DexFile(context.getPackageCodePath()).entries().apply {
    while(hasMoreElement()) {
    val fileName = nextElement.substringAfterLast('.').substringBefore('$')
    if(fileName.contains("Activity")) activityFiles.add(fileName)
    }
    }

    // Logic to generate Json. You may use any json library.
    val innerJson = mutableMapOf(
    getAppName() to mapOf(
    "directory" to activityFiles
    )
    )

    // Hard-code the parent project name (holding both sub application)
    val jsonRepresentation: MutableMap<String, Any> = mutableMapOf("my_project" to innerJson)

    val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()

    val json = moshi.adapter(Map::class.java).toJson(jsonRepresentation)


    fun getAppName(ctx: Context) {
    val appInfo = context.getApplicationInfo()
    val id = appInfo.labelRes
    return if (id == 0) applicationInfo.nonLocalizedLabel.toString()
    else context.getString(id)
    }

    我想知道是否有可能同时拥有 feature1 信息和 feature2,因为您将拥有该指定应用程序的上下文。如果您在 feature1 中编写代码然后将无法看到另一个 feature2 的类/应用程序名称.

    引用
  • https://developer.android.com/reference/kotlin/dalvik/system/DexFile#entries()
  • Is there a way to get a list of all classes from a .dex file?
  • Find all classes in a package in Android
  • Android - How to get application name? (Not package name)
  • 关于android - 将文件结构表示为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62124218/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com