Support JSON for Java interface in Android example.

This commit is contained in:
levlam 2024-05-31 11:41:35 +03:00
parent f9b7e1bc2e
commit 371ddde1f5
3 changed files with 29 additions and 8 deletions

View File

@ -4,6 +4,9 @@ project(TdAndroid VERSION 1.0 LANGUAGES CXX)
set(TD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../..)
option(TD_ANDROID_JSON "Use \"ON\" to build JSON interface.")
option(TD_ANDROID_JSON_JAVA "Use \"ON\" to build Java wrapper for JSON API.")
if (TD_ANDROID_JSON)
if (CMAKE_CROSSCOMPILING)
string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -flto=thin -Oz")
@ -13,7 +16,9 @@ if (TD_ANDROID_JSON)
return()
endif()
option(TD_ENABLE_JNI "Enable JNI-compatible TDLib API" ON)
if (NOT TD_ANDROID_JSON_JAVA)
option(TD_ENABLE_JNI "Enable JNI-compatible TDLib API" ON)
endif()
if (CMAKE_CROSSCOMPILING)
set(CMAKE_MODULE_PATH "${TD_DIR}/CMake")
@ -27,7 +32,13 @@ if (CMAKE_CROSSCOMPILING)
add_library(tdjni SHARED "${TD_DIR}/example/java/td_jni.cpp")
target_link_libraries(tdjni PRIVATE Td::TdStatic)
if (TD_ANDROID_JSON_JAVA)
target_link_libraries(tdjni PRIVATE Td::TdJsonStatic)
target_compile_definitions(tdjni PRIVATE TD_JSON_JAVA=1)
set_target_properties(tdjni PROPERTIES OUTPUT_NAME "tdjsonjava")
else()
target_link_libraries(tdjni PRIVATE Td::TdStatic)
endif()
target_compile_definitions(tdjni PRIVATE PACKAGE_NAME="org/drinkless/tdlib")
add_custom_command(TARGET tdjni POST_BUILD
@ -36,6 +47,10 @@ if (CMAKE_CROSSCOMPILING)
else()
add_subdirectory(${TD_DIR} td)
if (TD_ANDROID_JSON_JAVA)
return()
endif()
set(TD_API_JAVA_PACKAGE "org/drinkless/tdlib")
set(TD_API_JAVA_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${TD_API_JAVA_PACKAGE}/TdApi.java")
set(TD_API_TLO_PATH "${TD_DIR}/td/generate/auto/tlo/td_api.tlo")

View File

@ -21,7 +21,9 @@ You can specify different OpenSSL version as the fourth parameter to the script
You can build TDLib against shared standard C++ library by specifying "c++_shared" as the fourth parameter to the script `./build-tdlib.sh`. This can reduce total application size if you have a lot of other C++ code and want it to use the same shared library.
You can build TDLib with [JSON interface](https://github.com/tdlib/td#using-json) instead of [Java](https://github.com/tdlib/td#using-java) interface by passing "JSON" as the fifth parameter to the script `./build-tdlib.sh`.
You can also build TDLib with [JSON interface](https://github.com/tdlib/td#using-json) instead of [Java](https://github.com/tdlib/td#using-java) interface by passing "JSON" as the fifth parameter to the script `./build-tdlib.sh`.
You can also build TDLib with [JSON interface](https://github.com/tdlib/td#using-json) that can be called from Java by passing "JSONJava" as the fifth parameter to the script `./build-tdlib.sh`.
You can pass an empty string instead of any script parameter to use its default value. For example, you can use the command `./build-tdlib.sh '' '' '' '' 'JSON'` to build TDLib with [JSON interface](https://github.com/tdlib/td#using-json) using default values for other parameters.

View File

@ -11,8 +11,8 @@ if [ "$ANDROID_STL" != "c++_static" ] && [ "$ANDROID_STL" != "c++_shared" ] ; th
exit 1
fi
if [ "$TDLIB_INTERFACE" != "Java" ] && [ "$TDLIB_INTERFACE" != "JSON" ] ; then
echo 'Error: TDLIB_INTERFACE must be either "Java" or "JSON".'
if [ "$TDLIB_INTERFACE" != "Java" ] && [ "$TDLIB_INTERFACE" != "JSON" ] && [ "$TDLIB_INTERFACE" != "JSONJava" ] ; then
echo 'Error: TDLIB_INTERFACE must be either "Java", "JSON", or "JSONJava".'
exit 1
fi
@ -32,7 +32,7 @@ ANDROID_SDK_ROOT="$(cd "$(dirname -- "$ANDROID_SDK_ROOT")" >/dev/null; pwd -P)/$
ANDROID_NDK_ROOT="$ANDROID_SDK_ROOT/ndk/$ANDROID_NDK_VERSION"
OPENSSL_INSTALL_DIR="$(cd "$(dirname -- "$OPENSSL_INSTALL_DIR")" >/dev/null; pwd -P)/$(basename -- "$OPENSSL_INSTALL_DIR")"
PATH=$ANDROID_SDK_ROOT/cmake/3.22.1/bin:$PATH
TDLIB_INTERFACE_OPTION=$([ "$TDLIB_INTERFACE" == "JSON" ] && echo "-DTD_ANDROID_JSON=ON" || echo "")
TDLIB_INTERFACE_OPTION=$([ "$TDLIB_INTERFACE" == "JSON" ] && echo "-DTD_ANDROID_JSON=ON" || [ "$TDLIB_INTERFACE" == "JSONJava" ] && echo "-DTD_ANDROID_JSON_JAVA=ON" || echo "")
cd $(dirname $0)
@ -54,7 +54,7 @@ if [ "$TDLIB_INTERFACE" == "Java" ] ; then
cmake --build build-native-$TDLIB_INTERFACE --target tl_generate_java || exit 1
php AddIntDef.php org/drinkless/tdlib/TdApi.java || exit 1
mkdir -p tdlib/java/org/drinkless/tdlib || exit 1
cp -p {../../example,tdlib}/java/org/drinkless/tdlib/Client.java || exit 1
cp -p {..,tdlib}/java/org/drinkless/tdlib/Client.java || exit 1
mv {,tdlib/java/}org/drinkless/tdlib/TdApi.java || exit 1
rm -rf org || exit 1
@ -64,6 +64,10 @@ if [ "$TDLIB_INTERFACE" == "Java" ] ; then
javadoc -d tdlib/javadoc -encoding UTF-8 -charset UTF-8 -classpath "android.jar${JAVADOC_SEPARATOR}annotation-1.4.0.jar" -quiet -sourcepath tdlib/java org.drinkless.tdlib || exit 1
rm android.jar annotation-1.4.0.jar || exit 1
fi
if [ "$TDLIB_INTERFACE" == "JSONJava" ] ; then
mkdir -p tdlib/java/org/drinkless/tdlib || exit 1
cp -p {..,tdlib}/java/org/drinkless/tdlib/JsonClient.java || exit 1
fi
echo "Building TDLib..."
for ABI in arm64-v8a armeabi-v7a x86_64 x86 ; do
@ -72,7 +76,7 @@ for ABI in arm64-v8a armeabi-v7a x86_64 x86 ; do
mkdir -p build-$ABI-$TDLIB_INTERFACE || exit 1
cd build-$ABI-$TDLIB_INTERFACE
cmake -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake" -DOPENSSL_ROOT_DIR="$OPENSSL_INSTALL_DIR/$ABI" -DCMAKE_BUILD_TYPE=RelWithDebInfo -GNinja -DANDROID_ABI=$ABI -DANDROID_STL=$ANDROID_STL -DANDROID_PLATFORM=android-16 $TDLIB_INTERFACE_OPTION .. || exit 1
if [ "$TDLIB_INTERFACE" == "Java" ] ; then
if [ "$TDLIB_INTERFACE" == "Java" ] || [ "$TDLIB_INTERFACE" == "JSONJava" ] ; then
cmake --build . --target tdjni || exit 1
cp -p libtd*.so* ../tdlib/libs/$ABI/ || exit 1
fi