xmake-gradle plug-in v1.0.7 released, integrated with xmake to quickly build Android JNI program

Introduction

xmake-gradle is a gradle plugin that seamlessly integrates xmake.

At present, there are two ways to do integrated development of android jni in gradle, supported by ndkBuild or CMake, gradle also has built-in integration of these two tools

However, maintaining  Android.mk  is still very tedious, especially for large projects, and the dsl syntax of cmake is not simple and intuitive, and I personally don't like it very much. Therefore, I used xmake to implement cross-platform development. The advantages are: It's simple, fast, friendly to novices, and it's also very powerful. You can go to the xmake project homepage to see the introduction.

But before I want to use xmake to compile the android so library, I can only use the command line, such as:

xmake f -p android --ndk=xxxx
xmake

Although it is very simple, but if you want to package and integrate with android apk / aar, still need a lot of extra work. In order to improve the efficiency of developers, I recently reorganized this gradle plugin to seamlessly integrate into the entire gradle build System.

In this way, users can easily use xmake to compile the jni library in android studio, and automatic integration.

In addition, the related gradle configuration is basically the same as cmake and ndkbuild, most of them are compatible, and the switching cost will also be reduced a lot.

Everyone is welcome to try it, the newly released plugin, if you want to know more, please refer to:

Ready to work

We need to install the corresponding xmake command line tool first. For installation instructions, please see: xmake .

Application plugin

Integration via plug-in DSL

plugins {
  id 'org.tboox.gradle-xmake-plugin' version '1.0.6'
}

Abandoned plugin integration method

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath 'org.tboox:gradle-xmake-plugin:1.0.6'
  }
  repositories {
    mavenCentral()
  }
}

apply plugin: "org.tboox.gradle-xmake-plugin"

Configuration

The simplest configuration example

If we add xmake.luafiles to it projectdir/jni/xmake.lua, then we only need to enable the corresponding JNI project path specified in xmake specified in build.gradle.

build.gradle

android {
    externalNativeBuild {
        xmake {
            path "jni/xmake.lua"
        }
    }
}

JNI

JNI engineering structure

projectdir
  - src
    - main
      - java
  - jni
    - xmake.lua
    - *.cpp

xmake.lua:

add_rules("mode.debug", "mode.release")
target("nativelib")
    set_kind("shared")
    add_files("nativelib.cc")

More Gradle configuration instructions

android {
    defaultConfig {
        externalNativeBuild {
            xmake {
                // 追加设置全局 c 编译 flags
                cFlags "-DTEST"

                // 追加设置全局 c++编译 flags
                cppFlags "-DTEST", "-DTEST2"

                // 设置切换编译模式,与`xmake f -m debug`的配置对应,具体模式值根据自己的 xmake.lua 设置而定
                buildMode "debug"

                // 设置需要编译的 abi 列表,支持:armeabi, armeabi-v7a, arm64-v8a, x86, x86_64
                // 如果没有设置的话,我们也支持从 defaultConfig.ndk.abiFilters 中获取 abiFilters
                abiFilters "armeabi-v7a", "arm64-v8a"
            }
        }
    }

    externalNativeBuild {
        xmake {
            // 设置 jni 工程中 xmake.lua 根文件路径,这是必须的,不设置就不会启用 jni 编译
            path "jni/xmake.lua"

            // 启用详细输出,会显示完整编译命令行参数,其他值:verbose, warning, normal
            logLevel "verbose"

            // 指定 c++ stl 库,默认不指定会使用 c++_static,其他值:c++_static/c++_shared, gnustl_static/gnustl_shared, stlport_static/stlport_shared
            stl "c++_shared"

            // 设置 xmake 可执行程序路径(通常不用设置)
            // program /usr/local/bin/xmake

            // 禁用 stdc++库,默认是启用的
            // stdcxx false

            // 设置其他指定的 ndk 目录路径 (这是可选的,默认 xmake 会自动从$ANDROID_NDK_HOME 或者`~/Library/Android/sdk/ndk-bundle`中检测)
            // 当然如果用户通过`xmake g --ndk=xxx`配置了全局设置,也会自动从这个里面检测
            // ndk "/Users/ruki/files/android-ndk-r20b/"

            // 设置 ndk 中 sdk 版本
            // sdkver 21
        }
    }
}

Compile JNI

Compile JNI and generate APK

When the gradle-xmake-pluginplug-in is applied, the xmakeBuildtask will be automatically injected into the existing assembletask, and the jni library compilation and integration will be automatically performed.

$ ./gradlew app:assembleDebug
> Task :nativelib:xmakeConfigureForArm64
> Task :nativelib:xmakeBuildForArm64
>> xmake build
[ 50%]: ccache compiling.debug nativelib.cc
[ 75%]: linking.debug libnativelib.so
[100%]: build ok!
>> install artifacts to /Users/ruki/projects/personal/xmake-gradle/nativelib/libs/arm64-v8a
> Task :nativelib:xmakeConfigureForArmv7
> Task :nativelib:xmakeBuildForArmv7
>> xmake build
[ 50%]: ccache compiling.debug nativelib.cc
[ 75%]: linking.debug libnativelib.so
[100%]: build ok!
>> install artifacts to /Users/ruki/projects/personal/xmake-gradle/nativelib/libs/armeabi-v7a
> Task :nativelib:preBuild
> Task :nativelib:assemble
> Task :app:assembleDebug

Forced reconstruction of JNI

$ ./gradlew nativelib:xmakeRebuild

Guess you like

Origin www.oschina.net/news/114960/xmake-gradle-1-0-7-released