jni作为lib使用,优化编译

This commit is contained in:
2025-04-10 18:54:02 +08:00
parent ab69c535e4
commit 7007425199
26 changed files with 164 additions and 28 deletions

View File

@@ -21,7 +21,8 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk { //可以根据需要来自行选择并添加对应cpu类型的.so库。
ndk {
//可以根据需要来自行选择并添加对应cpu类型的.so库。
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
// 还可以添加 'armeabi', 'x86', 'x86_64', 'mips', 'mips64'
}
@@ -42,12 +43,6 @@ android {
}
}
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
}
}
signingConfigs {
own {
storeFile file("keystore/ttstd.jks")
@@ -120,6 +115,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(path: ':ttutils')
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

View File

@@ -2,9 +2,6 @@ package com.example.mir4updater.activity.main;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
@@ -24,18 +21,8 @@ import com.example.mir4updater.bean.FileItem;
import com.example.mir4updater.config.CommonConfig;
import com.example.mir4updater.databinding.ActivityMainBinding;
import com.example.mir4updater.utils.HashUtils;
import com.example.mir4updater.utils.NativeUtil;
import com.example.mir4updater.utils.PermissionUtils;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.play.core.appupdate.AppUpdateInfo;
import com.google.android.play.core.appupdate.AppUpdateManager;
import com.google.android.play.core.appupdate.AppUpdateManagerFactory;
import com.google.android.play.core.install.model.AppUpdateType;
import com.google.android.play.core.install.model.UpdateAvailability;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.Toaster;
import com.trello.rxlifecycle4.RxLifecycle;

View File

@@ -7,11 +7,10 @@ import android.util.Log;
import com.example.mir4updater.base.mvvm.BaseViewModel;
import com.example.mir4updater.config.CommonConfig;
import com.example.mir4updater.databinding.ActivityMainBinding;
import com.example.mir4updater.utils.NativeUtil;
import com.ttstd.ttutils.NativeUtil;
import com.trello.rxlifecycle4.android.ActivityEvent;
import java.io.File;
import java.util.Arrays;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
@@ -47,7 +46,7 @@ public class MainViewModel extends BaseViewModel<ActivityMainBinding, ActivityEv
Log.e("patchAPK", "mAppUpdateManager: Package: " + packageName + ", Path: " + apkPath);
File file = new File("/sdcard");
long time = System.currentTimeMillis();
emitter.onNext(NativeUtil.patchAPK(apkPath, "/sdcard/mir4_update.xapk", "/sdcard/mir4.patch"));
emitter.onNext(NativeUtil.patchApk(apkPath, "/sdcard/mir4_update.xapk", "/sdcard/mir4.patch"));
Log.e(TAG, "subscribe: " + (System.currentTimeMillis() - time) + "ms");
emitter.onComplete();
}

View File

@@ -1,2 +1,3 @@
rootProject.name='MIR4Updater'
include ':app'
include ':ttutils'

1
ttutils/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -10,7 +10,10 @@ cmake_minimum_required(VERSION 3.4.1)
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
include_directories(src/main/jni/bzip)
set(CMAKE_CXX_FLAGS_RELEASE "-O3") # Release
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/jniLibs/${ANDROID_ABI})
#include_directories(src/main/jni/bzip)
# bzip2
file(GLOB bzip_sources src/main/jni/bzip/*.c)
@@ -22,6 +25,7 @@ add_library( # Sets the name of the library.
# Provides a relative path to your source file(s).
src/main/jni/ttutils.c
src/main/jni/ttutilscpp.cpp
src/main/jni/bspatch.c
src/main/jni/bsdiff.c

51
ttutils/build.gradle Normal file
View File

@@ -0,0 +1,51 @@
apply plugin: 'com.android.library'
android {
compileSdkVersion 34
// buildToolsVersion "34.0.0"
defaultConfig {
minSdkVersion 24
targetSdkVersion 34
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
ndk {
moduleName "ttutils"
//armeabi not supported for platform
//1.降低NDK版本至r16b以下。
//2.删除不再受支持的cpu架构相关配置。
// build.gradle中移除armeabi mips等
//选择要添加的对应 cpu 类型的 .so 库。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
// 还可以添加 'armeabi', 'x86', 'x86_64', 'mips', 'mips64'
}
}
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}

View File

21
ttutils/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,27 @@
package com.ttstd.ttutils;
import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.ttstd.ttutils.test", appContext.getPackageName());
}
}

View File

@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ttstd.ttutils" />

View File

@@ -1,4 +1,4 @@
package com.example.mir4updater.utils;
package com.ttstd.ttutils;
public class NativeUtil {
static {
@@ -11,5 +11,6 @@ public class NativeUtil {
* @param newApkFile 新APK文件路径存储生成的APK的路径
* @param patchFile 差异文件
*/
public native static int patchAPK(String oldApkFile,String newApkFile,String patchFile);
public native static int patchApk(String oldApkFile,String newApkFile,String patchFile);
public native static int patchApkCpp(String oldApkFile,String newApkFile,String patchFile);
}

View File

@@ -13,7 +13,7 @@
extern int bspatch_main(int argc, char *argv[]);
JNIEXPORT jint JNICALL
Java_com_example_mir4updater_utils_NativeUtil_patchAPK(JNIEnv *env, jclass clazz,
Java_com_ttstd_ttutils_NativeUtil_patchApk(JNIEnv *env, jclass clazz,
jstring old_path, jstring new_path, jstring patch_path) {
LOGI("patchAPK");
const char *argv[4] = {"bspatch",
@@ -27,4 +27,3 @@ Java_com_example_mir4updater_utils_NativeUtil_patchAPK(JNIEnv *env, jclass clazz
(*env)->ReleaseStringUTFChars(env,patch_path, argv[3]);
return result;
}

View File

@@ -0,0 +1,30 @@
//
// Created by TT on 2025/4/9.
//
// 日志打印
#include <android/log.h>
#define LOG_TAG "bspatch_jni"
#define LOGI(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#include <jni.h>
#include <unistd.h>
#include <stdlib.h>
//extern "C"
//JNIEXPORT jint JNICALL
//Java_com_ttstd_ttutils_NativeUtil_patchApkCpp(JNIEnv *env, jclass clazz,
// jstring old_path, jstring new_path,
// jstring patch_path) {
// LOGI("patchAPK");
// const char *argv[4] = {"bspatch",
// env->GetStringUTFChars(old_path, 0),
// env->GetStringUTFChars(new_path, 0),
// env->GetStringUTFChars(patch_path, 0)};
// int result = bspatch_main(4, (char **) argv); // 调用修改后的函数
// // 释放字符串资源
// env->ReleaseStringUTFChars(old_path, argv[1]);
// env->ReleaseStringUTFChars(new_path, argv[2]);
// env->ReleaseStringUTFChars(patch_path, argv[3]);
// return result;
//}

View File

@@ -0,0 +1,17 @@
package com.ttstd.ttutils;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}