Skip to content

Commit afaa9bd

Browse files
committed
feat: Download JREs from github
This should help reduce apk size. We still maintain Java 8 due to reasons. All old legacy code for having internal runtimes were not removed because we have no reason to.
1 parent 32d987c commit afaa9bd

4 files changed

Lines changed: 101 additions & 34 deletions

File tree

.github/workflows/android.yml

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,35 @@ jobs:
3838
branch: buildjre8
3939
name: jre8-pojav
4040

41-
- name: Get JRE17
42-
uses: dawidd6/action-download-artifact@v9
43-
with:
44-
workflow: build.yml
45-
path: app_pojavlauncher/src/main/assets/components/jre-new
46-
workflow_conclusion: completed
47-
repo: AngelAuraMC/angelauramc-openjdk-build
48-
branch: buildjre17-21
49-
name: jre17-pojav
50-
51-
- name: Get JRE21
52-
uses: dawidd6/action-download-artifact@v9
53-
with:
54-
workflow: build.yml
55-
path: app_pojavlauncher/src/main/assets/components/jre-21
56-
workflow_conclusion: completed
57-
repo: AngelAuraMC/angelauramc-openjdk-build
58-
branch: buildjre17-21
59-
name: jre21-pojav
60-
61-
- name: Get JRE25
62-
uses: dawidd6/action-download-artifact@v9
63-
with:
64-
workflow: build.yml
65-
path: app_pojavlauncher/src/main/assets/components/jre-25
66-
workflow_conclusion: completed
67-
repo: FCL-Team/Android-OpenJDK-Build
68-
branch: Build_JRE_25
69-
name: jre25-multiarch
41+
# - name: Get JRE17
42+
# uses: dawidd6/action-download-artifact@v9
43+
# with:
44+
# workflow: build.yml
45+
# path: app_pojavlauncher/src/main/assets/components/jre-new
46+
# workflow_conclusion: completed
47+
# repo: AngelAuraMC/angelauramc-openjdk-build
48+
# branch: buildjre17-21
49+
# name: jre17-pojav
50+
#
51+
# - name: Get JRE21
52+
# uses: dawidd6/action-download-artifact@v9
53+
# with:
54+
# workflow: build.yml
55+
# path: app_pojavlauncher/src/main/assets/components/jre-21
56+
# workflow_conclusion: completed
57+
# repo: AngelAuraMC/angelauramc-openjdk-build
58+
# branch: buildjre17-21
59+
# name: jre21-pojav
60+
#
61+
# - name: Get JRE25
62+
# uses: dawidd6/action-download-artifact@v9
63+
# with:
64+
# workflow: build.yml
65+
# path: app_pojavlauncher/src/main/assets/components/jre-25
66+
# workflow_conclusion: completed
67+
# repo: FCL-Team/Android-OpenJDK-Build
68+
# branch: Build_JRE_25
69+
# name: jre25-multiarch
7070

7171
- uses: gradle/actions/setup-gradle@v4
7272
with:

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/NewJREUtil.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
package net.kdt.pojavlaunch;
22

33
import static net.kdt.pojavlaunch.Architecture.archAsString;
4+
import static net.kdt.pojavlaunch.Architecture.getDeviceArchitecture;
5+
import static net.kdt.pojavlaunch.Tools.NATIVE_LIB_DIR;
6+
import static net.kdt.pojavlaunch.Tools.downloadFile;
7+
import static net.kdt.pojavlaunch.Tools.isOnline;
48

59
import android.app.Activity;
610
import android.content.res.AssetManager;
711
import android.util.Log;
812

13+
import androidx.appcompat.app.AlertDialog;
14+
15+
import com.kdt.mcgui.ProgressLayout;
16+
917
import net.kdt.pojavlaunch.multirt.MultiRTUtils;
1018
import net.kdt.pojavlaunch.multirt.Runtime;
19+
import net.kdt.pojavlaunch.progresskeeper.DownloaderProgressWrapper;
20+
import net.kdt.pojavlaunch.progresskeeper.ProgressKeeper;
21+
import net.kdt.pojavlaunch.utils.DownloadUtils;
22+
import net.kdt.pojavlaunch.utils.JREUtils;
1123
import net.kdt.pojavlaunch.utils.MathUtils;
1224
import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles;
1325
import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile;
1426

27+
import java.io.BufferedInputStream;
28+
import java.io.File;
29+
import java.io.FileInputStream;
1530
import java.io.IOException;
31+
import java.io.InputStream;
1632
import java.util.Arrays;
1733
import java.util.List;
1834

@@ -84,7 +100,7 @@ public static boolean installNewJreIfNeeded(Activity activity, JMinecraftVersion
84100
// Check whether the selection is an internal runtime
85101
InternalRuntime internalRuntime = getInternalRuntime(runtime);
86102
// If it is, check if updates are available from the APK file
87-
if(internalRuntime != null) {
103+
if (internalRuntime != null) {
88104
// Not calling showRuntimeFail on failure here because we did, technically, find the compatible runtime
89105
return checkInternalRuntime(assetManager, internalRuntime);
90106
}
@@ -100,6 +116,18 @@ public static boolean installNewJreIfNeeded(Activity activity, JMinecraftVersion
100116
nearestInternalRuntime, nearestInstalledRuntime, (value)->value.rank
101117
);
102118

119+
// Check if the selected runtime actually exists in the APK, else download it
120+
// If it isn't InternalRuntime then it wasn't in the apk in the first place!
121+
if (selectedRankedRuntime.value instanceof InternalRuntime)
122+
if (!checkInternalRuntime(assetManager, (InternalRuntime) selectedRankedRuntime.value)) {
123+
if (nearestInstalledRuntime == null) // If this was non-null then it would be a valid runtime and we can leave it be
124+
tryDownloadRuntime(activity, gameRequiredVersion);
125+
// This means the internal runtime didn't extract so let's use installed instead
126+
// This also refreshes it so after the runtime download, it can find the new runtime
127+
selectedRankedRuntime = getNearestInstalledRuntime(gameRequiredVersion);
128+
}
129+
130+
103131
// No possible selections
104132
if(selectedRankedRuntime == null) {
105133
showRuntimeFail(activity, versionInfo);
@@ -141,6 +169,46 @@ private static void showRuntimeFail(Activity activity, JMinecraftVersionList.Ver
141169
activity.getString(R.string.multirt_nocompatiblert, verInfo.javaVersion.majorVersion));
142170
}
143171

172+
public static boolean isValidJavaVersion(int version) {
173+
for (InternalRuntime javaVersion : InternalRuntime.values()) {
174+
if (javaVersion.majorVersion == version) {
175+
return true;
176+
}
177+
}
178+
return false;
179+
}
180+
181+
private static String getJreSource(int javaVersion, String arch){
182+
return String.format("https://github.com/AngelAuraMC/angelauramc-openjdk-build/releases/latest/download/jre%s-android-%s.tar.xz", javaVersion, arch);
183+
}
184+
/**
185+
* @return whether installation was successful or not
186+
*/
187+
private static boolean tryDownloadRuntime(Activity activity, int gameRequiredVersion){
188+
if (!isOnline(activity)) return false;
189+
String arch = archAsString(getDeviceArchitecture());
190+
if (!isValidJavaVersion(gameRequiredVersion)) return false;
191+
try {
192+
File outputFile = new File(Tools.DIR_CACHE, String.format("jre%s-android-%s.tar.xz", gameRequiredVersion, arch));
193+
DownloaderProgressWrapper monitor = new DownloaderProgressWrapper(R.string.newdl_downloading_jre_runtime,
194+
ProgressLayout.UNPACK_RUNTIME);
195+
monitor.extraString = Integer.toString(gameRequiredVersion);
196+
DownloadUtils.downloadFileMonitored(
197+
getJreSource(gameRequiredVersion, arch),
198+
outputFile,
199+
null,
200+
monitor
201+
);
202+
String jreName = "External-" + gameRequiredVersion;
203+
MultiRTUtils.installRuntimeNamed(NATIVE_LIB_DIR, new FileInputStream(outputFile), jreName);
204+
MultiRTUtils.postPrepare(jreName);
205+
outputFile.delete();
206+
} catch (IOException e) {
207+
throw new RuntimeException("Failed to download Java "+gameRequiredVersion+" for "+arch, e);
208+
}
209+
return true;
210+
}
211+
144212
private enum InternalRuntime {
145213
JRE_17(17, "Internal-17", "components/jre-new"),
146214
JRE_21(21, "Internal-21", "components/jre-21"),

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/MinecraftDownloader.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ private void downloadGame(Activity activity, JMinecraftVersionList.Version verIn
141141
mDownloaderThreadException = new AtomicReference<>(null);
142142
mUseFileCounter = false;
143143

144-
if(!downloadAndProcessMetadata(activity, verInfo, versionName)) {
145-
throw new RuntimeException(activity.getString(R.string.exception_failed_to_unpack_jre17));
146-
}
144+
downloadAndProcessMetadata(activity, verInfo, versionName);
147145

148146
ArrayBlockingQueue<Runnable> taskQueue =
149147
new ArrayBlockingQueue<>(mScheduledDownloadTasks.size(), false);
@@ -293,7 +291,7 @@ private boolean downloadAndProcessMetadata(Activity activity, JMinecraftVersionL
293291
}
294292

295293
if(activity != null && !NewJREUtil.installNewJreIfNeeded(activity, verInfo)){
296-
return false;
294+
throw new RuntimeException(activity.getString(R.string.exception_failed_to_unpack_jre17));
297295
}
298296

299297
JAssets assets = downloadAssetsIndex(verInfo);

app_pojavlauncher/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@
381381
<string name="preference_vsync_in_zink_description">Allows the launcher to use internal system APIs to enable V-Sync for Zink. Turn this off if your launcher suddenly crashes with Zink after a system update.</string>
382382
<string name="exception_failed_to_unpack_jre17">Failed to install JRE 17</string>
383383
<string name="newdl_starting">Reading game metadata…</string>
384+
<string name="newdl_downloading_jre_runtime">Downloading Java %s runtime (%.2f/%.2f MB)</string>
384385
<string name="newdl_downloading_metadata">Downloading game metadata (%s)</string>
385386
<string name="newdl_downloading_game_files">Downloading game… (%d/%d, %.2f MB/s)</string>
386387
<string name="newdl_downloading_game_files_size">Downloading game… (%.2f/%.2f MB, %.2f MB/s)</string>

0 commit comments

Comments
 (0)