Android Studio Gradle 重命名输出App或者Library的文件名称、更改输出目录

本文详细介绍了如何使用Gradle配置来定制Android应用和库的输出文件路径及命名,包括两种方法:通过assemble任务完成后复制文件并重命名,以及直接修改output.outputFile属性。

from: http://laole918.com/2016/07/12/gradle-output/

更改app的outputFile

  • 方法一:编译完成之后,copy到指定目录,同时重命名

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    android {
    	{...}
    	applicationVariants.all { variant ->
            if (variant.buildType.name == 'release') {
                variant.assemble.doLast {
                    variant.outputs.each { output ->
                        def outputFile = output.outputFile
                        if (outputFile != null && outputFile.name.endsWith('release.apk')) {
                            def fileName = "${project.name}${variant.flavorName}_${defaultConfig.versionName}_${releaseTime()}"
                            def outputPath = "../output/apk";
                            copy {
                                from outputFile
                                into outputPath
                                rename { fileName + ".apk" }
                            }
                        }
                    }
                }
            }
        }
        {...}
    }
    
  • 方法二:直接改变output.outputFile的目录并重命名
    注意:没有 doLast{}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    android {
    	{...}
        applicationVariants.all { variant ->
            if (variant.buildType.name == 'release') {
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('release.apk')) {
                        def fileName = "${project.name}${variant.flavorName}_${defaultConfig.versionName}_${releaseTime()}"
                        def outputPath = "../output/apk";
                        output.outputFile = new File(outputPath, fileName + ".apk")
                    }
                }
            }
        }
        {...}
    }
    

更改library的outputFile

  • 方法一:编译完成之后,copy到指定目录,同时重命名

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    android{
    	    libraryVariants.all { variant ->
            if (variant.buildType.name == 'release') {
                variant.assemble.doLast {
                    variant.outputs.each { output ->
                        def outputFile = output.outputFile
                        if (outputFile != null && outputFile.name.endsWith('release.aar')) {
                            def fileName = "${project.name}${variant.flavorName}_${android.defaultConfig.versionName}_${releaseTime()}"
                            def outputPath = "../output/aar"
                            copy {
                                from outputFile
                                into outputPath
                                rename { fileName + ".aar" }
                            }
                        }
                    }
                }
            }
        }
    }
    
  • 方法二:直接改变output.outputFile的目录并重命名

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    android {
    	libraryVariants.all { variant ->
            if (variant.buildType.name == 'release') {
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('release.aar')) {
                        def fileName = "${project.name}${variant.flavorName}_${android.defaultConfig.versionName}_${releaseTime()}"
                        def outputPath = "../output/aar"
                        output.outputFile = new File(outputPath, fileName + ".aar")
                    }
                }
            }
        }
    }
    
  • 方法三:如果想要最终打出jar,兼容eclipse。

    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
    
    android {
    	{...}
        libraryVariants.all { variant ->
            if (variant.buildType.name == 'release') {
                variant.assemble.doLast {
                    variant.outputs.each { output ->
                        def outputFile = output.outputFile
                        if (outputFile != null && outputFile.name.endsWith('release.aar')) {
                            def fileName = "${project.name}${variant.flavorName}_${android.defaultConfig.versionName}_${releaseTime()}"
                            def outputPath = "../output/jar"
                            def flavorBundlesPath = "build/intermediates/bundles/$variant.flavorName"
                            tasks.create(name: "export", type: Jar) {
                                from zipTree(flavorBundlesPath + '/release/classes.jar')
                                from fileTree(dir: flavorBundlesPath + '/release', includes: ['assets/**'])
                                baseName = fileName
                                destinationDir = file(outputPath)
                            }
                            export.execute()
                        }
                    }
                }
            }
        }
        {...}
    }
    

注意:以上方法中用到的variant.flavorName是项目中有多渠道配置需要的,如果项目没有配置productFlavors,便可以省略。

参考

http://stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/
http://stackoverflow.com/questions/19034466/how-to-create-an-android-library-jar-with-gradle-without-publicly-revealing-sour

<think>嗯,用户问的是为什么他们的安卓项目只有gradle目录和testfacepass目录,而没有APP目录。这个问题需要先理解安卓项目的标准结构。通常一个标准的安卓项目在Android Studio里会有app模块,对应app目录,里面包含源代码、资源文件等。但是用户的项目里没有这个目录,可能的原因有几个。 首先,用户可能没有正确导入项目或者项目结构被修改过。有时候,特别是从其他构建工具迁移过来的项目,或者项目使用了非传统的结构,可能会有不同的目录安排。比如,可能将app模块重命名为了testfacepass,或者项目是多模块的,其中testfacepass是其中一个模块,而主模块可能缺失或被误删。 其次,用户提到的testfacepass目录可能本身就是应用模块。需要检查这个目录里是否有AndroidManifest.xml、src、res等子目录。如果有,那testfacepass可能就是应用模块,代替了传统的app目录。这种情况常见于项目模块化设计,或者模块被重命名的情况。 另外,gradle目录通常是存放Gradle Wrapper相关文件的,而项目本身的模块应该在根目录下作为子目录存在。如果用户的项目只有这两个目录,可能项目配置有问题,或者用户没有正确查看所有目录。比如,可能app目录被隐藏了,或者项目是从其他地方克隆或下载的,导致结构不完整。 还有一种可能是项目使用了复合构建(composite builds)或者包含多个独立模块,其中主应用模块未被正确包含进来。这时候需要检查settings.gradle文件,看看是否包含了需要的模块。如果settings.gradle里没有包含app模块,或者模块路径指向错误,就会导致没有生成对应的目录。 另外,用户可能误删了app目录或者在构建过程中出现了问题,导致目录没有被正确生成。需要检查版本控制历史,比如Git,看看是否有删除记录,或者构建日志是否有错误提示。 最后,可能用户的项目本身就是一个库项目(library module),而不是一个可运行的应用程序,因此没有app目录。库项目通常不会有app模块,而是作为依赖被其他项目引用。这时候需要确认项目的类型和用途。 总结下来,可能的原因包括:模块重命名、项目结构非标准、模块未被正确包含、项目类型为库、目录隐藏或误删除等。需要用户进一步检查目录结构、Gradle配置文件和版本控制历史来确认具体原因。</think>以下是关于Android项目缺失`app`目录的详细分析及解决方案: --- ### 一、常见原因分析 #### 1. **模块化项目结构** - **现象**:`testfacepass`可能是主应用模块 - **验证方式**: ```bash testfacepass/ ├── build.gradle ├── src/ │ └── main/ │ ├── AndroidManifest.xml │ └── java/ └── res/ ``` - **解决方案**:检查`settings.gradle`文件是否包含: ```groovy include ':testfacepass' # 确认模块声明 ``` #### 2. **Gradle配置异常** - **检查点**:查看根目录`build.gradle`: ```groovy allprojects { repositories { google() jcenter() } } ``` - **问题定位**:确认是否有模块声明缺失 #### 3. **项目导入错误** - **典型场景**: - 从Eclipse项目迁移时未正确转换 - Git克隆时`app`模块被`.gitignore`过滤 --- ### 二、技术验证步骤 #### 1. **Gradle文件分析** ```groovy // 根目录settings.gradle include ':app' // 如果此行被注释或缺失 // 根目录build.gradle dependencies { classpath 'com.android.tools.build:gradle:7.2.1' } ``` #### 2. **文件树结构检查** ```bash # 使用tree命令查看完整结构 project-root/ ├── gradle/ ├── testfacepass/ │ └── build.gradle # 检查android插件类型 └── build.gradle ``` 关键判断点: ```groovy // testfacepass/build.gradle中的插件声明 apply plugin: 'com.android.application' # 应用模块 apply plugin: 'com.android.library' # 库模块 ``` --- ### 三、解决方案 #### 方案A:重建主模块 1. 执行Gradle命令: ```bash ./gradlew init --type basic --dsl groovy ``` 2.Android Studio中使用`New > Module > Phone & Tablet Module` #### 方案B:配置现有模块 1. 修改`settings.gradle`: ```groovy include ':testfacepass' project(':testfacepass').projectDir = new File('testfacepass') ``` 2. 修改主`build.gradle`: ```groovy dependencies { implementation project(':testfacepass') } ``` --- ### 四、特殊场景处理 #### 场景1:组件化架构 ```bash # 典型结构 features/ ├── login/ ├── payment/ └── profile/ ``` **处理方案**: ```groovy // settings.gradle include ':app' include ':features:login' ``` #### 场景2:动态模块加载 ```groovy // build.gradle配置动态特性 dynamicFeatures = [':video_feature'] ``` --- ### 五、排查工具推荐 | 工具/命令 | 功能说明 | |-------------------------|--------------------------| | `./gradlew projects` | 列出所有已配置模块 | | `./gradlew :module:dependencies` | 查看模块依赖树 | | Android Studio > Project Structure | 图形化模块管理 | --- ### 六、经典错误案例 #### 案例1:路径映射错误 ```groovy // 错误配置 include ':app' project(':app').projectDir = new File('wrong/path') ``` #### 案例2:插件冲突 ```groovy // 错误示例 apply plugin: 'java' // 与Android插件冲突 apply plugin: 'com.android.application' ``` --- **建议优先执行以下命令诊断**: ```bash ./gradlew clean && ./gradlew :testfacepass:assembleDebug --stacktrace ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值