Skip to content

Instantly share code, notes, and snippets.

@amal
Forked from blessedbyjobs/CMakeLists.txt
Created March 20, 2024 14:36
Show Gist options
  • Save amal/4b1d3f1286a3772150ccca7f3af8818c to your computer and use it in GitHub Desktop.
Save amal/4b1d3f1286a3772150ccca7f3af8818c to your computer and use it in GitHub Desktop.
Код, используемый для проверки, что устройство имеет root права
android {
defaultConfig {
externalNativeBuild {
cmake {
arguments "-DANDROID_TOOLCHAIN=clang", "-O3"
cppFlags "-std=c++11"
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
// этот файл поместить в корень проекта
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
// адаптировать под структуру вашего проекта
set(SOURCES src/main/cpp/NativeRootChecker.cpp)
add_library(root_checker_jni SHARED ${SOURCES})
find_library(M m)
find_library(LOG log)
target_link_libraries(root_checker_jni ${M} ${LOG})
#include <jni.h>
#include <iostream>
#include <fstream>
using namespace std;
bool rooted();
const string folders[] = {"/system/app/Superuser.apk", "/system/app/KingUser.apk", "/sbin/su",
"/system/bin/su", "/system/xbin/su", "/data/local/xbin/su",
"/data/local/bin/su", "/system/sd/xbin/su", "/system/bin/failsafe/su",
"/data/local/su", "/su/bin/su"};
extern "C"
// адаптировать под структуру вашего проекта
JNIEXPORT jboolean JNICALL Java_NativeRootChecker_check(JNIEnv *env, jclass type) {
auto isRooted = static_cast<jboolean>(rooted());
return isRooted;
}
inline bool file_exists(const std::string &name) {
ifstream f(name.c_str());
return f.rdstate() == 0;
}
bool rooted() {
for (int i = 0; i < folders->length(); ++i) {
if (file_exists(folders[i])) {
return true;
}
}
return false;
}
/**
* JNI мост, при инициализации проходит проверка на root-права
*/
object NativeRootChecker {
init {
System.loadLibrary("root_checker_jni")
}
external fun check(): Boolean
}
/**
* Объект, проверяющий наличие рут-прав на устройстве
*/
object RootChecker {
val isRoot: Boolean = isRootInternal()
private fun isRootInternal(): Boolean = NativeRootChecker.check()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment