Skip to content

Commit 986fa08

Browse files
committed
Fix cmake for native code in release builds
1 parent 3979ba0 commit 986fa08

4 files changed

Lines changed: 29 additions & 125 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88

99
defaultConfig {
1010
applicationId "com.application.bethela"
11-
minSdk 30
11+
minSdk 26
1212
targetSdk 33
1313
versionCode 1
1414
versionName "1.0"
@@ -20,7 +20,7 @@ android {
2020
// arguments "-DANDROID_STL=c++_shared"
2121

2222
// Set C++ version
23-
cppFlags '-std=c++14', '-march=armv8-a+crypto', '-O3'
23+
cppFlags '-std=c++14'
2424
}
2525
}
2626
}

app/src/main/cpp/CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,26 @@ find_library( # Sets the name of the path variable.
3939
# Specifies libraries CMake should link to your target library. You
4040
# can link multiple libraries, such as libraries you define in this
4141
# build script, prebuilt third-party libraries, or system libraries.
42-
4342
target_link_libraries( # Specifies the target library.
4443
bethela
4544

4645
# Links the target library to the log library
4746
# included in the NDK.
4847
${log-lib})
4948

49+
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
50+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
51+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -O3")
52+
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
53+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
54+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+crypto -O3")
55+
elseif (${ANDROID_ABI} STREQUAL "arm64-v8a")
56+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
57+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+crypto -O3")
58+
#else()
59+
# message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}")
60+
endif()
61+
5062
# Enable Address Sanitizer - NOT FOR RELEASE (IT'S NOT WORKING BECAUSE I'M AN IDIOT!)
5163
#target_compile_options(${TARGET} -fsanitize=hwaddress -fno-omit-frame-pointer)
5264
#set_target_properties(${TARGET} PROPERTIES LINK_FLAGS -fsanitize=hwaddress)

app/src/main/cpp/native-lib.cpp

Lines changed: 4 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
#include <limits>
1111
#include <android/log.h>
1212

13+
#if defined(__x86_64__) || defined(_M_X64)
14+
#define USE_AESNI
15+
#elif defined(__aarch64__) || defined(_M_ARM64)
1316
#define USE_ARM_AES
17+
#endif
1418

1519
#include "Krypt/src/Krypt.hpp"
1620
#include "jpp.hpp"
@@ -35,120 +39,6 @@ constexpr static jint FILE_SIGNATURE_SIZE = 7;
3539
/// the size of one AES block in bytes.
3640
constexpr static size_t AES_BLOCK_SIZE = 16;
3741

38-
extern "C" JNIEXPORT jstring JNICALL Java_com_application_bethela_BethelaActivity_doubleString (
39-
JNIEnv *env,
40-
jobject,
41-
jstring arg
42-
) {
43-
jboolean isCopy = 1;
44-
const char *c_str_arg = env->GetStringUTFChars(arg, &isCopy);
45-
std::string test(c_str_arg);
46-
test += " | 2" + test + " : ";
47-
jstring doubleString = env->NewStringUTF(test.c_str());
48-
delete[] c_str_arg;
49-
return doubleString;
50-
}
51-
52-
extern "C" JNIEXPORT jbyteArray JNICALL Java_com_application_bethela_BethelaActivity_doubleByte (
53-
JNIEnv *env,
54-
jobject,
55-
jbyteArray arg,
56-
jint size
57-
) {
58-
jbyte *read_only = env->GetByteArrayElements(arg, nullptr);
59-
60-
jbyteArray doubleArray = env->NewByteArray(size * 2);
61-
env->SetByteArrayRegion(doubleArray, 0, size, read_only);
62-
env->SetByteArrayRegion(doubleArray, size, size, read_only);
63-
64-
env->ReleaseByteArrayElements(arg, read_only, 0);
65-
66-
return doubleArray;
67-
}
68-
69-
extern "C" JNIEXPORT jobjectArray JNICALL Java_com_application_bethela_BethelaActivity_transpose (
70-
JNIEnv *env,
71-
jobject,
72-
jobjectArray arr,
73-
jint row,
74-
jint column
75-
) {
76-
// Create a vector that will hold the original jintArray rows
77-
std::vector<jintArray> original_rows;
78-
79-
// Allocate an array of pointers that will contain the copy/reference of the rows
80-
jint **row_elements = new jint *[row];
81-
82-
for (jsize i = 0; i < row; ++i) {
83-
// get the `jintArray` row at index `i` of the `jobjectArray arr`, push it to a vector
84-
original_rows.push_back((jintArray) env->GetObjectArrayElement(arr, i));
85-
86-
// create a copy/reference buffer of the aquired `jintArray`
87-
row_elements[i] = env->GetIntArrayElements(original_rows.back(), nullptr);
88-
}
89-
90-
// Allocate a new C 2D array for the transpose and apply the transpose
91-
jint **transposed_row_elements = new jint *[column];
92-
for (jsize i = 0; i < column; ++i) {
93-
transposed_row_elements[i] = new jint[row];
94-
for (jsize j = 0; j < row; ++j) {
95-
transposed_row_elements[i][j] = row_elements[j][i];
96-
}
97-
}
98-
99-
// create a new jobjectArray that will contain the transpose 2D array values
100-
jclass jintArrayClass = env->FindClass("[I");
101-
jobjectArray transposed = env->NewObjectArray(column, jintArrayClass, nullptr);
102-
103-
for (jsize i = 0; i < column; ++i) {
104-
// create a new jintArray
105-
jintArray curr_row = env->NewIntArray(row);
106-
107-
// set the values of the new jintArray using the values of the transposed matrix
108-
env->SetIntArrayRegion(curr_row, 0, row, transposed_row_elements[i]);
109-
110-
// add the row jintArray to the main jobjectArray transpose matrix
111-
env->SetObjectArrayElement(transposed, i, curr_row);
112-
}
113-
114-
// release the copy/reference buffers of the original rows that we got from the first loop
115-
for (jsize i = 0; i < row; ++i) {
116-
env->ReleaseIntArrayElements(original_rows[i], row_elements[i], 0);
117-
}
118-
119-
// deallocate all of the C++ array that we allocated using C++ convention
120-
for (jsize i = 0; i < column; ++i) {
121-
delete[] transposed_row_elements[i];
122-
}
123-
124-
delete[] transposed_row_elements;
125-
delete[] row_elements;
126-
127-
return transposed;
128-
}
129-
130-
extern "C" JNIEXPORT jintArray JNICALL Java_com_application_bethela_BethelaActivity_reverse (
131-
JNIEnv *env,
132-
jobject,
133-
jintArray arr,
134-
jint size
135-
) {
136-
// A C array that could be a copy or a direct pointer to `arr`
137-
jint *reverse_array = env->GetIntArrayElements(arr, nullptr);
138-
139-
// reverse the array
140-
for (jsize i = 0; i < size / 2; ++i) {
141-
jint temp = reverse_array[i];
142-
reverse_array[i] = reverse_array[size - 1 - i];
143-
reverse_array[size - 1 - i] = temp;
144-
}
145-
146-
// free the C array and apply the changes back to the `arr`
147-
env->ReleaseIntArrayElements(arr, reverse_array, 0);
148-
149-
return arr;
150-
}
151-
15242
namespace RESULT_CODE {
15343
jint INVALID_INTERNAL_BUFFER_SIZE = -1;
15444
jint THREAD_ATTACHMENT_FAILED = -2;

app/src/main/java/com/application/bethela/BethelaActivity.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ protected void onCreate(Bundle savedInstanceState) {
9797
Toast.makeText(this, "AES - Portable C++ (Slow)", Toast.LENGTH_LONG).show();
9898
} else if (checkAesCodeImplementation() == 1) {
9999
Toast.makeText(this, "AES - ARM Neon (fast)", Toast.LENGTH_LONG).show();
100+
} else if (checkAesCodeImplementation() == 2) {
101+
Toast.makeText(this, "AES - AES-NI (fast)", Toast.LENGTH_LONG).show();
100102
} else {
101103
Toast.makeText(this, "AES - cannot detect implementation", Toast.LENGTH_LONG).show();
102104
}
@@ -108,14 +110,14 @@ protected void onCreate(Bundle savedInstanceState) {
108110

109111
private void allBtnSetEnabled(boolean enable) {
110112
imgBtnSelectKeyFile.setEnabled(enable);
111-
imgBtnSelectPassword.setEnabled(enable);;
112-
imgBtnGenerateKey.setEnabled(enable);;
113-
imgBtnClearKeys.setEnabled(enable);;
114-
imgBtnSelectFiles.setEnabled(enable);;
115-
imgBtnClearFiles.setEnabled(enable);;
116-
imgBtnSelectLocation.setEnabled(enable);;
117-
imgBtnEncrypt.setEnabled(enable);;
118-
imgBtnDecrypt.setEnabled(enable);;
113+
imgBtnSelectPassword.setEnabled(enable);
114+
imgBtnGenerateKey.setEnabled(enable);
115+
imgBtnClearKeys.setEnabled(enable);
116+
imgBtnSelectFiles.setEnabled(enable);
117+
imgBtnClearFiles.setEnabled(enable);
118+
imgBtnSelectLocation.setEnabled(enable);
119+
imgBtnEncrypt.setEnabled(enable);
120+
imgBtnDecrypt.setEnabled(enable);
119121
}
120122

121123
// ##################################################################################

0 commit comments

Comments
 (0)