Myeeygo's blog Myeeygo's blog
首页
技术
关于
  • 分类
  • 标签
  • 归档

Myeeygo

个人技术博客
首页
技术
关于
  • 分类
  • 标签
  • 归档
  • GitHub高级搜索技巧
  • GithubPage配置
  • kali配置clash
  • Vmware移动kali文件后无法正常进入系统
  • 对git设置代理
  • AndroidStudio模拟器无法启动问题
  • win11更新23H2失败问题
  • win11固定应用到开始菜单
  • Excel相关问题
  • Virtualbox虚拟机扩容
  • Virtualbox虚拟机使用主机VPN
  • vscode选择虚拟环境python解释器
  • 云服务器进行rdp远程桌面连接
  • 同一局域网的服务器使用本地VPN代理
  • 将jar转换为dex
  • 导出AndroidStudio中的apk依赖jar包
    • 概述
    • 处理方法
  • 解决在conda中未使用对应python版本
  • 云服务器ssh连接排查记录
  • 使用qq联系openclaw掉线问题
  • python的subprocess命令运行问题
  • 本地电脑访问云服务器自建网站
  • 中文路径pip安装报错
  • ssh连接权限问题设置
  • git大文件上传问题
  • 博客网站云服务器配置流程
  • 远程云服务器使用本地代理
  • 技术
myeeygo
2026-01-28
目录

导出AndroidStudio中的apk依赖jar包

# 概述

进行某些实验时,需要在AndroidStudio的apk项目中,导出其依赖的jar包。

# 处理方法

说明,将以下代码复制到AndroidStudio的 app/build.gradle 文件中,之后 sync now。

afterEvaluate {
    // 核心任务:提取JAR(含AAR内的JAR)到指定目录
    task extractAarJars(type: DefaultTask) {
        def outputDir = file("$rootDir/dependencies/jars")
        def processedDeps = [] as Set  // 已处理的依赖坐标
        def allFiles = [] as Set       // 已生成的文件名(保证唯一)

        doLast {
            // 清空并重建输出目录
            outputDir.deleteDir()
            outputDir.mkdirs()
    
            // 1. 查找可用的依赖配置(优先releaseRuntimeClasspath → runtimeClasspath → compileClasspath)
            def configs = [
                    configurations.findByName("releaseRuntimeClasspath"),
                    configurations.findByName("runtimeClasspath"),
                    configurations.findByName("compileClasspath")
            ].findAll() ?: configurations.findAll { !it.name.toLowerCase().contains("test") }
    
            // 2. 提取通用方法:生成唯一文件名
            def getUniqueFileName = { baseName ->
                def fileName = baseName
                int counter = 1
                while (allFiles.contains(fileName)) {
                    fileName = "${baseName}-${counter++}"
                }
                allFiles.add(fileName)
                fileName
            }
    
            // 3. 提取通用方法:复制文件到输出目录
            def copyToOutput = { sourceFile, newFileName ->
                copy {
                    from sourceFile
                    into outputDir
                    rename { newFileName }
                }
            }
    
            // 4. 遍历配置处理依赖
            configs.each { config ->
                try {
                    config.resolvedConfiguration.resolvedArtifacts.each { artifact ->
                        def compId = artifact.id.componentIdentifier
                        // 跳过项目依赖和非模块依赖
                        if (!(compId instanceof org.gradle.api.artifacts.component.ModuleComponentIdentifier)) {
                            println "[Skip] Unsupported dependency type: ${compId.class.simpleName}"
                            return
                        }
    
                        // 解析依赖坐标
                        def moduleId = compId
                        def group = moduleId.group ?: ""
                        def name = moduleId.module ?: ""
                        def version = moduleId.version ?: ""
                        def depCoords = "${group}:${name}:${version}"
    
                        // 跳过空字段或已处理的依赖
                        if (group.isEmpty() || name.isEmpty() || version.isEmpty() || processedDeps.contains(depCoords)) {
                            return
                        }
                        processedDeps.add(depCoords)
    
                        // 处理JAR/AAR文件
                        try {
                            def artifactFile = artifact.file
                            if (artifactFile.name.endsWith(".jar")) {
                                // 处理原生JAR
                                def baseName = "${group.replace('.', '-')}-${name}-${version}.jar"
                                def uniqueName = getUniqueFileName(baseName)
                                println "[Prepare] Copy JAR: ${artifactFile.name} --> ${uniqueName}"
                                copyToOutput(artifactFile, uniqueName)
    
                            } else if (artifactFile.name.endsWith(".aar")) {
                                // 处理AAR(解压提取内部JAR)
                                println "[Prepare] Unpack AAR: ${artifactFile.name} (${depCoords})"
                                def tempDir = file("$buildDir/tmp/aarUnpack/${name}-${version}")
                                // 解压AAR → 提取JAR → 清理临时目录
                                try {
                                    copy { from zipTree(artifactFile); into tempDir }
                                    fileTree(tempDir).include("**/*.jar").each { aarJar ->
                                        // 新增:判断是否为lint.jar,若是则跳过复制
                                        if (aarJar.name == "lint.jar") {
                                            println "    --> Skip lint.jar: Won't copy! "
                                            return // Groovy中跳出当前each循环迭代(等效于Java的continue)
                                        }
    
                                        def baseName = "${group.replace('.', '-')}-${name}-${version}-${aarJar.name}"
                                        def uniqueName = getUniqueFileName(baseName)
                                        println "    --> Extract JAR: ${aarJar.name} -->${uniqueName}"
                                        copyToOutput(aarJar, uniqueName)
                                    }
                                } finally {
                                    tempDir.deleteDir()
                                }
                            }
                        } catch (Exception e) {
                            println "[Error] Failed to process ${depCoords}: ${e.message}"
                        }
                    }
                } catch (Exception e) {
                    println "[Error] Failed to process config ${config.name}: ${e.message}"
                }
            }
    
            // 输出最终统计
            def jarCount = outputDir.listFiles()?.count { it.name.endsWith(".jar") } ?: 0
            println "\n" + "=" * 50
            println "Extraction Result: ${jarCount} JAR(s) exported to ${outputDir.absolutePath}"
            println "=" * 50
            if (jarCount == 0) println "WARNING: No JAR files extracted (check dependencies/configurations)"
        }
    }
    
    // 一键执行任务(先clean再提取)
    task downloadJars {
        dependsOn clean, extractAarJars
        extractAarJars.mustRunAfter clean
        doLast { println " All JAR files extracted successfully!" }
    }
    
    // 任务描述
    extractAarJars.description = "Extracts JAR files from dependencies (raw JARs and AAR-contained JARs)"
    downloadJars.description = "Cleans and extracts all dependency JAR files"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

之后打开IDE的终端,输入以下命令

gradle.bat  app:downloadJars 
1

之后在 dependencies/jars 文件夹下面就有导出的jar包了。

将jar转换为dex
解决在conda中未使用对应python版本

← 将jar转换为dex 解决在conda中未使用对应python版本→

最近更新
01
远程云服务器使用本地代理
05-19
02
博客网站云服务器配置流程
05-19
03
git大文件上传问题
05-19
更多文章>
Theme by Vdoing | Copyright © 2025-2026 Myeeygo | 鲁ICP备2026016472号-3 | 鲁公网安备37028202001294号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式