android-tools icon indicating copy to clipboard operation
android-tools copied to clipboard

ZipAlign

Open timscriptov opened this issue 4 years ago • 11 comments
trafficstars

Please build libzipalign.so

timscriptov avatar Apr 18 '21 18:04 timscriptov

Hi @TimScriptov , you can find zipalign here for android-11.

https://github.com/JonForShort/android-tools/tree/master/build/android-11.0.0_r33/zipalign

JonForShort avatar Apr 23 '21 01:04 JonForShort

Hi @JonForShort

https://developer.android.com/about/versions/10/behavior-changes-10

Untrusted apps that target Android 10 cannot invoke exec() on files within the app's home directory. This execution of files from the writable app home directory is a W^X violation. Apps should load only the binary code that's embedded within an app's APK file.

Need used libzipalign.so for Android 10+

timscriptov avatar Apr 23 '21 12:04 timscriptov

C++:

#include <cstdlib>
#include <jni.h>
#include <zipalign/ZipAlign.h>

using namespace android;

extern "C"
JNIEXPORT jboolean
JNICALL Java_com_android_zipalign_ZipAlign_process
(JNIEnv *env, jclass clazz, jstring inZipFile, jstring outZipFile, jint alignment, jboolean force, jboolean zopfli, jboolean pageAlignSharedLibs) {
    const char *inFileName = env->GetStringUTFChars(inZipFile, nullptr);
    const char *outFileName = env->GetStringUTFChars(outZipFile, nullptr);
    if(!inFileName || !outFileName) {
        return JNI_FALSE;
    }
    if (process(inFileName, outFileName, alignment, force, zopfli, pageAlignSharedLibs) == 0) return JNI_TRUE;
    return JNI_FALSE;
}

extern "C"
JNIEXPORT jboolean
JNICALL Java_com_android_zipalign_ZipAlign_verify
(JNIEnv *env, jclass clazz, jstring inZipFile, jint alignment, jboolean verbose, jboolean pageAlignSharedLibs) {
    const char *inFileName = env->GetStringUTFChars(inZipFile, nullptr);
    if(!inFileName) {
        return JNI_FALSE;
    }
    if(verify(inFileName, alignment, verbose, pageAlignSharedLibs) == 0) return JNI_TRUE;
    return JNI_FALSE;
}

Java:

package com.android.zipalign;

public class ZipAlign {
    static {
        System.loadLibrary("zipalign");
    }

    public static native boolean process(String inZipFile, String outZipFile, int alignment, boolean force, boolean pageAlignSharedLibs);

    public static native boolean verify(String inZipFile, int alignment, boolean verbose, boolean pageAlignSharedLibs);
}

timscriptov avatar Apr 23 '21 12:04 timscriptov

Thanks @TimScriptov . I can add this as well. I am currently working on getting aapt working for android-11, but I'll work on your request next.

Just a quick question regarding the home app restriction, have you tried executing it from your app's cache directory? Technically, this is different than your home directory but just something I was curious about.

JonForShort avatar Apr 24 '21 02:04 JonForShort

For my own reference, this is the target that will need to be built.

https://android.googlesource.com/platform/build/+/master/tools/zipalign/Android.bp#21

JonForShort avatar Apr 24 '21 03:04 JonForShort

Thanks @TimScriptov . I can add this as well. I am currently working on getting aapt working for android-11, but I'll work on your request next.

Just a quick question regarding the home app restriction, have you tried executing it from your app's cache directory? Technically, this is different than your home directory but just something I was curious about.

Thanks, aapt it cool!

I myself have already assembled the code I need, but I'm not sure about 100% of the assembly's performance. I will be looking forward to your further updates.

timscriptov avatar Apr 24 '21 06:04 timscriptov

Just a quick question regarding the home app restriction, have you tried executing it from your app's cache directory? Technically, this is different than your home directory but just something I was curious about.

If TargetSdk is 28 or less, then binaries work.

If TargetSdk is 29 or higher then binaries do not work (Permission denied)

timscriptov avatar Apr 24 '21 06:04 timscriptov

@TimScriptov as far i know in new api you can only execute binaries from app root folder (data/data/com.whatever.idk/files/ if you know sketchware it can execute binaries from root folder with context.getFilesDir() , you can check out the modded source and see how AAPT and AAPT2 get executed : https://github.com/Sketchware-Pro/Sketchware-Pro/blob/75d4a528a8d01ef12647d07ed68cbb835f30de54/app/src/main/java/a/a/a/Dp.java#L116

SectionTN avatar Apr 30 '21 23:04 SectionTN

this work even in API 30

SectionTN avatar Apr 30 '21 23:04 SectionTN

yes, working

SectionTN avatar May 18 '21 03:05 SectionTN

I have already said that you cannot run binaries on API 29 (If targetSdkVersion=29+)

timscriptov avatar May 18 '21 17:05 timscriptov