gradle 指定导出包的名字和路径

为了方便识别apk文件,一般我们都希望通过androoid studio打包的文件,会带上app的名字,时间,之类的,使用多渠道打包的时候,还希望带上渠道名称

以前我都这样做

 //修改生成的apk名字
android{
        applicationVariants.all { variant ->
            if (variant.buildType.name.equals('release')) {
                variant.outputs.each { output ->
                    def parent = './apk/'
                    def buildName
                    def releaseApkName
                    def type = variant.buildType.name;
                    releaseApkName = buildName + '_' + type + "_" + versionName + '_' + getDate() + '.apk'
                    output.outputFile = new File(parent, releaseApkName)
                }
            }
      }
}

但是gradle更新到3.0.0以后就不行了,会报错

Error:(26, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=release, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
<a href="openFile:E:\workspace\Downloader\app\build.gradle">Open File</a>

再贴个图

 

image.png

这个错误大概意思就是outputFile这个引用现在是“read-only ”(只读)的,不能重新赋予新的对象。也就是说原来的讨论不让用了,经过多方Google,我找到了这个方法

现在这样做

android{
  applicationVariants.all { variant ->
        if (variant.buildType.name.equals('release')) {
            variant.outputs.all { output ->
                def buildName = "Downloader"
                def type = variant.buildType.name
                def releaseApkName = buildName + '_' + type + "_" + versionName + '_' + getTime() + '.apk'
                outputFileName = releaseApkName
            }
        }
    }
}

两种方式的区别在于,重命名的地方,从 variant.outputs.each 方法改成 variant.outputs.all 方法,以及output.outputFile = new File(parent, releaseApkName) 改成 outputFileName = releaseApkName,就是说,现在不能指定路径,你只能修改文件名,路径只能使用默认的(当然你如果找到了可以修改路径的方法,千万留言),默认路径一般在/app/build/outputs/apk/{buildType}/:

image.png

你如果使用Android stuido 的打包选型,并且创建了新的productFlavors,会在App文件夹里自动创建对应productFlavors的文件:

 

image.png

补充内容

评论中lijiankun24告知,可以用这种方法指定apk输出目录

android{
  applicationVariants.all { variant ->
        if (variant.buildType.name.equals('release')) {
            variant.outputs.all { output ->
                def buildName = "Downloader"
                def type = variant.buildType.name
                def releaseApkName = buildName + '_' + type + "_" + versionName + '_' + getTime() + '.apk'
                outputFileName = releaseApkName
                variant.packageApplication.outputDirectory = new File("./apk")
            }
        }
    }
}

即在all方法中调用variant.packageApplication.outputDirectory = new File("./apk")

 


转载自:https://www.jianshu.com/p/0066cb59aa4f

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好,可以使用Apache POI库将JavaFX中的TableView导出为Excel文件,然后按照指定的文件路径保存。具体步骤如下: 1. 添加Apache POI库的依赖。您可以在Maven或Gradle中添加以下依赖: ```xml <!-- For Excel file processing --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建Excel文档并创建工作表: ```java Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Table Data"); ``` 3. 获取TableView中的数据并将其写入Excel中: ```java ObservableList<TableColumn<Person, ?>> columns = tableView.getColumns(); ObservableList<Person> data = tableView.getItems(); // Write column headers Row headerRow = sheet.createRow(0); for (int i = 0; i < columns.size(); i++) { TableColumn<Person, ?> column = columns.get(i); headerRow.createCell(i).setCellValue(column.getText()); } // Write data rows for (int rowIndex = 0; rowIndex < data.size(); rowIndex++) { Row dataRow = sheet.createRow(rowIndex + 1); for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { TableColumn<Person, ?> column = columns.get(columnIndex); String cellValue = column.getCellData(data.get(rowIndex)).toString(); dataRow.createCell(columnIndex).setCellValue(cellValue); } } ``` 4. 将Excel文件保存到指定路径: ```java FileOutputStream outputStream = new FileOutputStream(filePath); workbook.write(outputStream); workbook.close(); outputStream.close(); ``` 完整的代码示例可以参考以下代码: ```java import javafx.application.Application; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; public class ExportTableViewToExcel extends Application { @Override public void start(Stage primaryStage) throws Exception { TableView<Person> tableView = new TableView<>(); tableView.getColumns().addAll( new TableColumn<>("First Name"), new TableColumn<>("Last Name"), new TableColumn<>("Age") ); tableView.getItems().addAll( new Person("John", "Doe", 30), new Person("Jane", "Doe", 25), new Person("Bob", "Smith", 40) ); StackPane root = new StackPane(tableView); Scene scene = new Scene(root, 400, 400); primaryStage.setScene(scene); primaryStage.show(); exportTableViewToExcel(tableView, "C:/temp/persons.xlsx"); } private void exportTableViewToExcel(TableView<Person> tableView, String filePath) throws IOException { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Table Data"); ObservableList<TableColumn<Person, ?>> columns = tableView.getColumns(); ObservableList<Person> data = tableView.getItems(); // Write column headers Row headerRow = sheet.createRow(0); for (int i = 0; i < columns.size(); i++) { TableColumn<Person, ?> column = columns.get(i); headerRow.createCell(i).setCellValue(column.getText()); } // Write data rows for (int rowIndex = 0; rowIndex < data.size(); rowIndex++) { Row dataRow = sheet.createRow(rowIndex + 1); for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { TableColumn<Person, ?> column = columns.get(columnIndex); String cellValue = column.getCellData(data.get(rowIndex)).toString(); dataRow.createCell(columnIndex).setCellValue(cellValue); } } FileOutputStream outputStream = new FileOutputStream(filePath); workbook.write(outputStream); workbook.close(); outputStream.close(); } public static void main(String[] args) { launch(args); } private static class Person { private String firstName; private String lastName; private int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } } ``` 以上代码将TableView中的数据导出到了C:/temp/persons.xlsx文件中。您可以根据需要更改文件路径

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值