After many days of searching the internet i have found a solution:
- download source code from https://stockfishchess.org/download/, doesn't matter the platform
- create a Native C++ project in android studio
- you can delete the native-lib.cpp file in the cpp folder (and the references in code)
- extract the content of the downloaded file, go to src folder, copy all the files into the cpp folder of the android project
- in the CMakeLists.txt(cpp folder) add :
add_executable(
stockfish
benchmark.cpp evaluate.h ...(and the rest of the copied files)
)
- in the build.gradle(app) in externalNativeBuild->cmake add
targets "stockfish"
like so:
externalNativeBuild {
cmake {
targets "stockfish"
cppFlags ""
}
}
- build APK
- in the folder /app/.cxx/cmake/debug have been generated the folders that contain executables
- create a new android project without c++ support
- in the new project in the libs folder copy the folders mentioned above (keep the names), and in each folder delete everything but the stockfish file.
- rename each stockfish file in each folder like so
lib_stockfish.so
, now the folders should look like this:
...
-libs
-arm64-v8a
-lib_stockfish.so
-armeabi-v7a
-lib_stockfish.so
and so on...
...
- in the build.gradle(app) at android add:
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
- in the build.gradle(app) at dependencies add:
implementation fileTree(dir: "libs", include: ["*.jar", "*.so"])
- in the manifest file in the application tag add:
android:extractNativeLibs="true"
- now it's just the code in java:
String path = getApplicationContext().getApplicationInfo().nativeLibraryDir+"/lib_stockfish.so";
File file = new File(path);
try {
process = Runtime.getRuntime().exec(file.getPath());
} catch (IOException e) {
e.printStackTrace();
}
Thread outThread = new Thread(new Runnable() {
@Override
public void run() {
Process processOut = process;
if(processOut == null){
return;
}
BufferedReader out = new BufferedReader(new InputStreamReader(processOut.getInputStream()));
String data;
try{
while( (data = out.readLine()) != null ){
//do something with data
}
} catch(IOException e){
}
}
});
outThread.start();
///to give commands
String command = "uci";//or other command
command += "\n";
try {
Process ep = process;
if (ep != null) {
ep.getOutputStream().write(command.getBytes());
ep.getOutputStream().flush();
}
} catch (IOException e) {
}