diff --git a/README.md b/README.md index 6ce58ba..0101266 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat) ![](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat) -![](https://img.shields.io/badge/release-1.0.1-red.svg?style=flat) +![](https://img.shields.io/badge/release-1.0.2-red.svg?style=flat) ![](https://img.shields.io/badge/Android-4.1%20--%2011-blue.svg?style=flat) ![](https://img.shields.io/badge/arch-armeabi--v7a%20%7C%20arm64--v8a%20%7C%20x86%20%7C%20x86__64-blue.svg?style=flat) @@ -41,76 +41,45 @@ If xDL is compiled into an independent dynamic library: ## Usage +xDL uses [Prefab](https://google.github.io/prefab/) package format, which is supported by Android Gradle Plugin 4.0+. + +More information: [Using native dependencies](https://developer.android.com/studio/build/native-dependencies) + ### 1. Add dependency in build.gradle ```Gradle -ext { - XDL_VERSION = '1.0.1' +android { + buildFeatures { + prefab true + } } dependencies { - implementation "io.hexhacking.xdl:xdl-android-lib:${XDL_VERSION}" + implementation 'io.hexhacking.xdl:xdl-android-lib:1.0.2' } - -apply from: "https://raw.githubusercontent.com/hexhacking/xDL/master/gradle/nativedeps.gradle" ``` -If your network is restricted, please try: - -```Gradle -apply from: "https://gitlab.com/hexhacking/xDL/-/raw/master/gradle/nativedeps.gradle" -``` - -Or: - -```Gradle -apply from: "https://gitee.com/hexhacking/xDL/raw/master/gradle/nativedeps.gradle" -``` - -Of course, you can also download this script and put it in your own project. - -`nativedeps.gradle` will download xDL header file and dynamic library to the `build` directory. - ### 2. Add dependency in CMakeLists.txt or Android.mk > CMakeLists.txt ```CMake -# xDL base path (you may need to modify this path) -set(XDL_BASE ${CMAKE_CURRENT_SOURCE_DIR}/../../../build/nativedeps/xdl) +find_package(xdl REQUIRED CONFIG) -# import xDL -add_library(xdl SHARED IMPORTED) -set_target_properties(xdl PROPERTIES - IMPORTED_LOCATION ${XDL_BASE}/aar/jni/${ANDROID_ABI}/libxdl.so - INTERFACE_INCLUDE_DIRECTORIES ${XDL_BASE}/header - ) - -# your library add_library(mylib SHARED mylib.c) -target_link_libraries(mylib xdl) +target_link_libraries(mylib xdl::xdl) ``` > Android.mk ``` -# xDL base path (you may need to modify this path) -LOCAL_PATH := $(call my-dir) -XDL_BASE := $(LOCAL_PATH)/../../../build/nativedeps/xdl - -# import xDL -include $(CLEAR_VARS) -LOCAL_MODULE := xdl -LOCAL_SRC_FILES := $(XDL_BASE)/aar/jni/$(TARGET_ARCH_ABI)/libxdl.so -LOCAL_EXPORT_C_INCLUDES := $(XDL_BASE)/header -include $(PREBUILT_SHARED_LIBRARY) - -# your library include $(CLEAR_VARS) -LOCAL_MODULE := mylib -LOCAL_SRC_FILES := mylib.c -LOCAL_SHARED_LIBRARIES += xdl +LOCAL_MODULE := mylib +LOCAL_SRC_FILES := mylib.c +LOCAL_SHARED_LIBRARIES += xdl include $(BUILD_SHARED_LIBRARY) + +$(call import-module,prefab/xdl) ``` ### 3. Specify one or more ABI(s) you need @@ -125,7 +94,7 @@ android { } ``` -### 4. Exclude libxdl.so when packaging (Optional) +### 4. Add packaging options If you are using xDL in an SDK project, you may need to avoid packaging libxdl.so into your AAR, so as not to encounter duplicate libxdl.so file when packaging the app project. @@ -137,13 +106,21 @@ android { } ``` +On the other hand, if you are using xDL in an APP project, you may need to add some options to deal with conflicts caused by duplicate libxdl.so file. + +```Gradle +android { + packagingOptions { + pickFirst '**/libxdl.so' + } +} +``` + There is a sample app in the [xdl-sample](xdl_sample) folder you can refer to. ## API -include xDL's header file: - ```C #include "xdl.h" ``` diff --git a/README.zh-CN.md b/README.zh-CN.md index c4539b2..0f868f2 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -2,7 +2,7 @@ ![](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat) ![](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat) -![](https://img.shields.io/badge/release-1.0.1-red.svg?style=flat) +![](https://img.shields.io/badge/release-1.0.2-red.svg?style=flat) ![](https://img.shields.io/badge/Android-4.1%20--%2011-blue.svg?style=flat) ![](https://img.shields.io/badge/arch-armeabi--v7a%20%7C%20arm64--v8a%20%7C%20x86%20%7C%20x86__64-blue.svg?style=flat) @@ -41,76 +41,45 @@ xDL 是 Android DL 系列函数的增强实现。 ## 使用 +xDL 使用从 Android Gradle Plugin 4.0+ 开始支持的 [Prefab](https://google.github.io/prefab/) 包格式。 + +更多信息: [使用 native 依赖项](https://developer.android.com/studio/build/native-dependencies) + ### 1. 在 build.gradle 中增加依赖 ```Gradle -ext { - XDL_VERSION = '1.0.1' +android { + buildFeatures { + prefab true + } } dependencies { - implementation "io.hexhacking.xdl:xdl-android-lib:${XDL_VERSION}" + implementation 'io.hexhacking.xdl:xdl-android-lib:1.0.2' } - -apply from: "https://raw.githubusercontent.com/hexhacking/xDL/master/gradle/nativedeps.gradle" ``` -如果你的网络受到了限制,请试一试: - -```Gradle -apply from: "https://gitlab.com/hexhacking/xDL/-/raw/master/gradle/nativedeps.gradle" -``` - -或者: - -```Gradle -apply from: "https://gitee.com/hexhacking/xDL/raw/master/gradle/nativedeps.gradle" -``` - -当然,你也可以把这个脚本下载后放在你自己的工程里。 - -`nativedeps.gradle` 将把 xDL 的头文件和动态库下载到 `build` 目录中。 - ### 2. 在 CMakeLists.txt 或 Android.mk 中增加依赖 > CMakeLists.txt ```CMake -# xDL base 路径 (你可能需要修改这个路径) -set(XDL_BASE ${CMAKE_CURRENT_SOURCE_DIR}/../../../build/nativedeps/xdl) +find_package(xdl REQUIRED CONFIG) -# 导入 xDL -add_library(xdl SHARED IMPORTED) -set_target_properties(xdl PROPERTIES - IMPORTED_LOCATION ${XDL_BASE}/aar/jni/${ANDROID_ABI}/libxdl.so - INTERFACE_INCLUDE_DIRECTORIES ${XDL_BASE}/header - ) - -# 你的动态库 add_library(mylib SHARED mylib.c) -target_link_libraries(mylib xdl) +target_link_libraries(mylib xdl::xdl) ``` > Android.mk ``` -# xDL base 路径 (你可能需要修改这个路径) -LOCAL_PATH := $(call my-dir) -XDL_BASE := $(LOCAL_PATH)/../../../build/nativedeps/xdl - -# 导入 xDL -include $(CLEAR_VARS) -LOCAL_MODULE := xdl -LOCAL_SRC_FILES := $(XDL_BASE)/aar/jni/$(TARGET_ARCH_ABI)/libxdl.so -LOCAL_EXPORT_C_INCLUDES := $(XDL_BASE)/header -include $(PREBUILT_SHARED_LIBRARY) - -# 你的动态库 include $(CLEAR_VARS) -LOCAL_MODULE := mylib -LOCAL_SRC_FILES := mylib.c -LOCAL_SHARED_LIBRARIES += xdl +LOCAL_MODULE := mylib +LOCAL_SRC_FILES := mylib.c +LOCAL_SHARED_LIBRARIES += xdl include $(BUILD_SHARED_LIBRARY) + +$(call import-module,prefab/xdl) ``` ### 3. 指定一个或多个你需要的 ABI @@ -125,7 +94,7 @@ android { } ``` -### 4. 打包时排除 libxdl.so (可选的) +### 4. 增加打包选项 如果你是在一个 SDK 工程里使用 xDL,你可能需要避免把 libxdl.so 打包到你的 AAR 里,以免 app 工程打包时遇到重复的 libxdl.so 文件。 @@ -137,13 +106,21 @@ android { } ``` +另一方便, 如果你是在一个 APP 工程里使用 xDL,你可以需要增加一些选项,用来处理重复的 libxdl.so 文件引起的冲突。 + +```Gradle +android { + packagingOptions { + pickFirst '**/libxdl.so' + } +} +``` + 你可以参考 [xdl-sample](xdl_sample) 文件中的示例 app。 ## API -包含 xDL 的头文件: - ```C #include "xdl.h" ``` diff --git a/build.gradle b/build.gradle index be2a9bb..9498740 100644 --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,7 @@ buildscript { allprojects { repositories { +// mavenLocal() google() jcenter() } @@ -33,7 +34,7 @@ ext { POM_GROUP_ID = "io.hexhacking.xdl" POM_ARTIFACT_ID = "xdl-android-lib" - POM_VERSION_NAME = "1.0.1" + POM_VERSION_NAME = "1.0.2" POM_NAME = "xDL Android Lib" POM_DESCRIPTION = "xDL is an enhanced implementation of the Android DL series functions." diff --git a/gradle/nativedeps.gradle b/gradle/nativedeps.gradle deleted file mode 100644 index 8003c0d..0000000 --- a/gradle/nativedeps.gradle +++ /dev/null @@ -1,62 +0,0 @@ -project.afterEvaluate { - preBuild.doFirst { - def fileBase = 'xdl-android-lib-' + XDL_VERSION - def pathBase = project.buildDir.absolutePath + File.separator + 'nativedeps' + File.separator + 'xdl' + File.separator - def urlBase = 'https://dl.bintray.com/hexhacking/maven/io/hexhacking/xdl/xdl-android-lib/' + XDL_VERSION + '/' + fileBase - - ['aar': '.aar', 'header': '-native-header.zip'].each { type, suffix -> - downloadUnzip(urlBase + suffix, pathBase + type + File.separator + fileBase + suffix) - } - } -} - -static def downloadUnzip(String url, String filename) { - def newFile = new File(filename) - if(newFile.exists()) { - return - } - - def newDir = new File(newFile.parent) - if (!newDir.exists()) { - newDir.mkdirs() - } else { - newDir.deleteDir() - newDir.mkdirs() - } - - try { - while (url) { - new URL(url).openConnection().with { conn -> - conn.instanceFollowRedirects = false - url = conn.getHeaderField("Location") - if (!url) { - newFile.withOutputStream { out -> - conn.inputStream.with { inp -> - out << inp - inp.close() - } - } - } - } - } - - if (newFile.exists()) { - def zip = new java.util.zip.ZipFile(newFile) - zip.entries().each { - if (!it.isDirectory()) { - def fout = new File(newDir.getPath() + File.separator + it.name) - new File(fout.parent).mkdirs() - def fos = new FileOutputStream(fout) - def buf = new byte[it.size] - def len = zip.getInputStream(it).read(buf) - fos.write(buf, 0, len) - fos.close() - } - } - zip.close() - } - } catch(Exception ex) { - newDir.deleteDir() - throw ex - } -} diff --git a/gradle/publish.gradle b/gradle/publish.gradle index 03f6db3..bf4e0ed 100644 --- a/gradle/publish.gradle +++ b/gradle/publish.gradle @@ -34,16 +34,6 @@ task javadocJar(type: Jar, dependsOn: javadoc) { from javadoc.destinationDir } -task nativesymbolZip(type: Zip, dependsOn: 'build') { - archiveClassifier.set("native-symbol") - from file("build/intermediates/cmake/release/obj/") -} - -task nativeheaderZip(type: Zip) { - archiveClassifier.set("native-header") - EXPORTED_NATIVE_HEADERS.each { from file(it) } -} - project.afterEvaluate { publishing.publications { mavenAar(MavenPublication) { @@ -51,8 +41,6 @@ project.afterEvaluate { artifact sourcesJar artifact javadocJar - artifact nativesymbolZip - artifact nativeheaderZip artifactId POM_ARTIFACT_ID groupId POM_GROUP_ID diff --git a/settings.gradle b/settings.gradle index 74758d6..fb6ead1 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,3 @@ -include ':xdl_lib' +include ':xdl' include ':xdl_sample' rootProject.name = "xDL" diff --git a/xdl_lib/.gitignore b/xdl/.gitignore similarity index 100% rename from xdl_lib/.gitignore rename to xdl/.gitignore diff --git a/xdl_lib/build.gradle b/xdl/build.gradle similarity index 85% rename from xdl_lib/build.gradle rename to xdl/build.gradle index 00a13f2..32ce261 100644 --- a/xdl_lib/build.gradle +++ b/xdl/build.gradle @@ -10,6 +10,7 @@ android { externalNativeBuild { cmake { abiFilters rootProject.ext.abiFilters.split(",") + arguments "-DANDROID_STL=none" if(rootProject.ext.useASAN) { arguments "-DANDROID_ARM_MODE=arm" arguments "-DUSEASAN=ON" @@ -35,10 +36,14 @@ android { minifyEnabled false } } -} - -ext { - EXPORTED_NATIVE_HEADERS = ['src/main/cpp/xdl.h'] + buildFeatures { + prefabPublishing true + } + prefab { + xdl { + headers "src/main/cpp/include" + } + } } apply from: rootProject.file('gradle/publish.gradle') diff --git a/xdl_lib/src/main/AndroidManifest.xml b/xdl/src/main/AndroidManifest.xml similarity index 100% rename from xdl_lib/src/main/AndroidManifest.xml rename to xdl/src/main/AndroidManifest.xml diff --git a/xdl_lib/src/main/cpp/CMakeLists.txt b/xdl/src/main/cpp/CMakeLists.txt similarity index 93% rename from xdl_lib/src/main/cpp/CMakeLists.txt rename to xdl/src/main/cpp/CMakeLists.txt index feefb05..f6264e2 100644 --- a/xdl_lib/src/main/cpp/CMakeLists.txt +++ b/xdl/src/main/cpp/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.4.1) +cmake_minimum_required(VERSION 3.10.2) add_compile_options( -std=c11 @@ -12,6 +12,7 @@ add_library(xdl SHARED ${XDL_SRC}) target_include_directories(xdl PUBLIC + include .) if(USEASAN) diff --git a/xdl_lib/src/main/cpp/xdl.h b/xdl/src/main/cpp/include/xdl.h similarity index 98% rename from xdl_lib/src/main/cpp/xdl.h rename to xdl/src/main/cpp/include/xdl.h index 69dfdbe..46fbd85 100644 --- a/xdl_lib/src/main/cpp/xdl.h +++ b/xdl/src/main/cpp/include/xdl.h @@ -22,7 +22,7 @@ // Created by caikelun on 2020-10-04. // -// xDL version: 1.0.1 +// xDL version: 1.0.2 // // You can always get the latest version from: // https://github.com/hexhacking/xDL diff --git a/xdl_lib/src/main/cpp/xdl.c b/xdl/src/main/cpp/xdl.c similarity index 100% rename from xdl_lib/src/main/cpp/xdl.c rename to xdl/src/main/cpp/xdl.c diff --git a/xdl_lib/src/main/cpp/xdl.map.txt b/xdl/src/main/cpp/xdl.map.txt similarity index 100% rename from xdl_lib/src/main/cpp/xdl.map.txt rename to xdl/src/main/cpp/xdl.map.txt diff --git a/xdl_lib/src/main/cpp/xdl_const.h b/xdl/src/main/cpp/xdl_const.h similarity index 100% rename from xdl_lib/src/main/cpp/xdl_const.h rename to xdl/src/main/cpp/xdl_const.h diff --git a/xdl_lib/src/main/cpp/xdl_iterate.c b/xdl/src/main/cpp/xdl_iterate.c similarity index 100% rename from xdl_lib/src/main/cpp/xdl_iterate.c rename to xdl/src/main/cpp/xdl_iterate.c diff --git a/xdl_lib/src/main/cpp/xdl_iterate.h b/xdl/src/main/cpp/xdl_iterate.h similarity index 100% rename from xdl_lib/src/main/cpp/xdl_iterate.h rename to xdl/src/main/cpp/xdl_iterate.h diff --git a/xdl_lib/src/main/cpp/xdl_lzma.c b/xdl/src/main/cpp/xdl_lzma.c similarity index 100% rename from xdl_lib/src/main/cpp/xdl_lzma.c rename to xdl/src/main/cpp/xdl_lzma.c diff --git a/xdl_lib/src/main/cpp/xdl_lzma.h b/xdl/src/main/cpp/xdl_lzma.h similarity index 100% rename from xdl_lib/src/main/cpp/xdl_lzma.h rename to xdl/src/main/cpp/xdl_lzma.h diff --git a/xdl_lib/src/main/cpp/xdl_util.c b/xdl/src/main/cpp/xdl_util.c similarity index 100% rename from xdl_lib/src/main/cpp/xdl_util.c rename to xdl/src/main/cpp/xdl_util.c diff --git a/xdl_lib/src/main/cpp/xdl_util.h b/xdl/src/main/cpp/xdl_util.h similarity index 100% rename from xdl_lib/src/main/cpp/xdl_util.h rename to xdl/src/main/cpp/xdl_util.h diff --git a/xdl_sample/build.gradle b/xdl_sample/build.gradle index 1fa7884..01325dc 100644 --- a/xdl_sample/build.gradle +++ b/xdl_sample/build.gradle @@ -4,7 +4,6 @@ android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion ndkVersion rootProject.ext.ndkVersion - defaultConfig { applicationId "io.hexhacking.xdl.sample" minSdkVersion rootProject.ext.minSdkVersion @@ -15,7 +14,6 @@ android { abiFilters rootProject.ext.abiFilters.split(",") } } - externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" @@ -25,12 +23,10 @@ android { // path "src/main/cpp/Android.mk" // } } - compileOptions { sourceCompatibility rootProject.ext.javaVersion targetCompatibility rootProject.ext.javaVersion } - buildTypes { debug { minifyEnabled false @@ -39,21 +35,21 @@ android { minifyEnabled false } } + buildFeatures { + prefab true + } + packagingOptions { + pickFirst '**/libxdl.so' + } } -//ext { -// XDL_VERSION = '1.0.1' -//} - dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' - implementation project(':xdl_lib') - //implementation "io.hexhacking.xdl:xdl-android-lib:${XDL_VERSION}" + implementation project(':xdl') +// implementation 'io.hexhacking.xdl:xdl-android-lib:1.0.2' } apply from: rootProject.file('gradle/sanitizer.gradle') -//apply from: rootProject.file('gradle/nativedeps.gradle') -//apply from: "https://raw.githubusercontent.com/hexhacking/xDL/master/gradle/nativedeps.gradle" diff --git a/xdl_sample/src/main/cpp/Android.mk b/xdl_sample/src/main/cpp/Android.mk index 136eb64..d782c73 100644 --- a/xdl_sample/src/main/cpp/Android.mk +++ b/xdl_sample/src/main/cpp/Android.mk @@ -1,13 +1,5 @@ LOCAL_PATH := $(call my-dir) -XDL_BASE := $(LOCAL_PATH)/../../../build/nativedeps/xdl - -include $(CLEAR_VARS) -LOCAL_MODULE := xdl -LOCAL_SRC_FILES := $(XDL_BASE)/aar/jni/$(TARGET_ARCH_ABI)/libxdl.so -LOCAL_EXPORT_C_INCLUDES := $(XDL_BASE)/header -include $(PREBUILT_SHARED_LIBRARY) - include $(CLEAR_VARS) LOCAL_MODULE := sample LOCAL_SRC_FILES := sample.c @@ -16,3 +8,6 @@ LOCAL_CONLYFLAGS := -std=c11 LOCAL_LDLIBS := -llog LOCAL_SHARED_LIBRARIES += xdl include $(BUILD_SHARED_LIBRARY) + +#import xdl by maven +$(call import-module,prefab/xdl) diff --git a/xdl_sample/src/main/cpp/CMakeLists.txt b/xdl_sample/src/main/cpp/CMakeLists.txt index 9474f45..c17876b 100644 --- a/xdl_sample/src/main/cpp/CMakeLists.txt +++ b/xdl_sample/src/main/cpp/CMakeLists.txt @@ -1,28 +1,25 @@ -cmake_minimum_required(VERSION 3.4.1) +cmake_minimum_required(VERSION 3.10.2) add_compile_options( -std=c11 -Weverything -Werror) +# import xdl by local-project string(TOLOWER ${CMAKE_BUILD_TYPE} BUILD_TYPE_DIRNAME) -set(XDL_BASE ${CMAKE_CURRENT_SOURCE_DIR}/../../../../xdl_lib) -add_library(xdl SHARED IMPORTED) -set_target_properties(xdl PROPERTIES +set(XDL_BASE ${CMAKE_CURRENT_SOURCE_DIR}/../../../../xdl) +add_library(xdl::xdl SHARED IMPORTED) +set_target_properties(xdl::xdl PROPERTIES IMPORTED_LOCATION ${XDL_BASE}/build/intermediates/stripped_native_libs/${BUILD_TYPE_DIRNAME}/out/lib/${ANDROID_ABI}/libxdl.so - INTERFACE_INCLUDE_DIRECTORIES ${XDL_BASE}/src/main/cpp + INTERFACE_INCLUDE_DIRECTORIES ${XDL_BASE}/src/main/cpp/include ) -#set(XDL_BASE ${CMAKE_CURRENT_SOURCE_DIR}/../../../build/nativedeps/xdl) -#add_library(xdl SHARED IMPORTED) -#set_target_properties(xdl PROPERTIES -# IMPORTED_LOCATION ${XDL_BASE}/aar/jni/${ANDROID_ABI}/libxdl.so -# INTERFACE_INCLUDE_DIRECTORIES ${XDL_BASE}/header -# ) +# import xdl by maven +#find_package(xdl REQUIRED CONFIG) add_library(sample SHARED sample.c) target_link_libraries(sample - xdl + xdl::xdl log)