Skip to content

Commit 868d84a

Browse files
committed
打包时支持选择编译后APK的权限
1 parent 9e21c1b commit 868d84a

File tree

13 files changed

+319
-156
lines changed

13 files changed

+319
-156
lines changed

apkbuilder/build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ dependencies {
2828
})
2929
testImplementation 'junit:junit:4.13.2'
3030
api fileTree(dir: 'libs', include: ['*.jar'])
31-
api files('libs/tiny-sign-0.9.jar')
3231
api files('libs/commons-io-2.5.jar')
3332
implementation "androidx.core:core-ktx:1.8.0"
3433
}

apkbuilder/libs/tiny-sign-0.9.jar

-8.16 KB
Binary file not shown.

apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ApkBuilder.java

-114
This file was deleted.

apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ApkPackager.java

-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import java.util.zip.ZipEntry;
1414
import java.util.zip.ZipInputStream;
1515

16-
import pxb.android.tinysign.TinySign;
17-
1816
/**
1917
* Created by Stardust on 2017/10/23.
2018
*/
@@ -52,12 +50,6 @@ public void unzip() throws IOException {
5250
}
5351
}
5452

55-
public void repackage(String newApkPath) throws Exception {
56-
FileOutputStream fos = new FileOutputStream(newApkPath);
57-
TinySign.sign(new File(mWorkspacePath), fos);
58-
fos.close();
59-
}
60-
6153
public void cleanWorkspace() {
6254

6355
}

apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ManifestEditor.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.stardust.autojs.apkbuilder;
22

3+
import android.util.Log;
4+
35
import org.apache.commons.io.IOUtils;
46

57
import java.io.IOException;
@@ -94,28 +96,44 @@ public void onAttr(AxmlWriter.Attr attr) {
9496
}
9597
}
9698

99+
public boolean filterPermission(String permissionName) {
100+
return true;
101+
}
102+
97103

98-
private class MutableAxmlWriter extends AxmlWriter {
99-
private class MutableNodeImpl extends AxmlWriter.NodeImpl {
104+
public class MutableAxmlWriter extends AxmlWriter {
105+
public class MutableNodeImpl extends AxmlWriter.NodeImpl {
106+
private boolean ignore;
107+
private final String name;
100108

101109
MutableNodeImpl(String ns, String name) {
102110
super(ns, name);
111+
this.name = name;
103112
}
104113

105114
@Override
106115
protected void onAttr(AxmlWriter.Attr a) {
116+
if ("uses-permission".equals(this.name) && "name".equals(a.name.data) && a.value instanceof StringItem) {
117+
if (ManifestEditor.this.filterPermission(((StringItem) a.value).data)) {
118+
ignore = true;
119+
}
120+
}
107121
ManifestEditor.this.onAttr(a);
108122
super.onAttr(a);
109123
}
110124

111125

112126
@Override
113127
public NodeVisitor child(String ns, String name) {
128+
Log.d("MutableAxmlWriter", "child: " + ns + " name: " + name);
114129
NodeImpl child = new MutableNodeImpl(ns, name);
115130
this.children.add(child);
116131
return child;
117132
}
118133

134+
public boolean isIgnore() {
135+
return ignore;
136+
}
119137
}
120138

121139
@Override

apkbuilder/src/main/java/pxb/android/axml/AxmlWriter.java

+29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package pxb.android.axml;
1717

18+
import android.util.Log;
19+
20+
import com.stardust.autojs.apkbuilder.ManifestEditor;
21+
1822
import pxb.android.StringItem;
1923
import pxb.android.StringItems;
2024

@@ -31,6 +35,7 @@
3135
* @author <a href="mailto:[email protected]">Panxiaobo</a>
3236
*/
3337
public class AxmlWriter extends AxmlVisitor {
38+
private static final String TAG = "AxmlWriter";
3439
static final Comparator<Attr> ATTR_CMP = new Comparator<Attr>() {
3540

3641
@Override
@@ -91,6 +96,12 @@ private int prepare() throws IOException {
9196
int size = 0;
9297

9398
for (NodeImpl first : firsts) {
99+
if (first instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
100+
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) {
101+
Log.d(TAG, "prepare: first is ignore: " + first.name);
102+
continue;
103+
}
104+
}
94105
size += first.prepare(this);
95106
}
96107
{
@@ -162,6 +173,12 @@ public byte[] toByteArray() throws IOException {
162173
}
163174

164175
for (NodeImpl first : firsts) {
176+
if (first instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
177+
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) {
178+
Log.d(TAG, "toByteArray: first is ignore: " + first.name);
179+
continue;
180+
}
181+
}
165182
first.write(out);
166183
}
167184

@@ -342,6 +359,12 @@ public int prepare(AxmlWriter axmlWriter) {
342359
int size = 24 + 36 + attrs.size() * 20;// 24 for end tag,36+x*20 for
343360
// start tag
344361
for (NodeImpl child : children) {
362+
if (child instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
363+
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) {
364+
Log.d(TAG, "prepare: child is ignore: " + child.name);
365+
continue;
366+
}
367+
}
345368
size += child.prepare(axmlWriter);
346369
}
347370
if (text != null) {
@@ -398,6 +421,12 @@ void write(ByteBuffer out) throws IOException {
398421

399422
// children
400423
for (NodeImpl child : children) {
424+
if (child instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) {
425+
if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) {
426+
Log.d(TAG, "white: child is ignore: " + child.name);
427+
continue;
428+
}
429+
}
401430
child.write(out);
402431
}
403432

app/src/main/java/org/autojs/autojs/build/ApkBuilder.java

+17
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import java.util.ArrayList;
3030
import java.util.Arrays;
3131
import java.util.Collections;
32+
import java.util.HashSet;
3233
import java.util.List;
34+
import java.util.Set;
3335
import java.util.concurrent.Callable;
3436
import java.util.concurrent.atomic.AtomicBoolean;
3537

@@ -74,6 +76,7 @@ public static class AppConfig {
7476
Boolean usePaddleOcr = false;
7577
Boolean useMlKitOcr = false;
7678
Boolean useTessTwo = false;
79+
Set<String> enabledPermission = new HashSet<>();
7780

7881
public static AppConfig fromProjectConfig(String projectDir, ProjectConfig projectConfig) {
7982
String icon = projectConfig.getIcon();
@@ -183,6 +186,14 @@ public Boolean getUseTessTwo() {
183186
public void setUseTessTwo(Boolean useTessTwo) {
184187
this.useTessTwo = useTessTwo;
185188
}
189+
190+
public Set<String> getEnabledPermission() {
191+
return enabledPermission;
192+
}
193+
194+
public void setEnabledPermission(Set<String> enabledPermission) {
195+
this.enabledPermission = enabledPermission;
196+
}
186197
}
187198

188199
private ProgressCallback mProgressCallback;
@@ -550,7 +561,13 @@ public void onAttr(AxmlWriter.Attr attr) {
550561
} else {
551562
super.onAttr(attr);
552563
}
564+
}
553565

566+
@Override
567+
public boolean filterPermission(String permissionName) {
568+
Log.d(TAG, "filterPermission: " + permissionName);
569+
boolean persist = mAppConfig.getEnabledPermission().contains(permissionName);
570+
return !persist;
554571
}
555572
}
556573
}

0 commit comments

Comments
 (0)