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
| task modifyVersionCode { File file = new File(projectDir.getParent(), '/setting/config.gradle') RandomAccessFile raf = null try { raf = new RandomAccessFile(file, "rw") String line = null long lastPoint = 0 //记住上一次的偏移量 while ((line = raf.readLine()) != null) { final long point = raf.getFilePointer() if (line.contains("versionCode")) { String line2 = line.replaceAll(Pattern.compile("\\s*|\t|\r|\n"), "") String version = line2.substring(line2.indexOf(':') + 1, line2.indexOf(',')) String lineNew = line.replace(version, (version.toInteger() + 1) + "") raf.seek(lastPoint) raf.writeBytes(lineNew) } lastPoint = point } } catch (Exception e) { e.printStackTrace() } finally { try { raf.close() } catch (IOException e) { e.printStackTrace() } } return true } preBuild.dependsOn modifyVersionCode
|