diff --git a/apkbuilder/src/androidTest/java/com/stardust/autojs/apkbuilder/ExampleInstrumentedTest.java b/apkbuilder/src/androidTest/java/com/stardust/autojs/apkbuilder/ExampleInstrumentedTest.java deleted file mode 100755 index be7e498e..00000000 --- a/apkbuilder/src/androidTest/java/com/stardust/autojs/apkbuilder/ExampleInstrumentedTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.stardust.autojs.apkbuilder; - -import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.junit.Assert.*; - -/** - * Instrumentation test, which will execute on an Android device. - * - * @see Testing documentation - */ -@RunWith(AndroidJUnit4.class) -public class ExampleInstrumentedTest { - @Test - public void useAppContext() throws Exception { - // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); - - assertEquals("com.stardust.autojs.apkbuilder.test", appContext.getPackageName()); - } -} diff --git a/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ApkPackager.java b/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ApkUnpackUtil.java similarity index 63% rename from apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ApkPackager.java rename to apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ApkUnpackUtil.java index 3b525508..ab7c6eca 100755 --- a/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ApkPackager.java +++ b/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ApkUnpackUtil.java @@ -14,24 +14,20 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** + * 负责将内置apk解压到指定目录 + * * Created by Stardust on 2017/10/23. */ +public class ApkUnpackUtil { -public class ApkPackager { + private final InputStream mApkInputStream; + private final String mWorkspacePath; - private InputStream mApkInputStream; - private String mWorkspacePath; - - public ApkPackager(InputStream apkInputStream, String workspacePath) { + public ApkUnpackUtil(InputStream apkInputStream, String workspacePath) { mApkInputStream = apkInputStream; mWorkspacePath = workspacePath; } - public ApkPackager(String apkPath, String workspacePath) throws FileNotFoundException { - mApkInputStream = new FileInputStream(apkPath); - mWorkspacePath = workspacePath; - } - public void unzip() throws IOException { try (ZipInputStream zis = new ZipInputStream(mApkInputStream)) { for (ZipEntry e = zis.getNextEntry(); e != null; e = zis.getNextEntry()) { @@ -39,19 +35,15 @@ public class ApkPackager { if (!e.isDirectory() && !TextUtils.isEmpty(name)) { File file = new File(mWorkspacePath, name); System.out.println(file); - file.getParentFile().mkdirs(); - FileOutputStream fos = new FileOutputStream(file); - StreamUtils.write(zis, fos); - fos.close(); + if (file.getParentFile() != null && file.getParentFile().mkdirs()) { + try (FileOutputStream fos = new FileOutputStream(file)) { + StreamUtils.write(zis, fos); + } + } } else { System.out.println("file or empty:" + name); } } } } - - public void cleanWorkspace() { - - } - } diff --git a/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ManifestEditor.java b/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ManifestEditor.java index 6d4e17eb..aae725e3 100755 --- a/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ManifestEditor.java +++ b/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/ManifestEditor.java @@ -1,7 +1,5 @@ package com.stardust.autojs.apkbuilder; -import android.util.Log; - import org.apache.commons.io.IOUtils; import java.io.IOException; @@ -11,7 +9,6 @@ import java.io.OutputStream; import pxb.android.StringItem; import pxb.android.axml.AxmlReader; import pxb.android.axml.AxmlWriter; -import pxb.android.axml.NodeVisitor; /** * Created by Stardust on 2017/10/23. @@ -55,7 +52,7 @@ public class ManifestEditor { public ManifestEditor commit() throws IOException { try { - AxmlWriter writer = new MutableAxmlWriter(); + AxmlWriter writer = new MutableAxmlWriter(this); AxmlReader reader = new AxmlReader(IOUtils.readFully(mManifestInputStream, mManifestInputStream.available())); reader.accept(writer); mManifestData = writer.toByteArray(); @@ -101,48 +98,4 @@ public class ManifestEditor { } - public class MutableAxmlWriter extends AxmlWriter { - public class MutableNodeImpl extends AxmlWriter.NodeImpl { - private boolean ignore; - private final String name; - - MutableNodeImpl(String ns, String name) { - super(ns, name); - this.name = name; - } - - @Override - protected void onAttr(AxmlWriter.Attr a) { - if ("uses-permission".equals(this.name) && "name".equals(a.name.data) && a.value instanceof StringItem) { - if (ManifestEditor.this.filterPermission(((StringItem) a.value).data)) { - ignore = true; - } - } - ManifestEditor.this.onAttr(a); - super.onAttr(a); - } - - - @Override - public NodeVisitor child(String ns, String name) { - Log.d("MutableAxmlWriter", "child: " + ns + " name: " + name); - NodeImpl child = new MutableNodeImpl(ns, name); - this.children.add(child); - return child; - } - - public boolean isIgnore() { - return ignore; - } - } - - @Override - public NodeVisitor child(String ns, String name) { - NodeImpl first = new MutableNodeImpl(ns, name); - this.firsts.add(first); - return first; - } - } - - } diff --git a/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/MutableAxmlWriter.java b/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/MutableAxmlWriter.java new file mode 100644 index 00000000..325fecc7 --- /dev/null +++ b/apkbuilder/src/main/java/com/stardust/autojs/apkbuilder/MutableAxmlWriter.java @@ -0,0 +1,63 @@ +package com.stardust.autojs.apkbuilder; + +import android.util.Log; + +import pxb.android.StringItem; +import pxb.android.axml.AxmlWriter; +import pxb.android.axml.NodeVisitor; + +/** + * AndroidManifest文件修改,用于修改xml元素 + * + * @author TonyJiangWJ + * @since 2023/8/16 + */ +public class MutableAxmlWriter extends AxmlWriter { + private final ManifestEditor manifestEditor; + + public MutableAxmlWriter(ManifestEditor manifestEditor) { + this.manifestEditor = manifestEditor; + } + + @Override + public NodeVisitor child(String ns, String name) { + AxmlWriter.NodeImpl first = new MutableAxmlWriter.MutableNodeImpl(ns, name); + this.firsts.add(first); + return first; + } + + public class MutableNodeImpl extends AxmlWriter.NodeImpl { + private boolean ignore; + private final String name; + + MutableNodeImpl(String ns, String name) { + super(ns, name); + this.name = name; + } + + @Override + protected void onAttr(AxmlWriter.Attr a) { + if ("uses-permission".equals(this.name) && "name".equals(a.name.data) && a.value instanceof StringItem) { + if (manifestEditor.filterPermission(((StringItem) a.value).data)) { + ignore = true; + } + } + manifestEditor.onAttr(a); + super.onAttr(a); + } + + + @Override + public NodeVisitor child(String ns, String name) { + Log.d("MutableAxmlWriter", "child: " + ns + " name: " + name); + AxmlWriter.NodeImpl child = new MutableAxmlWriter.MutableNodeImpl(ns, name); + this.children.add(child); + return child; + } + + public boolean isIgnore() { + return ignore; + } + } +} + diff --git a/apkbuilder/src/main/java/pxb/android/axml/AxmlWriter.java b/apkbuilder/src/main/java/pxb/android/axml/AxmlWriter.java index e4ade7e1..35e6fb3d 100755 --- a/apkbuilder/src/main/java/pxb/android/axml/AxmlWriter.java +++ b/apkbuilder/src/main/java/pxb/android/axml/AxmlWriter.java @@ -17,7 +17,7 @@ package pxb.android.axml; import android.util.Log; -import com.stardust.autojs.apkbuilder.ManifestEditor; +import com.stardust.autojs.apkbuilder.MutableAxmlWriter; import pxb.android.StringItem; import pxb.android.StringItems; @@ -96,8 +96,8 @@ public class AxmlWriter extends AxmlVisitor { int size = 0; for (NodeImpl first : firsts) { - if (first instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) { - if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) { + if (first instanceof MutableAxmlWriter.MutableNodeImpl) { + if (((MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) { Log.d(TAG, "prepare: first is ignore: " + first.name); continue; } @@ -173,8 +173,8 @@ public class AxmlWriter extends AxmlVisitor { } for (NodeImpl first : firsts) { - if (first instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) { - if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) { + if (first instanceof MutableAxmlWriter.MutableNodeImpl) { + if (((MutableAxmlWriter.MutableNodeImpl)first).isIgnore()) { Log.d(TAG, "toByteArray: first is ignore: " + first.name); continue; } @@ -359,8 +359,8 @@ public class AxmlWriter extends AxmlVisitor { int size = 24 + 36 + attrs.size() * 20;// 24 for end tag,36+x*20 for // start tag for (NodeImpl child : children) { - if (child instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) { - if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) { + if (child instanceof MutableAxmlWriter.MutableNodeImpl) { + if (((MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) { Log.d(TAG, "prepare: child is ignore: " + child.name); continue; } @@ -421,8 +421,8 @@ public class AxmlWriter extends AxmlVisitor { // children for (NodeImpl child : children) { - if (child instanceof ManifestEditor.MutableAxmlWriter.MutableNodeImpl) { - if (((ManifestEditor.MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) { + if (child instanceof MutableAxmlWriter.MutableNodeImpl) { + if (((MutableAxmlWriter.MutableNodeImpl)child).isIgnore()) { Log.d(TAG, "white: child is ignore: " + child.name); continue; } diff --git a/app/src/main/java/org/autojs/autojs/build/ApkBuilder.java b/app/src/main/java/org/autojs/autojs/build/ApkBuilder.java index d56b1681..3bc51860 100644 --- a/app/src/main/java/org/autojs/autojs/build/ApkBuilder.java +++ b/app/src/main/java/org/autojs/autojs/build/ApkBuilder.java @@ -8,7 +8,7 @@ import com.android.apksig.ApkSigner; import com.android.apksigner.PasswordRetriever; import com.android.apksigner.SignerParams; import com.stardust.app.GlobalAppContext; -import com.stardust.autojs.apkbuilder.ApkPackager; +import com.stardust.autojs.apkbuilder.ApkUnpackUtil; import com.stardust.autojs.apkbuilder.ManifestEditor; import com.stardust.autojs.apkbuilder.util.StreamUtils; import com.stardust.autojs.project.BuildInfo; @@ -197,7 +197,7 @@ public class ApkBuilder { } private ProgressCallback mProgressCallback; - private ApkPackager mApkPackager; + private ApkUnpackUtil mApkUnpackUtil; private String mArscPackageName; private ManifestEditor mManifestEditor; private String mWorkspacePath; @@ -210,7 +210,7 @@ public class ApkBuilder { public ApkBuilder(InputStream apkInputStream, File outApkFile, String workspacePath) { mWorkspacePath = workspacePath; mOutApkFile = outApkFile; - mApkPackager = new ApkPackager(apkInputStream, mWorkspacePath); + mApkUnpackUtil = new ApkUnpackUtil(apkInputStream, mWorkspacePath); PFiles.ensureDir(outApkFile.getPath()); } @@ -224,7 +224,7 @@ public class ApkBuilder { GlobalAppContext.post(() -> mProgressCallback.onPrepare(ApkBuilder.this)); } (new File(mWorkspacePath)).mkdirs(); - mApkPackager.unzip(); + mApkUnpackUtil.unzip(); return this; }