Skip to content

Commit 766d6b4

Browse files
committed
添加apkbuilder到工程,移除apkapkbuilder依赖
1 parent 5e5dd99 commit 766d6b4

File tree

81 files changed

+9108
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+9108
-2
lines changed

apkbuilder/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

apkbuilder/build.gradle

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion versions.compile
5+
buildToolsVersion versions.buildTool
6+
7+
defaultConfig {
8+
minSdkVersion versions.mini
9+
targetSdkVersion versions.target
10+
versionCode 1
11+
versionName "1.0"
12+
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
15+
}
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
23+
24+
}
25+
26+
dependencies {
27+
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.1-alpha01', {
28+
exclude group: 'com.android.support', module: 'support-annotations'
29+
})
30+
testImplementation 'junit:junit:4.12'
31+
api fileTree(dir: 'libs', include: ['*.jar'])
32+
api files('libs/tiny-sign-0.9.jar')
33+
api files('libs/commons-io-2.5.jar')
34+
}

apkbuilder/libs/commons-io-2.5.jar

204 KB
Binary file not shown.

apkbuilder/libs/tiny-sign-0.9.jar

8.16 KB
Binary file not shown.

apkbuilder/proguard-rules.pro

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in E:\YiBin\eclipse\Android_SDK_windows/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.stardust.autojs.apkbuilder;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.stardust.autojs.apkbuilder.test", appContext.getPackageName());
25+
}
26+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.stardust.autojs.apkbuilder">
3+
4+
<application
5+
android:allowBackup="true"
6+
android:label="@string/app_name"
7+
android:supportsRtl="true">
8+
9+
</application>
10+
11+
</manifest>
2.01 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.stardust.autojs.apkbuilder;
2+
3+
4+
import com.stardust.autojs.apkbuilder.util.StreamUtils;
5+
6+
import org.apache.commons.io.IOUtils;
7+
8+
import java.io.BufferedInputStream;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.File;
11+
import java.io.FileInputStream;
12+
import java.io.FileNotFoundException;
13+
import java.io.FileOutputStream;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.util.ArrayList;
17+
import java.util.Arrays;
18+
import java.util.Collections;
19+
20+
import zhao.arsceditor.ResDecoder.ARSCDecoder;
21+
import zhao.arsceditor.ResDecoder.AXMLDecoder;
22+
23+
/**
24+
* Created by Stardust on 2017/7/1.
25+
*/
26+
27+
public class ApkBuilder {
28+
29+
30+
private File mOutApkFile;
31+
private ApkPackager mApkPackager;
32+
private ManifestEditor mManifestEditor;
33+
private String mWorkspacePath;
34+
private String mArscPackageName;
35+
36+
public ApkBuilder(InputStream apkInputStream, File outApkFile, String workspacePath) {
37+
mOutApkFile = outApkFile;
38+
mWorkspacePath = workspacePath;
39+
mApkPackager = new ApkPackager(apkInputStream, mWorkspacePath);
40+
}
41+
42+
public ApkBuilder(File inFile, File outFile, String workspacePath) throws FileNotFoundException {
43+
this(new FileInputStream(inFile), outFile, workspacePath);
44+
}
45+
46+
public ApkBuilder prepare() throws IOException {
47+
new File(mWorkspacePath).mkdirs();
48+
mApkPackager.unzip();
49+
return this;
50+
}
51+
52+
private File getManifestFile() {
53+
return new File(mWorkspacePath, "AndroidManifest.xml");
54+
}
55+
56+
public ManifestEditor editManifest() throws FileNotFoundException {
57+
mManifestEditor = new ManifestEditor(new FileInputStream(getManifestFile()));
58+
return mManifestEditor;
59+
}
60+
61+
public ApkBuilder setArscPackageName(String packageName) throws IOException {
62+
mArscPackageName = packageName;
63+
return this;
64+
}
65+
66+
67+
public ApkBuilder replaceFile(String relativePath, String newFilePath) throws IOException {
68+
StreamUtils.write(new FileInputStream(newFilePath),
69+
new FileOutputStream(new File(mWorkspacePath, relativePath)));
70+
return this;
71+
}
72+
73+
public ApkBuilder build() throws IOException {
74+
if (mManifestEditor != null) {
75+
mManifestEditor.writeTo(new FileOutputStream(getManifestFile()));
76+
}
77+
if (mArscPackageName != null) {
78+
buildArsc();
79+
}
80+
return this;
81+
}
82+
83+
private void buildArsc() throws IOException {
84+
File oldArsc = new File(mWorkspacePath, "resources.arsc");
85+
File newArsc = new File(mWorkspacePath, "resources.arsc.new");
86+
ARSCDecoder decoder = new ARSCDecoder(new BufferedInputStream(new FileInputStream(oldArsc)), null, false);
87+
FileOutputStream fos = new FileOutputStream(newArsc);
88+
decoder.CloneArsc(fos, mArscPackageName, true);
89+
oldArsc.delete();
90+
newArsc.renameTo(oldArsc);
91+
}
92+
93+
public ApkBuilder sign() throws Exception {
94+
mApkPackager.repackage(mOutApkFile.getPath());
95+
return this;
96+
}
97+
98+
public ApkBuilder cleanWorkspace() {
99+
delete(new File(mWorkspacePath));
100+
return this;
101+
}
102+
103+
private void delete(File file) {
104+
if (file.isFile()) {
105+
file.delete();
106+
return;
107+
}
108+
for (File child : file.listFiles()) {
109+
delete(child);
110+
}
111+
file.delete();
112+
}
113+
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.stardust.autojs.apkbuilder;
2+
3+
import com.stardust.autojs.apkbuilder.util.StreamUtils;
4+
5+
//import net.lingala.zip4j.io.inputstream.ZipInputStream;
6+
//import net.lingala.zip4j.model.LocalFileHeader;
7+
8+
import java.io.File;
9+
import java.io.FileInputStream;
10+
import java.io.FileNotFoundException;
11+
import java.io.FileOutputStream;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.io.OutputStream;
15+
import java.util.zip.ZipEntry;
16+
import java.util.zip.ZipInputStream;
17+
18+
import pxb.android.tinysign.TinySign;
19+
20+
/**
21+
* Created by Stardust on 2017/10/23.
22+
*/
23+
24+
public class ApkPackager {
25+
26+
private InputStream mApkInputStream;
27+
private String mWorkspacePath;
28+
29+
public ApkPackager(InputStream apkInputStream, String workspacePath) {
30+
mApkInputStream = apkInputStream;
31+
mWorkspacePath = workspacePath;
32+
}
33+
34+
public ApkPackager(String apkPath, String workspacePath) throws FileNotFoundException {
35+
mApkInputStream = new FileInputStream(apkPath);
36+
mWorkspacePath = workspacePath;
37+
}
38+
39+
public void unzip() throws IOException {
40+
ZipInputStream zis = new ZipInputStream(mApkInputStream);
41+
for (ZipEntry e = zis.getNextEntry(); e != null; e = zis.getNextEntry()) {
42+
String name = e.getName();
43+
if (!e.isDirectory()) {
44+
File file = new File(mWorkspacePath, name);
45+
System.out.println(file);
46+
file.getParentFile().mkdirs();
47+
FileOutputStream fos = new FileOutputStream(file);
48+
StreamUtils.write(zis, fos);
49+
fos.close();
50+
}
51+
}
52+
zis.close();
53+
}
54+
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+
61+
public void cleanWorkspace() {
62+
63+
}
64+
65+
}

0 commit comments

Comments
 (0)