Skip to content

Commit 33a5cf0

Browse files
committed
Improve windows compatibility
1 parent 23097aa commit 33a5cf0

File tree

10 files changed

+89
-45
lines changed

10 files changed

+89
-45
lines changed

nmm-protect/apkprotect/src/main/java/com/nmmedit/apkprotect/ApkProtect.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void run() throws IOException {
159159
private void addFileToZip(ZipOutputStream zipOutput, File file, ZipEntry zipEntry) throws IOException {
160160
zipOutput.putNextEntry(zipEntry);
161161
try (FileInputStream input = new FileInputStream(file);) {
162-
copyStream(input, zipOutput);
162+
FileUtils.copyStream(input, zipOutput);
163163
}
164164
zipOutput.closeEntry();
165165
}
@@ -227,18 +227,18 @@ private static List<String> getAbis(File apk) throws IOException {
227227
if (abis.isEmpty()) {
228228
//默认只生成armeabi-v7a
229229
ArrayList<String> abi = new ArrayList<>();
230-
if(Prefs.isArm()) {
230+
if (Prefs.isArm()) {
231231
abi.add("armeabi-v7a");
232232
}
233-
if(Prefs.isArm64()) {
233+
if (Prefs.isArm64()) {
234234
abi.add("arm64-v8a");
235235
}
236236

237-
if(Prefs.isX86()) {
237+
if (Prefs.isX86()) {
238238
abi.add("x86");
239239
}
240240

241-
if(Prefs.isX64()) {
241+
if (Prefs.isX64()) {
242242
abi.add("x86_64");
243243
}
244244
return abi;
@@ -247,10 +247,19 @@ private static List<String> getAbis(File apk) throws IOException {
247247
}
248248

249249
private void generateCSources(String packageName) throws IOException {
250-
final List<File> cSources = ApkUtils.extractFiles(
251-
new File(FileUtils.getHomePath(), "tools/vmsrc.zip"), ".*", apkFolders.getDex2cSrcDir()
250+
final File vmsrcFile = new File(FileUtils.getHomePath(), "tools/vmsrc.zip");
251+
if (!vmsrcFile.exists()) {
252+
vmsrcFile.getParentFile().mkdirs();
253+
//copy vmsrc.zip to external directory
254+
try (
255+
InputStream inputStream = ApkProtect.class.getResourceAsStream("/vmsrc.zip");
256+
final FileOutputStream outputStream = new FileOutputStream(vmsrcFile);
257+
) {
258+
FileUtils.copyStream(inputStream, outputStream);
259+
}
260+
}
261+
final List<File> cSources = ApkUtils.extractFiles(vmsrcFile, ".*", apkFolders.getDex2cSrcDir());
252262

253-
);
254263
//处理指令及apk验证,生成新的c文件
255264
for (File source : cSources) {
256265
if (source.getName().endsWith("DexOpcodes.h")) {
@@ -576,7 +585,7 @@ private static void zipCopy(ZipInputStream zipInputStream, File tempDir, ZipOutp
576585
zipOutputStream.putNextEntry(zipEntry);
577586

578587
try (final FileInputStream fileIn = new FileInputStream(fileObj.file)) {
579-
copyStream(fileIn, zipOutputStream);
588+
FileUtils.copyStream(fileIn, zipOutputStream);
580589
}
581590
zipOutputStream.closeEntry();
582591
}
@@ -613,13 +622,6 @@ private static long copyStreamCalcCrc32(InputStream in, OutputStream out) throws
613622
return crc32.getValue();
614623
}
615624

616-
private static void copyStream(InputStream in, OutputStream out) throws IOException {
617-
byte[] buf = new byte[4 * 1024];
618-
int len;
619-
while ((len = in.read(buf)) != -1) {
620-
out.write(buf, 0, len);
621-
}
622-
}
623625

624626
private static class FileObj {
625627
private final File file;

nmm-protect/apkprotect/src/main/java/com/nmmedit/apkprotect/BuildNativeLib.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,10 @@ public String getAbi() {
125125
return abi;
126126
}
127127

128-
public String getVmLibOutputDir() {
129-
return new File(new File(getProjectHome(), ".cxx/cmake/Release"), abi + File.separator + "vm").getAbsolutePath();
130-
}
131-
132128
public String getLibOutputDir() {
133129
return new File(new File(getProjectHome(), "obj"), abi).getAbsolutePath();
134130
}
135131

136-
public String getVpLibOutputDir() {
137-
return new File(new File(getProjectHome(), ".cxx/cmake/Release"), abi).getAbsolutePath();
138-
}
139132

140133
//camke --build <BuildPath>
141134
public String getBuildPath() {
@@ -148,16 +141,16 @@ public String getStripBinaryPath() {
148141
switch (abi) {
149142
case "armeabi-v7a":
150143
return new File(getNdkHome(), "/toolchains/arm-linux-androideabi-4.9/prebuilt/" +
151-
Prefs.ndkToolchains() + "/bin/arm-linux-androideabi-strip").getAbsolutePath();
144+
Prefs.getOsName() + "/bin/arm-linux-androideabi-strip").getAbsolutePath();
152145
case "arm64-v8a":
153146
return new File(getNdkHome(), "/toolchains/aarch64-linux-android-4.9/prebuilt/" +
154-
Prefs.ndkToolchains() + "/bin/aarch64-linux-android-strip").getAbsolutePath();
147+
Prefs.getOsName() + "/bin/aarch64-linux-android-strip").getAbsolutePath();
155148
case "x86":
156149
return new File(getNdkHome(), "/toolchains/x86-4.9/prebuilt/" +
157-
Prefs.ndkToolchains() + "/bin/i686-linux-android-strip").getAbsolutePath();
150+
Prefs.getOsName() + "/bin/i686-linux-android-strip").getAbsolutePath();
158151
case "x86_64":
159152
return new File(getNdkHome(), "/toolchains/x86_64-4.9/prebuilt/" +
160-
Prefs.ndkToolchains() + "/bin/x86_64-linux-android-strip").getAbsolutePath();
153+
Prefs.getOsName() + "/bin/x86_64-linux-android-strip").getAbsolutePath();
161154
}
162155
//不支持arm和x86以外的abi
163156
throw new RuntimeException("Unsupported abi " + abi);
@@ -194,8 +187,8 @@ public List<String> getCmakeArguments() {
194187
//最后输出的so文件
195188
public List<File> getSharedObjectFile() {
196189
return Arrays.asList(
197-
new File(getVmLibOutputDir(), "libnmmvm.so"),
198-
new File(getVpLibOutputDir(), "libnmmp.so")
190+
new File(getLibOutputDir(), "libnmmvm.so"),
191+
new File(getLibOutputDir(), "libnmmp.so")
199192
);
200193
}
201194

nmm-protect/apkprotect/src/main/java/com/nmmedit/apkprotect/data/Prefs.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,34 @@
33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
55
import com.nmmedit.apkprotect.data.config.Config;
6-
import com.nmmedit.apkprotect.data.config.Constants;
76
import com.nmmedit.apkprotect.util.FileUtils;
7+
import com.nmmedit.apkprotect.util.OsDetector;
88

9+
import java.io.File;
10+
import java.io.FileOutputStream;
911
import java.io.IOException;
12+
import java.io.InputStream;
1013
import java.nio.charset.StandardCharsets;
1114

1215
public class Prefs {
16+
public static final String CONFIG_PATH = new File(FileUtils.getHomePath(), "tools/config.json").getAbsolutePath();
17+
1318
public static Config config() {
19+
final File configFile = new File(CONFIG_PATH);
20+
if (!configFile.exists()) {
21+
configFile.getParentFile().mkdirs();
22+
try (
23+
final InputStream inputStream = Prefs.class.getResourceAsStream("/" + (OsDetector.isWindows() ? "config-windows.json" : "config.json"));
24+
final FileOutputStream outputStream = new FileOutputStream(configFile);
25+
) {
26+
FileUtils.copyStream(inputStream, outputStream);
27+
} catch (IOException e) {
28+
}
29+
}
1430
GsonBuilder builder = new GsonBuilder();
1531
Gson gson = builder.create();
1632
try {
17-
String content = FileUtils.readFile(Constants.CONFIG_PATH, StandardCharsets.UTF_8);
33+
String content = FileUtils.readFile(CONFIG_PATH, StandardCharsets.UTF_8);
1834
return gson.fromJson(content, Config.class);
1935
} catch (IOException e) {
2036
e.printStackTrace();
@@ -50,7 +66,7 @@ public static String ndkPath() {
5066
return config().path.ndk;
5167
}
5268

53-
public static String ndkToolchains() {
54-
return config().ndk.toolchains;
69+
public static String getOsName() {
70+
return config().ndk.osName;
5571
}
5672
}

nmm-protect/apkprotect/src/main/java/com/nmmedit/apkprotect/data/config/Constants.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

nmm-protect/apkprotect/src/main/java/com/nmmedit/apkprotect/data/config/NdkConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import com.google.gson.annotations.SerializedName;
44

55
public class NdkConfig {
6-
@SerializedName("toolchains")
7-
public String toolchains;
6+
@SerializedName("os_name")
7+
public String osName;
88
}

nmm-protect/apkprotect/src/main/java/com/nmmedit/apkprotect/util/FileUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
57
import java.net.URISyntaxException;
68
import java.nio.charset.Charset;
79
import java.nio.file.Files;
@@ -28,4 +30,12 @@ public static String readFile(String path, Charset encoding) throws IOException
2830
byte[] encoded = Files.readAllBytes(Paths.get(path));
2931
return new String(encoded, encoding);
3032
}
33+
34+
public static void copyStream(InputStream in, OutputStream out) throws IOException {
35+
byte[] buf = new byte[4 * 1024];
36+
int len;
37+
while ((len = in.read(buf)) != -1) {
38+
out.write(buf, 0, len);
39+
}
40+
}
3141
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.nmmedit.apkprotect.util;
2+
3+
public class OsDetector {
4+
5+
public static boolean isWindows() {
6+
final String osName = System.getProperty("os.name");
7+
if (osName == null) {
8+
return false;
9+
}
10+
return osName.toLowerCase().contains("windows");
11+
}
12+
13+
}

nmm-protect/out/artifacts/nmm_protect_jar/tools/config.json renamed to nmm-protect/apkprotect/src/main/resources/config-windows.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
// Indicate under which platform the NDK is installed
1616
"ndk": {
1717
// For Linux: linux-x86_64
18-
"toolchains":"windows-x86_64" // E:\Android\Sdk\ndk\22.0.7026061\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin
18+
"os_name":"windows-x86_64" // E:\Android\Sdk\ndk\22.0.7026061\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin
1919
}
2020
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
// Choose under which architecture you need to build C++ libraries
3+
"abi": {
4+
"armeabi-v7a": true,
5+
"arm64-v8a": false,
6+
"x86": true,
7+
"x86_64": false
8+
},
9+
// If you do not have environment variables set, enter the path to SDK, NDK and CMAKE
10+
"path": {
11+
"sdk": "/opt/android-sdk",
12+
"cmake": "/opt/android-sdk/cmake/3.18.1",
13+
"ndk": "/opt/android-sdk/ndk/22.1.7171670"
14+
},
15+
// Indicate under which platform the NDK is installed
16+
"ndk": {
17+
"os_name": "linux-x86_64"
18+
}
19+
}
-40.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)