Browse Source

脚本更新

Alex 1 year ago
parent
commit
639853ded6

+ 1 - 0
app/build.gradle

@@ -63,4 +63,5 @@ dependencies {
     androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
     debugImplementation 'androidx.compose.ui:ui-tooling'
     debugImplementation 'androidx.compose.ui:ui-test-manifest'
+    implementation project(':cli')
 }

+ 1 - 0
build.gradle

@@ -2,4 +2,5 @@
 plugins {
 id 'com.android.application' version '8.2.2' apply false
     id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
+    id 'com.android.library' version '8.2.2' apply false
 }

+ 1 - 0
cli/.gitignore

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

+ 46 - 0
cli/build.gradle

@@ -0,0 +1,46 @@
+plugins {
+    id 'com.android.library'
+}
+
+android {
+    namespace 'com.eastpolar.cli'
+    compileSdk 34
+
+    defaultConfig {
+        minSdk 24
+
+        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
+        consumerProguardFiles "consumer-rules.pro"
+        externalNativeBuild {
+            cmake {
+                cppFlags "-std=c++17"
+            }
+        }
+    }
+
+    buildTypes {
+        release {
+            minifyEnabled false
+            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
+        }
+    }
+    externalNativeBuild {
+        cmake {
+            path "src/main/cpp/CMakeLists.txt"
+            version "3.22.1"
+        }
+    }
+    compileOptions {
+        sourceCompatibility JavaVersion.VERSION_1_8
+        targetCompatibility JavaVersion.VERSION_1_8
+    }
+}
+
+dependencies {
+
+    implementation 'androidx.appcompat:appcompat:1.6.1'
+    implementation 'com.google.android.material:material:1.11.0'
+    testImplementation 'junit:junit:4.13.2'
+    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
+    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
+}

+ 0 - 0
cli/consumer-rules.pro


+ 21 - 0
cli/proguard-rules.pro

@@ -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

+ 26 - 0
cli/src/androidTest/java/com/eastpolar/cli/ExampleInstrumentedTest.java

@@ -0,0 +1,26 @@
+package com.eastpolar.cli;
+
+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.eastpolar.cli.test", appContext.getPackageName());
+    }
+}

+ 4 - 0
cli/src/main/AndroidManifest.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+
+</manifest>

+ 37 - 0
cli/src/main/cpp/CMakeLists.txt

@@ -0,0 +1,37 @@
+# For more information about using CMake with Android Studio, read the
+# documentation: https://d.android.com/studio/projects/add-native-code.html.
+# For more examples on how to use CMake, see https://github.com/android/ndk-samples.
+
+# Sets the minimum CMake version required for this project.
+cmake_minimum_required(VERSION 3.22.1)
+
+# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
+# Since this is the top level CMakeLists.txt, the project name is also accessible
+# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
+# build script scope).
+project("cli")
+
+# Creates and names a library, sets it as either STATIC
+# or SHARED, and provides the relative paths to its source code.
+# You can define multiple libraries, and CMake builds them for you.
+# Gradle automatically packages shared libraries with your APK.
+#
+# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
+# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
+# is preferred for the same purpose.
+#
+# In order to load a library into your app from Java/Kotlin, you must call
+# System.loadLibrary() and pass the name of the library defined here;
+# for GameActivity/NativeActivity derived applications, the same library name must be
+# used in the AndroidManifest.xml file.
+add_library(${CMAKE_PROJECT_NAME} SHARED
+        # List C/C++ source files with relative paths to this CMakeLists.txt.
+        cli.cpp)
+
+# Specifies libraries CMake should link to your target library. You
+# can link libraries from various origins, such as libraries defined in this
+# build script, prebuilt third-party libraries, or Android system libraries.
+target_link_libraries(${CMAKE_PROJECT_NAME}
+        # List libraries link to the target library
+        android
+        log)

+ 55 - 0
cli/src/main/cpp/cli.cpp

@@ -0,0 +1,55 @@
+#include <jni.h>
+#include <string>
+#include <cstdio>
+#include <iostream>
+
+extern "C" JNIEXPORT jstring JNICALL
+Java_com_eastpolar_cli_NativeLib_stringFromJNI(
+        JNIEnv *env,
+        jobject /* this */) {
+    std::string hello = "Hello from C++";
+    return env->NewStringUTF(hello.c_str());
+}
+
+//纯c++,读取本地文件的方法,并且打印出来
+extern "C"
+JNIEXPORT _jbyteArray *JNICALL
+readFile(JNIEnv *env,
+         const char *filePath) {
+    //获取log并用于打印
+    jclass cls = env->FindClass("android/util/Log");
+    jmethodID mid = env->GetStaticMethodID(cls, "i", "(Ljava/lang/String;Ljava/lang/String;)I");
+    jstring tag = env->NewStringUTF("NativeLib");
+    jstring msg = env->NewStringUTF(filePath);
+    env->CallStaticIntMethod(cls, mid, tag, msg);
+
+    FILE *file = fopen(filePath, "r");
+    if (file == NULL) {
+        return nullptr;
+    }
+    // 定位到文件末尾获取大小
+    fseek(file, 0, SEEK_END);
+    long size = ftell(file); // 使用 long 接收 ftell 的返回值
+    printf("File size: %ld\n", size);
+    rewind(file); // 将文件指针重新定位到文件开头
+
+    std::string buffers; // 使用 std::string 来动态追加内容
+    char buffer[1024];
+    // 将所有的buffer写到一个对象中
+    while (fgets(buffer, sizeof(buffer), file)) {
+        printf("%s", buffer);
+        buffers += buffer; // 使用 += 运算符追加内容
+    }
+    fclose(file);
+
+    // 可选:打印或处理 buffers 中的全部内容
+    std::cout << "File content: " << buffers << std::endl;
+    env->CallStaticIntMethod(cls, mid, tag, env->NewStringUTF(buffers.c_str()));
+
+    //将buffers写到内存中,并且返回地址内存中的byte地址
+    jbyteArray byteArray = env->NewByteArray(buffers.size());
+    env->SetByteArrayRegion(byteArray, 0, buffers.size(), (jbyte *) buffers.c_str());
+    return byteArray;
+
+
+}

+ 15 - 0
cli/src/main/java/com/eastpolar/cli/NativeLib.java

@@ -0,0 +1,15 @@
+package com.eastpolar.cli;
+
+public class NativeLib {
+
+    // Used to load the 'cli' library on application startup.
+    static {
+        System.loadLibrary("cli");
+    }
+
+    /**
+     * A native method that is implemented by the 'cli' native library,
+     * which is packaged with this application.
+     */
+    public native String stringFromJNI();
+}

+ 17 - 0
cli/src/test/java/com/eastpolar/cli/ExampleUnitTest.java

@@ -0,0 +1,17 @@
+package com.eastpolar.cli;
+
+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);
+    }
+}

+ 1 - 0
settings.gradle

@@ -15,3 +15,4 @@ dependencyResolutionManagement {
 
 rootProject.name = "HookCli"
 include ':app'
+include ':cli'