Skip to content

Commit 028a948

Browse files
author
leontli
committed
udpate to V2.0.1
Fix资源没有释放的bug
1 parent 1c9aca6 commit 028a948

File tree

7 files changed

+123
-89
lines changed

7 files changed

+123
-89
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[![license](http://img.shields.io/badge/license-BSD3-brightgreen.svg?style=flat)](https://github.com/Tencent/VasDolly/blob/master/LICENSE)
2-
[![Release Version](https://img.shields.io/badge/release-2.0.0-red.svg)](https://github.com/Tencent/VasDolly/releases)
2+
[![Release Version](https://img.shields.io/badge/release-2.0.1-red.svg)](https://github.com/Tencent/VasDolly/releases)
33
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/VasDolly/pulls)
44
[![wiki](https://img.shields.io/badge/Wiki-open-brightgreen.svg)](https://github.com/Tencent/VasDolly/wiki)
55
---
@@ -33,7 +33,7 @@ signingConfigs {
3333
``` groovy
3434
dependencies {
3535
classpath 'com.android.tools.build:gradle:3.0.0'
36-
classpath 'com.leon.channel:plugin:2.0.0'
36+
classpath 'com.leon.channel:plugin:2.0.1'
3737
}
3838
```
3939
## 引用VasDolly Plugin
@@ -45,7 +45,7 @@ apply plugin: 'channel'
4545
在主App工程的`build.gradle`中,添加读取渠道信息的helper类库依赖:
4646
``` groovy
4747
dependencies {
48-
   api 'com.leon.channel:helper:2.0.0'
48+
   api 'com.leon.channel:helper:2.0.1'
4949
}
5050
```
5151
## 配置渠道列表

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ buildscript {
1111
jcenter()
1212
}
1313
dependencies {
14-
classpath 'com.leon.channel:plugin:2.0.0'
14+
classpath 'com.leon.channel:plugin:2.0.1'
1515
}
1616
}
1717

@@ -90,7 +90,7 @@ dependencies {
9090
})
9191
api 'com.android.support:appcompat-v7:26.1.0'
9292
//api:同老版本compile作用一样,即本module将会向外暴露其依赖的module变化,即若'helper'库发生了改变,那么本module以及所有依赖于本module的module都要重新编译
93-
api 'com.leon.channel:helper:2.0.0'
93+
api 'com.leon.channel:helper:2.0.1'
9494
//implementation:本module不会向外暴露其依赖的module变化,即若'junit'库发生改变,只会重编译本module,不会重编依赖于本module的外部module,即不会向外传播
9595
testImplementation 'junit:junit:4.12'
9696
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ext {
99

1010

1111
GROUP = 'com.leon.channel'
12-
VERSION = '2.0.0'
12+
VERSION = '2.0.1'
1313

1414
//POM_PACKAGING = "pom"
1515
POM_DESCRIPTION = "VasDolly"

command/jar/VasDolly.jar

169 Bytes
Binary file not shown.

common/src/main/java/com/leon/channel/common/V1SchemeUtil.java

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,55 +51,63 @@ public static void writeChannel(File file, String channel) throws Exception {
5151
if (file == null || !file.exists() || !file.isFile() || channel == null || channel.isEmpty()) {
5252
throw new Exception("param error , file : " + file + " , channel : " + channel);
5353
}
54-
54+
RandomAccessFile raf = null;
5555
byte[] comment = channel.getBytes(ChannelConstants.CONTENT_CHARSET);
5656
Pair<ByteBuffer, Long> eocdAndOffsetInFile = getEocd(file);
5757
if (eocdAndOffsetInFile.getFirst().remaining() == ZipUtils.ZIP_EOCD_REC_MIN_SIZE) {
5858
System.out.println("file : " + file.getAbsolutePath() + " , has no comment");
59-
60-
RandomAccessFile raf = new RandomAccessFile(file, "rw");
61-
//1.locate comment length field
62-
raf.seek(file.length() - ChannelConstants.SHORT_LENGTH);
63-
//2.write zip comment length (content field length + length field length + magic field length)
64-
writeShort(comment.length + ChannelConstants.SHORT_LENGTH + ChannelConstants.V1_MAGIC.length, raf);
65-
//3.write content
66-
raf.write(comment);
67-
//4.write content length
68-
writeShort(comment.length, raf);
69-
//5. write magic bytes
70-
raf.write(ChannelConstants.V1_MAGIC);
71-
raf.close();
59+
try {
60+
raf = new RandomAccessFile(file, "rw");
61+
//1.locate comment length field
62+
raf.seek(file.length() - ChannelConstants.SHORT_LENGTH);
63+
//2.write zip comment length (content field length + length field length + magic field length)
64+
writeShort(comment.length + ChannelConstants.SHORT_LENGTH + ChannelConstants.V1_MAGIC.length, raf);
65+
//3.write content
66+
raf.write(comment);
67+
//4.write content length
68+
writeShort(comment.length, raf);
69+
//5. write magic bytes
70+
raf.write(ChannelConstants.V1_MAGIC);
71+
} finally {
72+
if (raf != null) {
73+
raf.close();
74+
}
75+
}
7276
} else {
7377
System.out.println("file : " + file.getAbsolutePath() + " , has comment");
7478
if (containV1Magic(file)) {
7579
try {
7680
String existChannel = readChannel(file);
77-
if (existChannel != null){
81+
if (existChannel != null) {
7882
file.delete();
7983
throw new ChannelExistException("file : " + file.getAbsolutePath() + " has a channel : " + existChannel + ", only ignore");
8084
}
81-
}catch (Exception e){
85+
} catch (Exception e) {
8286
e.printStackTrace();
8387
}
8488
}
8589

8690
int existCommentLength = ZipUtils.getUnsignedInt16(eocdAndOffsetInFile.getFirst(), ZipUtils.ZIP_EOCD_REC_MIN_SIZE - ChannelConstants.SHORT_LENGTH);
8791
int newCommentLength = existCommentLength + comment.length + ChannelConstants.SHORT_LENGTH + ChannelConstants.V1_MAGIC.length;
88-
RandomAccessFile raf = new RandomAccessFile(file, "rw");
89-
//1.locate comment length field
90-
raf.seek(eocdAndOffsetInFile.getSecond() + ZipUtils.ZIP_EOCD_REC_MIN_SIZE - ChannelConstants.SHORT_LENGTH);
91-
//2.write zip comment length (existCommentLength + content field length + length field length + magic field length)
92-
writeShort(newCommentLength, raf);
93-
//3.locate where channel should begin
94-
raf.seek(eocdAndOffsetInFile.getSecond() + ZipUtils.ZIP_EOCD_REC_MIN_SIZE + existCommentLength);
95-
//4.write content
96-
raf.write(comment);
97-
//5.write content length
98-
writeShort(comment.length, raf);
99-
//6.write magic bytes
100-
raf.write(ChannelConstants.V1_MAGIC);
101-
raf.close();
102-
92+
try {
93+
raf = new RandomAccessFile(file, "rw");
94+
//1.locate comment length field
95+
raf.seek(eocdAndOffsetInFile.getSecond() + ZipUtils.ZIP_EOCD_REC_MIN_SIZE - ChannelConstants.SHORT_LENGTH);
96+
//2.write zip comment length (existCommentLength + content field length + length field length + magic field length)
97+
writeShort(newCommentLength, raf);
98+
//3.locate where channel should begin
99+
raf.seek(eocdAndOffsetInFile.getSecond() + ZipUtils.ZIP_EOCD_REC_MIN_SIZE + existCommentLength);
100+
//4.write content
101+
raf.write(comment);
102+
//5.write content length
103+
writeShort(comment.length, raf);
104+
//6.write magic bytes
105+
raf.write(ChannelConstants.V1_MAGIC);
106+
} finally {
107+
if (raf != null) {
108+
raf.close();
109+
}
110+
}
103111
}
104112
}
105113

@@ -213,14 +221,22 @@ public static Pair<ByteBuffer, Long> getEocd(File apk) throws IOException, ApkSi
213221
if (apk == null || !apk.exists() || !apk.isFile()) {
214222
return null;
215223
}
216-
RandomAccessFile raf = new RandomAccessFile(apk, "r");
217-
//find the EOCD
218-
Pair<ByteBuffer, Long> eocdAndOffsetInFile = ApkSignatureSchemeV2Verifier.getEocd(raf);
219-
if (ZipUtils.isZip64EndOfCentralDirectoryLocatorPresent(raf, eocdAndOffsetInFile.getSecond())) {
220-
throw new ApkSignatureSchemeV2Verifier.SignatureNotFoundException("ZIP64 APK not supported");
224+
RandomAccessFile raf = null;
225+
try {
226+
raf = new RandomAccessFile(apk, "r");
227+
//find the EOCD
228+
Pair<ByteBuffer, Long> eocdAndOffsetInFile = ApkSignatureSchemeV2Verifier.getEocd(raf);
229+
if (ZipUtils.isZip64EndOfCentralDirectoryLocatorPresent(raf, eocdAndOffsetInFile.getSecond())) {
230+
throw new ApkSignatureSchemeV2Verifier.SignatureNotFoundException("ZIP64 APK not supported");
231+
}
232+
233+
return eocdAndOffsetInFile;
234+
}finally {
235+
if (raf != null){
236+
raf.close();
237+
}
221238
}
222239

223-
return eocdAndOffsetInFile;
224240
}
225241

226242
/**

common/src/main/java/com/leon/channel/common/V2SchemeUtil.java

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,28 @@ public static ByteBuffer getApkSigningBlock(File channelFile) throws ApkSignatur
9999
if (channelFile == null || !channelFile.exists() || !channelFile.isFile()) {
100100
return null;
101101
}
102-
RandomAccessFile apk = new RandomAccessFile(channelFile, "r");
103-
//1.find the EOCD
104-
Pair<ByteBuffer, Long> eocdAndOffsetInFile = ApkSignatureSchemeV2Verifier.getEocd(apk);
105-
ByteBuffer eocd = eocdAndOffsetInFile.getFirst();
106-
long eocdOffset = eocdAndOffsetInFile.getSecond();
107-
108-
if (ZipUtils.isZip64EndOfCentralDirectoryLocatorPresent(apk, eocdOffset)) {
109-
throw new ApkSignatureSchemeV2Verifier.SignatureNotFoundException("ZIP64 APK not supported");
110-
}
111-
112-
//2.find the APK Signing Block. The block immediately precedes the Central Directory.
113-
long centralDirOffset = ApkSignatureSchemeV2Verifier.getCentralDirOffset(eocd, eocdOffset);//通过eocd找到中央目录的偏移量
114-
//3. find the apk V2 signature block
115-
Pair<ByteBuffer, Long> apkSignatureBlock =
116-
ApkSignatureSchemeV2Verifier.findApkSigningBlock(apk, centralDirOffset);//找到V2签名块的内容和偏移量
102+
RandomAccessFile apk = null;
103+
try {
104+
apk = new RandomAccessFile(channelFile, "r");
105+
//1.find the EOCD
106+
Pair<ByteBuffer, Long> eocdAndOffsetInFile = ApkSignatureSchemeV2Verifier.getEocd(apk);
107+
ByteBuffer eocd = eocdAndOffsetInFile.getFirst();
108+
long eocdOffset = eocdAndOffsetInFile.getSecond();
109+
110+
if (ZipUtils.isZip64EndOfCentralDirectoryLocatorPresent(apk, eocdOffset)) {
111+
throw new ApkSignatureSchemeV2Verifier.SignatureNotFoundException("ZIP64 APK not supported");
112+
}
117113

118-
return apkSignatureBlock.getFirst();
114+
//2.find the APK Signing Block. The block immediately precedes the Central Directory.
115+
long centralDirOffset = ApkSignatureSchemeV2Verifier.getCentralDirOffset(eocd, eocdOffset);//通过eocd找到中央目录的偏移量
116+
//3. find the apk V2 signature block
117+
Pair<ByteBuffer, Long> apkSignatureBlock = ApkSignatureSchemeV2Verifier.findApkSigningBlock(apk, centralDirOffset);//找到V2签名块的内容和偏移量
118+
return apkSignatureBlock.getFirst();
119+
}finally {
120+
if (apk != null){
121+
apk.close();
122+
}
123+
}
119124
}
120125

121126
/**
@@ -127,39 +132,46 @@ public static ByteBuffer getApkSigningBlock(File channelFile) throws ApkSignatur
127132
* @throws ApkSignatureSchemeV2Verifier.SignatureNotFoundException not have v2 sinature
128133
*/
129134
public static ApkSectionInfo getApkSectionInfo(File baseApk, boolean lowMemory) throws IOException, ApkSignatureSchemeV2Verifier.SignatureNotFoundException {
130-
RandomAccessFile apk = new RandomAccessFile(baseApk, "r");
131-
//1.find the EOCD and offset
132-
Pair<ByteBuffer, Long> eocdAndOffsetInFile = ApkSignatureSchemeV2Verifier.getEocd(apk);
133-
ByteBuffer eocd = eocdAndOffsetInFile.getFirst();
134-
long eocdOffset = eocdAndOffsetInFile.getSecond();
135-
136-
if (ZipUtils.isZip64EndOfCentralDirectoryLocatorPresent(apk, eocdOffset)) {
137-
throw new ApkSignatureSchemeV2Verifier.SignatureNotFoundException("ZIP64 APK not supported");
138-
}
135+
RandomAccessFile apk = null;
136+
try {
137+
apk = new RandomAccessFile(baseApk, "r");
138+
//1.find the EOCD and offset
139+
Pair<ByteBuffer, Long> eocdAndOffsetInFile = ApkSignatureSchemeV2Verifier.getEocd(apk);
140+
ByteBuffer eocd = eocdAndOffsetInFile.getFirst();
141+
long eocdOffset = eocdAndOffsetInFile.getSecond();
142+
143+
if (ZipUtils.isZip64EndOfCentralDirectoryLocatorPresent(apk, eocdOffset)) {
144+
throw new ApkSignatureSchemeV2Verifier.SignatureNotFoundException("ZIP64 APK not supported");
145+
}
139146

140-
//2.find the APK Signing Block. The block immediately precedes the Central Directory.
141-
long centralDirOffset = ApkSignatureSchemeV2Verifier.getCentralDirOffset(eocd, eocdOffset);//通过eocd找到中央目录的偏移量
142-
Pair<ByteBuffer, Long> apkSchemeV2Block = ApkSignatureSchemeV2Verifier.findApkSigningBlock(apk, centralDirOffset);//找到V2签名块的内容和偏移量
147+
//2.find the APK Signing Block. The block immediately precedes the Central Directory.
148+
long centralDirOffset = ApkSignatureSchemeV2Verifier.getCentralDirOffset(eocd, eocdOffset);//通过eocd找到中央目录的偏移量
149+
Pair<ByteBuffer, Long> apkSchemeV2Block = ApkSignatureSchemeV2Verifier.findApkSigningBlock(apk, centralDirOffset);//找到V2签名块的内容和偏移量
143150

144-
//3.find the centralDir
145-
Pair<ByteBuffer, Long> centralDir = findCentralDir(apk, centralDirOffset, (int) (eocdOffset - centralDirOffset));
151+
//3.find the centralDir
152+
Pair<ByteBuffer, Long> centralDir = findCentralDir(apk, centralDirOffset, (int) (eocdOffset - centralDirOffset));
146153

147-
ApkSectionInfo apkSectionInfo = new ApkSectionInfo();
148-
apkSectionInfo.lowMemory = lowMemory;
149-
apkSectionInfo.apkSize = baseApk.length();
150-
if (!lowMemory) {
151-
//4.find the contentEntry
152-
apkSectionInfo.contentEntry = findContentEntry(apk, (int) apkSchemeV2Block.getSecond().longValue());
154+
ApkSectionInfo apkSectionInfo = new ApkSectionInfo();
155+
apkSectionInfo.lowMemory = lowMemory;
156+
apkSectionInfo.apkSize = baseApk.length();
157+
if (!lowMemory) {
158+
//4.find the contentEntry
159+
apkSectionInfo.contentEntry = findContentEntry(apk, (int) apkSchemeV2Block.getSecond().longValue());
160+
}
161+
apkSectionInfo.schemeV2Block = apkSchemeV2Block;
162+
apkSectionInfo.centralDir = centralDir;
163+
apkSectionInfo.eocd = eocdAndOffsetInFile;
164+
165+
//5. check Paramters
166+
apkSectionInfo.checkParamters();
167+
168+
System.out.println("baseApk : " + baseApk.getAbsolutePath() + "\nApkSectionInfo = " + apkSectionInfo);
169+
return apkSectionInfo;
170+
}finally {
171+
if (apk != null){
172+
apk.close();
173+
}
153174
}
154-
apkSectionInfo.schemeV2Block = apkSchemeV2Block;
155-
apkSectionInfo.centralDir = centralDir;
156-
apkSectionInfo.eocd = eocdAndOffsetInFile;
157-
158-
//5. check Paramters
159-
apkSectionInfo.checkParamters();
160-
161-
System.out.println("baseApk : " + baseApk.getAbsolutePath() + "\nApkSectionInfo = " + apkSectionInfo);
162-
return apkSectionInfo;
163175
}
164176

165177
/**

common/src/main/java/com/leon/channel/common/verify/ApkSignatureSchemeV2Verifier.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ public class ApkSignatureSchemeV2Verifier {
5050
* <p><b>NOTE: This method does not verify the signature.</b>
5151
*/
5252
public static boolean hasSignature(String apkFile) throws IOException {
53-
try (RandomAccessFile apk = new RandomAccessFile(apkFile, "r")) {
53+
RandomAccessFile apk = null;
54+
try {
55+
apk = new RandomAccessFile(apkFile, "r");
5456
findSignature(apk);
5557
return true;
5658
} catch (SignatureNotFoundException e) {
5759
return false;
60+
}finally {
61+
if (apk != null){
62+
apk.close();
63+
}
5864
}
5965
}
6066

0 commit comments

Comments
 (0)