### Initialize Dynamic SO Loading Configuration Source: https://github.com/testplanb/dyso/blob/master/README.md This code snippet demonstrates how to initialize the dynamic SO loading configuration using 'DynamicSoLauncher.INSTANCE.initDynamicSoConfig'. It requires a 'Context', the path where downloaded SOs are stored, and a callback function. The callback allows for custom logic, such as version validation, before the SO loading process begins. ```Java // 在合适的时候将自定义路径插入so检索路径 需要使用者自己负责在这个路径上有写入权限 DynamicSoLauncher.INSTANCE.initDynamicSoConfig(this, path, s -> { // 处理一些自定义逻辑 return true; }); ``` -------------------------------- ### Apply Dynamic SO Loading Gradle Plugin Source: https://github.com/testplanb/dyso/blob/master/README.md To enable the bytecode instrumentation for dynamic SO loading, this snippet shows how to apply the 'com.plugins.core' Gradle plugin in the project's 'build.gradle' file. This plugin is responsible for replacing 'System.loadLibrary' calls with the dynamic loading logic. ```Gradle apply plugin: 'com.plugins.core' ``` -------------------------------- ### Manually Load SO Dynamically Source: https://github.com/testplanb/dyso/blob/master/README.md This snippet shows how to manually load a SO library using 'DynamicSoLauncher.INSTANCE.loadSoDynamically'. This method should be called before using any methods from the SO. It accepts either the SO's name or a 'File' object pointing to the SO within the initialized path. ```Java DynamicSoLauncher.INSTANCE.loadSoDynamically(file) ``` -------------------------------- ### Enable Dynamic SO Loading with @DynamicLoad Annotation Source: https://github.com/testplanb/dyso/blob/master/README.md This snippet illustrates how to enable dynamic SO loading for an entire class by applying the '@DynamicLoad' annotation. When this annotation is used, a bytecode instrumentation plugin replaces all 'System.loadLibrary' calls within the class with 'DynamicSoLauncher's internal SO loading mechanism, providing a seamless integration. ```Java //@DynamicLoad public class MainActivity extends AppCompatActivity { ``` -------------------------------- ### Locate Prebuilt NDK Library Source: https://github.com/testplanb/dyso/blob/master/app/src/main/cpp/CMakeLists.txt Searches for a specified prebuilt library (e.g., NDK's log library) and stores its path in a variable. CMake includes system libraries in the search path by default and verifies the library's existence before completing its build. ```CMake find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log) ``` -------------------------------- ### Define Shared Native Libraries Source: https://github.com/testplanb/dyso/blob/master/app/src/main/cpp/CMakeLists.txt Creates and names shared libraries, specifying their source code files. Multiple libraries can be defined, and CMake builds them for you. Gradle automatically packages shared libraries with the APK. ```CMake add_library( native1 SHARED nativeso1.c ) add_library( native2 SHARED nativeso2.c ) add_library( native3 SHARED nativeso3.c ) ``` -------------------------------- ### Link Libraries to Native Targets Source: https://github.com/testplanb/dyso/blob/master/app/src/main/cpp/CMakeLists.txt Specifies which libraries CMake should link to a target library. This can include libraries defined in the current build script, prebuilt third-party libraries, or system libraries, demonstrating a multi-dependency scenario. ```CMake target_link_libraries( # Specifies the target library. native1 # Links the target library to the log library # included in the NDK. ${log-lib}) target_link_libraries( # Specifies the target library. native2 # Links the target library to the log library # included in the NDK. ${log-lib} native1) target_link_libraries( # Specifies the target library. native3 # Links the target library to the log library # included in the NDK. ${log-lib} native2) ``` -------------------------------- ### Set Minimum CMake Version Source: https://github.com/testplanb/dyso/blob/master/app/src/main/cpp/CMakeLists.txt Specifies the minimum required version of CMake to build the native library. This ensures compatibility and access to necessary CMake features. ```CMake cmake_minimum_required(VERSION 3.18.1) ``` -------------------------------- ### Delete Local SOs in Android Project (Method 1: packagingOptions) Source: https://github.com/testplanb/dyso/blob/master/README.md This method demonstrates how to prevent specific SO libraries from being packaged into the APK by adding 'exclude' rules under 'packagingOptions' in the 'build.gradle' file. This is a simple way to remove unwanted SOs during the build process. ```Gradle packagingOptions下增加 exclude 'lib/arm64-v8a/xxx.so' exclude 'lib/armeabi-v7a/xxx.so' ``` -------------------------------- ### Declare CMake Project Name Source: https://github.com/testplanb/dyso/blob/master/app/src/main/cpp/CMakeLists.txt Declares and names the CMake project. This name is used internally by CMake and can influence build artifacts. ```CMake project("nativecpp") ``` -------------------------------- ### Delete Local SOs in Android Project (Method 2: Custom Gradle Task) Source: https://github.com/testplanb/dyso/blob/master/README.md This method provides a more customizable way to delete specific SO libraries from the merged native libraries directory after the 'mergeDebugNativeLibs' task. It defines a 'dynamicSo' task that iterates through the build output and deletes SOs whose names are in the 'deleteSoName' list. This allows for fine-grained control over which SOs are removed. ```Gradle ext { deleteSoName = ["libnativecpptwo.so","libnativecpp.so"] } // 这个是初始化 -配置 -执行阶段中,配置阶段执行的任务之一,完成afterEvaluate就可以得到所有的tasks,从而可以在里面插入我们定制化的数据 task(dynamicSo) { }.doLast { println("dynamicSo insert!!!! ") //projectDir 在哪个project下面,projectDir就是哪个路径 print(getRootProject().findAll()) def file = new File("${projectDir}/build/intermediates/merged_native_libs/debug/out/lib") //默认删除所有的so库 if (file.exists()) { file.listFiles().each { if (it.isDirectory()) { it.listFiles().each { target -> print("file ${target.name}") def compareName = target.name deleteSoName.each { if (compareName.contains(it)) { target.delete() } } } } } } else { print("nil") } } afterEvaluate { print("dynamicSo task start") def customer = tasks.findByName("dynamicSo") def merge = tasks.findByName("mergeDebugNativeLibs") def strip = tasks.findByName("stripDebugDebugSymbols") if (merge != null || strip != null) { customer.mustRunAfter(merge) strip.dependsOn(customer) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.