Home >Backend Development >C++ >Can You Define LOCAL_SRC_FILES in the gradle.build ndk {} DSL?

Can You Define LOCAL_SRC_FILES in the gradle.build ndk {} DSL?

Linda Hamilton
Linda HamiltonOriginal
2024-11-29 00:54:11477browse

Can You Define LOCAL_SRC_FILES in the gradle.build ndk {} DSL?

define LOCAL_SRC_FILES in NDK

Question:

Is it possible to build ndk in gradle. {} Define LOCAL_SRC_FILES in DSL?

Background:

In the jni module gradle.build file, use the following configuration:

android {
    def jniSrc = System.getProperty("user.home") + "/srcs/jni"

    defaultConfig {
        ndk {
            moduleName "core"
            stl "gnustl_shared"
            cFlags "-std=c++11"
        }
    }

    ...

The jni source code contains instructions for different platforms (Android, iOS, WinRT) code. The goal is to exclude code in Android.mk that does not work on Android.

Answer:

Unfortunately, the current gradle plugin does not support defining LOCAL_SRC_FILES in the ndk {} DSL. Here are the workarounds:

Traditional method:

Keep the traditional Android.mk file, which can reliably exclude specific code. It is recommended to use the cppFlags and cFlags settings to have code citations and syntax highlighting in Android Studio.

Experimental approach (buildNative task):

Disable the regular NDK build task and inject a buildNative task to build native code. This works for the experimental plugin 'com.android.tools.build:gradle-experimental:0.2.0'.

Advanced approach (static library):

If your native code is divided into multiple directories (Android specific files and platform independent files), you can consider building a static library , and link it to get the necessary symbols. This approach allows building a static library using ndk-build and using it with the necessary objects at link time.

Update:

In experimental plugins, you can use compileJavaTask.dependsOn buildNative to replace the task compileTask.dependsOn buildNative.

Update 2:

It is possible to link a single local source file to the Android Studio project, thus solving the issue.

The above is the detailed content of Can You Define LOCAL_SRC_FILES in the gradle.build ndk {} DSL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn