Migrate the lp_solve
code from C, Java to Android.
Version of lp_solve
: 5.5.2.5
Few days ago, I was developing some tools for Princess Connect Re:Dive and met the problem of linear programming.
However, when I try the org.apache.commons:commons-math3
library, it shows the BAD performance as the constraints and variable increase. That's the reason why I migrate the lp_solve
library from C to Android.
It's believed that lp_solve
libraries give the correct answer at least **10 TIMES FASTER THAN ** org.apache.commons:commons-math3
. And the accuracy has increased dramatically, compared to the former.
Before we go on, we shall check some prerequisites:
- Android Studio
- CMake
After that, download the latest lpsolve_android_release.zip
in release page and unzip it. The directory structure is listed below
app
- build.gradle
- src
- main
- cpp
- CMakeLists.txt
- ...More
- java
- lpsolve
- ...More
lpsolve_bin
- lib
- arm64-v8a
- armeabi-v7a
- x86
- x86_64
The directories inside lpsolve_bin/lib
contain the .so
libraries for each platforms.
There are 2 different ways to lay out the lp_solve
libraries.
First, build a NDK project in AS.
The file structure in directory app
correspond to the real structure in ndk project of Android Studio. Then:
- Put the files in directory
app/src
in the corresponding location of the ndk project. Make sure thelpsolve
package is the subdirectory ofapp/src/main/java
and thecpp
dir is the subdirectory ofapp/src/main
, or it may cause errors. - Modify the
build.gradle
inapp
as below:
android{
...
defaultConfig{
...
# Here define the parameters which control the cmake
externalNativeBuild {
cmake {
cFlags ""
cppFlags ""
}
}
}
...
# Here is the relative path of the CMakeList.txt
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
}
-
Start an existing android project with AS.
-
Put the files in directory
app/src
in the corresponding location of the NDK project. Make sure thelpsolve
package is the subdirectory ofapp/src/main/java
and thecpp
directory is the subdirectory ofapp/src/main
, or it may cause errors. -
Open the Project pane from the left side of the IDE and select the Android view.
-
Right-click on the module you would like to link to your native library, such as the app module, and select Link C++ Project with Gradle from the menu.
-
From the drop-down menu, select CMake. Then, use the field next to Project Path to specify the
src/main/cpp/CMakeLists.txt
script file for your external CMake project. -
Click OK.
Above steps could be referred to Link Gradle to your native library on the website of Android Developers.
When everything is done, you could import the lpsolve
package the same as what we do with Java.
And every time the .apk
file is compiled and built, the lpsolve
would be packed in it.
The project relies on: