Skip to content

Commit e113460

Browse files
committed
Android 16KB issue
1 parent dfd5eff commit e113460

File tree

8 files changed

+239
-9
lines changed

8 files changed

+239
-9
lines changed

subs/compose/src/main/java/com/engineer/compose/ui/VideoPlayerActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ fun VideoPlayer(videoUri: Uri) {
119119
}
120120

121121
override fun surfaceDestroyed(holder: SurfaceHolder) {
122+
isPlaying = false
122123
mediaPlayer.stop()
123124
mediaPlayer.release()
124125
}

subs/cpp_native/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ android {
1616
externalNativeBuild {
1717
cmake {
1818
cppFlags ''
19+
arguments "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
1920
}
2021
}
2122
ndk {
@@ -36,6 +37,7 @@ android {
3637
externalNativeBuild {
3738
cmake {
3839
path file('src/main/cpp/CMakeLists.txt')
40+
// path file('src/main/cpp/p/CMakeLists.txt')
3941
version '3.22.1'
4042
}
4143
}

subs/cpp_native/src/docs/doc.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
## 使用 CMakeLists.txt 的方式
2+
3+
### 完全源码编译
4+
5+
#### 源码自身包含全部内容
6+
7+
- `bspatch.c` 中已经完全引入了全部源码,包含完整的头文件和 c/cpp
8+
9+
```c
10+
#include <bzlib.h>
11+
#include <stdlib.h>
12+
#include <stdio.h>
13+
#include <string.h>
14+
#include <err.h>
15+
#include <unistd.h>
16+
#include <fcntl.h>
17+
#include <sys/types.h>
18+
#include "bspatch.h"
19+
/** 导入bzip2的引用*/
20+
#include "bzip/bzlib.c"
21+
#include "bzip/crctable.c"
22+
#include "bzip/compress.c"
23+
#include "bzip/decompress.c"
24+
#include "bzip/randtable.c"
25+
#include "bzip/blocksort.c"
26+
#include "bzip/huffman.c"
27+
28+
```
29+
那么,在 `CMakeLists.txt` 中只需配置头文件即可
30+
31+
```cmake
32+
include_directories(../bzip/)
33+
include_directories(../logcat)
34+
35+
add_library( # Sets the name of the library.
36+
cpp_native
37+
38+
# Sets the library as a shared library.
39+
SHARED
40+
../bspatch.c
41+
# Provides a relative path to your source file(s).
42+
../cpp_native.cpp)
43+
```
44+
`include_directories` 会导入所有需要的 .h 文件,这样在 `bspatch.c` 通过文件名就可以直接引入了,同时由于 `bspatch.c` 引入了所有源码,编译时只需要引入单个文件即可
45+
46+
#### 源码只包含头文件
47+
48+
- `bspatch.c` 中只引入了头文件
49+
50+
```c
51+
#include <bzlib.h>
52+
#include <stdlib.h>
53+
#include <stdio.h>
54+
#include <string.h>
55+
#include <err.h>
56+
#include <unistd.h>
57+
#include <fcntl.h>
58+
#include <sys/types.h>
59+
#include "bspatch.h"
60+
/** 导入bzip2的引用*/
61+
//#include "bzip/bzlib.c"
62+
//#include "bzip/crctable.c"
63+
//#include "bzip/compress.c"
64+
//#include "bzip/decompress.c"
65+
//#include "bzip/randtable.c"
66+
//#include "bzip/blocksort.c"
67+
//#include "bzip/huffman.c"
68+
```
69+
对于这种情况,需要在 `CMakeLists.txt` 中配置完整的源码文件
70+
71+
```cmake
72+
73+
file(GLOB BZIP ../bzip/*.c)
74+
message("----------start------")
75+
message("${BZIP}")
76+
message("${CMAKE_CURRENT_SOURCE_DIR}")
77+
message("----------end------")
78+
#导入头文件
79+
include_directories(../bzip/)
80+
include_directories(../logcat)
81+
82+
add_library( # Sets the name of the library.
83+
cpp_native
84+
85+
# Sets the library as a shared library.
86+
SHARED
87+
#将bzip下的.c文件添加到library
88+
${BZIP}
89+
../bspatch.c
90+
# Provides a relative path to your source file(s).
91+
../cpp_native.cpp)
92+
```
93+
94+
通过以上配置,{BZIP} 包含全部 .c 文件, add_library 配置之后,就包含了全部源码。但是以上两种情况不能混用,否则会产生冲突。
95+
即如果 `bspatch.c` 如果已经有了完整源码,即不需要再通过 add_library 添加多余的内容了。
96+
97+
```cmake
98+
target_link_libraries( # Specifies the target library.
99+
cpp_native
100+
# Links the target library to the log library
101+
# included in the NDK.
102+
${log-lib})
103+
```
104+
最终会生成 `cpp_native.so` ,其实这个配置不是必须的,这个配置只是让 cpp_native 包含了 log-lib 的内容,如果用不到的话,可以忽略。
105+
add_library 这个配置其实就可以确保 so 可以生成。
106+
107+
### 生成任意 so
108+
109+
```cmake
110+
# 将 bspatch 的源码编译为共享库
111+
add_library(
112+
bspatch_tool
113+
SHARED
114+
../bspatch.c)
115+
```
116+
比如按上面这样配置之后,就会同时生成一个名为 bspatch_tool 的 so
117+
118+
[](libs.png)
119+
120+
这样就生成了两个 so, 后续我们只要有头文件或者说函数声明,就可以在任意位置方便的使用 so 中包含的方法了
121+
122+
123+
### 依赖 so 及头文件编译
124+
125+
通过上一步,我们将 `bspatch.c` 的实现打包成了一个 so ,那么我们就可以摆脱源码,直接使用这个文件了。
126+
127+
128+
```cmake
129+
130+
#导入头文件
131+
include_directories(bzip/)
132+
include_directories(logcat/)
133+
134+
135+
add_library( # Sets the name of the library.
136+
cpp_native
137+
138+
# Sets the library as a shared library.
139+
SHARED
140+
141+
cpp_native.cpp)
142+
143+
## 定义 distributionDir 变量,声明所依赖的 so 库存放的位置,这个不是必须的,只是为了灵活方便
144+
set(distributionDir ${CMAKE_CURRENT_SOURCE_DIR}/third-lib)[imitate-debug.apk](../../../../imitate/build/intermediates/apk/debug/imitate-debug.apk)
145+
## 所依赖的三方库定义名称,STATIC IMPORTED 表示是导入的
146+
add_library(lib_bspatch STATIC IMPORTED)
147+
## 定义导入的 so 库的路径
148+
set_target_properties(lib_bspatch PROPERTIES IMPORTED_LOCATION ${distributionDir}/${ANDROID_ABI}/libbspatch_tool.so)
149+
150+
# Specifies libraries CMake should link to your target library. You
151+
# can link multiple libraries, such as libraries you define in this
152+
# build script, prebuilt third-party libraries, or system libraries.
153+
154+
target_link_libraries( # Specifies the target library.
155+
cpp_native
156+
lib_bspatch
157+
# Links the target library to the log library
158+
# included in the NDK.
159+
${log-lib})
160+
```
161+
162+
我们只需要导入头文件,并且将 libbspatch_tool.so 设置为一个导入的静态库,最后只需要进行链接即可,这样最终生成的产物中,会有 cpp_native,lib_bspatch 两个 so 文件,使用起来更加的方便。

subs/cpp_native/src/docs/libs.png

18.1 KB
Loading

subs/cpp_native/src/main/cpp/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ message("${BZIP}")
1717
message("${CMAKE_CURRENT_SOURCE_DIR}")
1818
message("----------end------")
1919
#导入头文件
20-
include_directories(bzip)
21-
include_directories(logcat)
20+
include_directories(bzip/)
21+
include_directories(logcat/)
2222
# Creates and names a library, sets it as either STATIC
2323
# or SHARED, and provides the relative paths to its source code.
2424
# You can define multiple libraries, and CMake builds them for you.
@@ -30,8 +30,8 @@ add_library( # Sets the name of the library.
3030
# Sets the library as a shared library.
3131
SHARED
3232
#将bzip下的.c文件添加到library
33-
# ${BZIP}
34-
# bspatch.c
33+
# ${BZIP}
34+
# bspatch.c
3535
# Provides a relative path to your source file(s).
3636
cpp_native.cpp)
3737

subs/cpp_native/src/main/cpp/cpp_native.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <jni.h>
22
#include <string>
3-
#include "androidlog.h"
3+
//#include "androidlog.h"
44
#include "bspatch.h"
55

66
using namespace std;
@@ -10,7 +10,7 @@ JNIEXPORT jstring
1010

1111
JNICALL
1212
Java_com_example_cpp_1native_internal_NativeHouse_getNativeMessage(JNIEnv *env, jobject thiz) {
13-
LOGE("native method getNativeMessage called");
13+
// LOGE("native method getNativeMessage called");
1414
string result = "this is from c++";
1515
return env->NewStringUTF(result.c_str());
1616
}
@@ -27,9 +27,9 @@ Java_com_example_cpp_1native_internal_PatchUtil_patchAPK(JNIEnv *env, jclass cla
2727
argv[2] = (char *) (env->GetStringUTFChars(new_apk_file, nullptr));
2828
argv[3] = (char *) (env->GetStringUTFChars(patch_file, nullptr));
2929

30-
LOGD("argv[1] = %s", argv[1]);
31-
LOGD("argv[2] = %s", argv[2]);
32-
LOGD("argv[3] = %s", argv[3]);
30+
// LOGD("argv[1] = %s", argv[1]);
31+
// LOGD("argv[2] = %s", argv[2]);
32+
// LOGD("argv[3] = %s", argv[3]);
3333

3434
main(argc, argv);
3535

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
# For more information about using CMake with Android Studio, read the
3+
# documentation: https://d.android.com/studio/projects/add-native-code.html
4+
5+
# Sets the minimum version of CMake required to build the native library.
6+
7+
cmake_minimum_required(VERSION 3.22.1)
8+
9+
# Declares and names the project.
10+
11+
project("cpp_native")
12+
13+
#定义一个全局变量包含了所有要编译的C文件
14+
file(GLOB BZIP ../bzip/*.c)
15+
message("----------start------")
16+
message("${BZIP}")
17+
message("${CMAKE_CURRENT_SOURCE_DIR}")
18+
message("----------end------")
19+
#导入头文件
20+
include_directories(../bzip/)
21+
include_directories(../logcat)
22+
# Creates and names a library, sets it as either STATIC
23+
# or SHARED, and provides the relative paths to its source code.
24+
# You can define multiple libraries, and CMake builds them for you.
25+
# Gradle automatically packages shared libraries with your APK.
26+
27+
add_library( # Sets the name of the library.
28+
cpp_native
29+
30+
# Sets the library as a shared library.
31+
SHARED
32+
#将bzip下的.c文件添加到library
33+
# ${BZIP}
34+
../bspatch.c
35+
# Provides a relative path to your source file(s).
36+
../cpp_native.cpp)
37+
38+
# Searches for a specified prebuilt library and stores the path as a
39+
# variable. Because CMake includes system libraries in the search path by
40+
# default, you only need to specify the name of the public NDK library
41+
# you want to add. CMake verifies that the library exists before
42+
# completing its build.
43+
44+
find_library( # Sets the name of the path variable.
45+
log-lib
46+
47+
# Specifies the name of the NDK library that
48+
# you want CMake to locate.
49+
log)
50+
51+
# 将 bspatch 的源码编译为共享库
52+
add_library(
53+
bspatch_tool
54+
SHARED
55+
../bspatch.c)
56+
57+
# Specifies libraries CMake should link to your target library. You
58+
# can link multiple libraries, such as libraries you define in this
59+
# build script, prebuilt third-party libraries, or system libraries.
60+
61+
#target_link_libraries( # Specifies the target library.
62+
# cpp_native
63+
# # Links the target library to the log library
64+
# # included in the NDK.
65+
# ${log-lib})
-984 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)