Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmacd committed Aug 5, 2018
2 parents ffed639 + 9c17b8a commit 2213701
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 153 deletions.
1 change: 1 addition & 0 deletions app/src/full/java/com/topjohnwu/magisk/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Data {
public static int remoteMagiskVersionCode = -1;
public static String magiskLink;
public static String magiskNoteLink;
public static String magiskMD5;
public static String remoteManagerVersionString;
public static int remoteManagerVersionCode = -1;
public static String managerLink;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static void fetchUpdates() {
Data.remoteMagiskVersionCode = getInt(magisk, "versionCode", -1);
Data.magiskLink = getString(magisk, "link", null);
Data.magiskNoteLink = getString(magisk, "note", null);
Data.magiskMD5 = getString(magisk, "md5", null);

JSONObject manager = getJson(json, "app");
Data.remoteManagerVersionString = getString(manager, "version", null);
Expand Down
24 changes: 17 additions & 7 deletions app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
Expand Down Expand Up @@ -136,19 +137,28 @@ public int read(@NonNull byte[] b, int off, int len) throws IOException {
}

private void extractFiles(String arch) throws IOException {
console.add("- Downloading zip");
String filename = Utils.fmt("Magisk-v%s(%d).zip",
Data.remoteMagiskVersionString, Data.remoteMagiskVersionCode);
HttpURLConnection conn = WebService.mustRequest(Data.magiskLink, null);
BufferedInputStream buf = new BufferedInputStream(new ProgressStream(conn));
buf.mark(Integer.MAX_VALUE);
File zip = new File(Download.EXTERNAL_PATH, filename);
zip.getParentFile().mkdirs();
try (OutputStream out = new FileOutputStream(zip)) {
ShellUtils.pump(buf, out);
BufferedInputStream buf;

if (!ShellUtils.checkSum("MD5", zip, Data.magiskMD5)) {
console.add("- Downloading zip");
HttpURLConnection conn = WebService.mustRequest(Data.magiskLink, null);
buf = new BufferedInputStream(new ProgressStream(conn), conn.getContentLength());
buf.mark(Integer.MAX_VALUE);
try (OutputStream out = new FileOutputStream(zip)) {
ShellUtils.pump(buf, out);
}
buf.reset();
conn.disconnect();
} else {
console.add("- Existing zip found");
buf = new BufferedInputStream(new FileInputStream(zip), (int) zip.length());
buf.mark(Integer.MAX_VALUE);
}
conn.disconnect();

console.add("- Extracting files");
try (InputStream in = buf) {
ZipUtils.unzip(in, installDir, arch + "/", true);
Expand Down
4 changes: 2 additions & 2 deletions app/src/full/java/com/topjohnwu/magisk/asyncs/PatchAPK.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.RootUtils;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.ZipUtils;
import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.superuser.io.SuFile;
import com.topjohnwu.superuser.io.SuFileOutputStream;
import com.topjohnwu.utils.JarMap;
import com.topjohnwu.utils.SignAPK;

import java.security.SecureRandom;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -99,7 +99,7 @@ private static boolean patchAndHide() {
JarMap apk = new JarMap(mm.getPackageCodePath());
if (!patchPackageID(apk, Const.ORIG_PKG_NAME, pkg))
return false;
ZipUtils.signZip(apk, new SuFileOutputStream(repack));
SignAPK.sign(apk, new SuFileOutputStream(repack));
} catch (Exception e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.asyncs.PatchAPK;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.ZipUtils;
import com.topjohnwu.utils.JarMap;
import com.topjohnwu.utils.SignAPK;

import java.io.BufferedOutputStream;
import java.io.File;
Expand All @@ -37,7 +37,7 @@ public void onDownloadDone(Context context, Uri uri) {
try {
JarMap apk = new JarMap(orig);
PatchAPK.patchPackageID(apk, Const.ORIG_PKG_NAME, context.getPackageName());
ZipUtils.signZip(apk, new BufferedOutputStream(new FileOutputStream(patch)));
SignAPK.sign(apk, new BufferedOutputStream(new FileOutputStream(patch)));
super.onDownloadDone(context, Uri.fromFile(new File(patch)));
} catch (Exception ignored) { }
});
Expand Down
6 changes: 1 addition & 5 deletions app/src/full/java/com/topjohnwu/magisk/utils/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ public static void unzip(InputStream zip, File folder, String path, boolean junk
public static void signZip(File input, File output) throws Exception {
try (JarMap map = new JarMap(input, false);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(output))) {
signZip(map, out);
SignAPK.sign(map, out);
}
}

public static void signZip(JarMap input, OutputStream output) throws Exception {
SignAPK.signZip(null, null, input, output);
}
}
2 changes: 1 addition & 1 deletion app/src/full/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<string name="direct_install">Прямая установка (Рекомендуется)</string>
<string name="install_inactive_slot">Установка в неактивный слот (После OTA)</string>
<string name="warning">Предупреждение</string>
<string name="install_inactive_slot_msg">Ваше устройство будет принудительно перезагружено в неактивный слот!\nИспользуйте эту опцию только при установке OTA.\nContinue?</string>
<string name="install_inactive_slot_msg">Ваше устройство будет принудительно перезагружено в неактивный слот!\nИспользуйте эту опцию только при установке OTA.\nПродолжить?</string>
<string name="select_method">Выбор способа</string>
<string name="no_boot_file_patch_support">Целевая версия Magisk не поддерживает патчинг boot-образов</string>
<string name="boot_file_patch_msg">Выберите файл ядра (boot) - *.img или *.img.tar</string>
Expand Down
94 changes: 53 additions & 41 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def error(str):
def header(str):
print('\n' + '\033[44m' + str + '\033[0m' + '\n')

def vprint(str):
if args.verbose:
print(str)

# Environment checks
if not sys.version_info >= (3, 5):
error('Requires Python 3.5+')
Expand Down Expand Up @@ -44,6 +48,7 @@ def header(str):
cpu_count = multiprocessing.cpu_count()
gradlew = os.path.join('.', 'gradlew.bat' if os.name == 'nt' else 'gradlew')
archs = ['armeabi-v7a', 'x86']
keystore = 'release-key.jks'

def mv(source, target):
try:
Expand All @@ -54,7 +59,7 @@ def mv(source, target):
def cp(source, target):
try:
shutil.copyfile(source, target)
print('cp: {} -> {}'.format(source, target))
vprint('cp: {} -> {}'.format(source, target))
except:
pass

Expand All @@ -77,8 +82,8 @@ def mkdir_p(path, mode=0o777):
def zip_with_msg(zipfile, source, target):
if not os.path.exists(source):
error('{} does not exist! Try build \'binary\' and \'apk\' before zipping!'.format(source))
print('zip: {} -> {}'.format(source, target))
zipfile.write(source, target)
vprint('zip: {} -> {}'.format(source, target))

def build_all(args):
build_apk(args)
Expand All @@ -94,6 +99,12 @@ def collect_binary():
target = os.path.join('native', 'out', arch, bin)
mv(source, target)

def execv(cmd, redirect=None):
return subprocess.run(cmd, stdout=redirect if redirect != None else STDOUT)

def system(cmd, redirect=None):
return subprocess.run(cmd, shell=True, stdout=redirect if redirect != None else STDOUT)

def build_binary(args):
# If nothing specified, build everything
try:
Expand All @@ -115,7 +126,7 @@ def build_binary(args):

if 'magisk' in targets:
# Magisk is special case as it is a dependency of magiskinit
proc = subprocess.run('{} -C native {} B_MAGISK=1 -j{}'.format(ndk_build, base_flags, cpu_count), shell=True, stdout=STDOUT)
proc = system('{} -C native {} B_MAGISK=1 -j{}'.format(ndk_build, base_flags, cpu_count))
if proc.returncode != 0:
error('Build Magisk binary failed!')
collect_binary()
Expand Down Expand Up @@ -146,7 +157,7 @@ def build_binary(args):
old_plat = True

if old_plat:
proc = subprocess.run('{} -C native {} -j{}'.format(ndk_build, flags, cpu_count), shell=True, stdout=STDOUT)
proc = system('{} -C native {} -j{}'.format(ndk_build, flags, cpu_count))
if proc.returncode != 0:
error('Build binaries failed!')
collect_binary()
Expand All @@ -159,17 +170,37 @@ def build_binary(args):
new_plat = True

if new_plat:
proc = subprocess.run('{} -C native NEW_PLAT=1 {} -j{}'.format(ndk_build, flags, cpu_count), shell=True, stdout=STDOUT)
proc = system('{} -C native NEW_PLAT=1 {} -j{}'.format(ndk_build, flags, cpu_count))
if proc.returncode != 0:
error('Build binaries failed!')
collect_binary()

def sign_zip(unsigned, output, release):
signer_name = 'zipsigner-3.0.jar'
zipsigner = os.path.join('utils', 'build', 'libs', signer_name)

if not os.path.exists(zipsigner):
header('* Building ' + signer_name)
proc = execv([gradlew, 'utils:shadowJar'])
if proc.returncode != 0:
error('Build {} failed!'.format(signer_name))

header('* Signing Zip')

if release:
proc = execv(['java', '-jar', zipsigner, keystore, config['keyStorePass'], config['keyAlias'], config['keyPass'], unsigned, output])
else:
proc = execv(['java', '-jar', zipsigner, unsigned, output])

if proc.returncode != 0:
error('Signing zip failed!')

def sign_apk(source, target):
# Find the latest build tools
build_tool = os.path.join(os.environ['ANDROID_HOME'], 'build-tools',
sorted(os.listdir(os.path.join(os.environ['ANDROID_HOME'], 'build-tools')))[-1])

proc = subprocess.run([os.path.join(build_tool, 'zipalign'), '-vpf', '4', source, target], stdout=subprocess.DEVNULL)
proc = execv([os.path.join(build_tool, 'zipalign'), '-vpf', '4', source, target], subprocess.DEVNULL)
if proc.returncode != 0:
error('Zipalign Magisk Manager failed!')

Expand All @@ -182,8 +213,8 @@ def sign_apk(source, target):
if not apksigner:
error('Cannot find apksigner.jar in Android SDK build tools')

proc = subprocess.run('java -jar {} sign --ks release-key.jks --ks-pass pass:{} --ks-key-alias {} --key-pass pass:{} {}'.format(
apksigner, config['keyStorePass'], config['keyAlias'], config['keyPass'], target), shell=True)
proc = execv(['java', '-jar', apksigner, 'sign', '--ks', keystore, '--ks-pass', 'pass:' + config['keyStorePass'],
'--ks-key-alias', config['keyAlias'], '--key-pass', 'pass:' + config['keyPass'], target])
if proc.returncode != 0:
error('Release sign Magisk Manager failed!')

Expand All @@ -195,10 +226,7 @@ def build_apk(args):
cp(source, target)

if args.release:
if not os.path.exists('release-key.jks'):
error('Please generate a java keystore and place it in \'release-key.jks\'')

proc = subprocess.run('{} app:assembleRelease'.format(gradlew), shell=True, stdout=STDOUT)
proc = execv([gradlew, 'app:assembleRelease'])
if proc.returncode != 0:
error('Build Magisk Manager failed!')

Expand All @@ -219,7 +247,7 @@ def build_apk(args):
with open(release, 'rb') as src:
xz_dump(src, out, 'manager_xz')
else:
proc = subprocess.run('{} app:assembleDebug'.format(gradlew), shell=True, stdout=STDOUT)
proc = execv([gradlew, 'app:assembleDebug'])
if proc.returncode != 0:
error('Build Magisk Manager failed!')

Expand All @@ -234,7 +262,7 @@ def build_apk(args):
header('Output: ' + target)

def build_snet(args):
proc = subprocess.run('{} snet:assembleRelease'.format(gradlew), shell=True, stdout=STDOUT)
proc = execv([gradlew, 'snet:assembleRelease'])
if proc.returncode != 0:
error('Build snet extention failed!')
source = os.path.join('snet', 'build', 'outputs', 'apk', 'release', 'snet-release-unsigned.apk')
Expand Down Expand Up @@ -290,7 +318,7 @@ def zip_main(args):
# META-INF
# update-binary
target = os.path.join('META-INF', 'com', 'google', 'android', 'update-binary')
print('zip: ' + target)
vprint('zip: ' + target)
zipf.writestr(target, gen_update_binary())
# updater-script
source = os.path.join('scripts', 'flash_script.sh')
Expand Down Expand Up @@ -321,7 +349,7 @@ def zip_main(args):
util_func = script.read().replace('#MAGISK_VERSION_STUB',
'MAGISK_VER="{}"\nMAGISK_VER_CODE={}'.format(config['version'], config['versionCode']))
target = os.path.join('common', 'util_functions.sh')
print('zip: ' + source + ' -> ' + target)
vprint('zip: ' + source + ' -> ' + target)
zipf.writestr(target, util_func)
# addon.d.sh
source = os.path.join('scripts', 'addon.d.sh')
Expand All @@ -337,7 +365,7 @@ def zip_main(args):

output = os.path.join(config['outdir'], 'Magisk-v{}.zip'.format(config['version']) if config['prettyName'] else
'magisk-release.zip' if args.release else 'magisk-debug.zip')
sign_adjust_zip(unsigned, output)
sign_zip(unsigned, output, args.release)
header('Output: ' + output)

def zip_uninstaller(args):
Expand All @@ -349,7 +377,7 @@ def zip_uninstaller(args):
# META-INF
# update-binary
target = os.path.join('META-INF', 'com', 'google', 'android', 'update-binary')
print('zip: ' + target)
vprint('zip: ' + target)
zipf.writestr(target, gen_update_binary())
# updater-script
source = os.path.join('scripts', 'magisk_uninstaller.sh')
Expand All @@ -369,7 +397,7 @@ def zip_uninstaller(args):
with open(source, 'r') as script:
# Remove the stub
target = os.path.join('util_functions.sh')
print('zip: ' + source + ' -> ' + target)
vprint('zip: ' + source + ' -> ' + target)
zipf.writestr(target, script.read())

# Prebuilts
Expand All @@ -381,39 +409,21 @@ def zip_uninstaller(args):

output = os.path.join(config['outdir'], 'Magisk-uninstaller-{}.zip'.format(datetime.datetime.now().strftime('%Y%m%d'))
if config['prettyName'] else 'magisk-uninstaller.zip')
sign_adjust_zip(unsigned, output)
sign_zip(unsigned, output, args.release)
header('Output: ' + output)

def sign_adjust_zip(unsigned, output):
signer_name = 'zipsigner-2.2.jar'
jarsigner = os.path.join('utils', 'build', 'libs', signer_name)

if not os.path.exists(jarsigner):
header('* Building ' + signer_name)
proc = subprocess.run('{} utils:shadowJar'.format(gradlew), shell=True, stdout=STDOUT)
if proc.returncode != 0:
error('Build {} failed!'.format(signer_name))

header('* Signing Zip')

signed = tempfile.mkstemp()[1]

proc = subprocess.run(['java', '-jar', jarsigner, unsigned, output])
if proc.returncode != 0:
error('Signing zip failed!')

def cleanup(args):
if len(args.target) == 0:
args.target = ['native', 'java']

if 'native' in args.target:
header('* Cleaning native')
subprocess.run(ndk_build + ' -C native B_MAGISK=1 B_INIT=1 B_BOOT=1 B_BXZ=1 B_BB=1 clean', shell=True, stdout=STDOUT)
system(ndk_build + ' -C native B_MAGISK=1 B_INIT=1 B_BOOT=1 B_BXZ=1 B_BB=1 clean')
shutil.rmtree(os.path.join('native', 'out'), ignore_errors=True)

if 'java' in args.target:
header('* Cleaning java')
subprocess.run('{} app:clean snet:clean utils:clean'.format(os.path.join('.', 'gradlew')), shell=True, stdout=STDOUT)
execv([gradlew, 'app:clean', 'snet:clean', 'utils:clean'])

def parse_config():
c = {}
Expand Down Expand Up @@ -463,7 +473,7 @@ def parse_config():
snet_parser = subparsers.add_parser('snet', help='build snet extention for Magisk Manager')
snet_parser.set_defaults(func=build_snet)

zip_parser = subparsers.add_parser('zip', help='zip and sign Magisk into a flashable zip')
zip_parser = subparsers.add_parser('zip', help='zip Magisk into a flashable zip')
zip_parser.set_defaults(func=zip_main)

uninstaller_parser = subparsers.add_parser('uninstaller', help='create flashable uninstaller')
Expand All @@ -478,5 +488,7 @@ def parse_config():
sys.exit(1)

args = parser.parse_args()
if args.release and not os.path.exists(keystore):
error('Please generate a java keystore and place it in \'{}\''.format(keystore))
STDOUT = None if args.verbose else subprocess.DEVNULL
args.func(args)
3 changes: 2 additions & 1 deletion config.prop.sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ outdir=out
# The default output names are magisk-${release/debug/uninstaller}.zip
prettyName=false

# These pwds are passed to apksigner for release-key.jks. Necessary when building release apks
# Only used when building with release flag
# These passwords are used along with release-key.jks to sign APKs and zips
# keyPass is the pwd for the specified keyAlias
keyStorePass=
keyAlias=
Expand Down
2 changes: 1 addition & 1 deletion utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jar {
shadowJar {
baseName = 'zipsigner'
classifier = null
version = 2.2
version = 3.0
}

buildscript {
Expand Down
Loading

0 comments on commit 2213701

Please sign in to comment.