Home > Backend Development > C++ > How to Define LOCAL_SRC_FILES in Android NDK DSL?

How to Define LOCAL_SRC_FILES in Android NDK DSL?

Susan Sarandon
Release: 2024-11-16 04:11:03
Original
964 people have browsed it

How to Define LOCAL_SRC_FILES in Android NDK DSL?

Defining LOCAL_SRC_FILES in NDK DSL

The Android NDK DSL allows you to specify native source files for your module. However, in older versions of the DSL, there was no direct way to manually define LOCAL_SRC_FILES.

Experimental Plugin Solution

With the experimental Android Gradle plugin 0.4.0, you can now exclude source files from the NDK build based on patterns. For example:

android.sources {
    main {
        jni.source {
            srcDirs = ["~/srcs/jni"]
            exclude "**/win.cpp"
        }
    }
}
Copy after login

Workaround for Older Plugins

Unfortunately, for older versions of the DSL, it is not possible to define LOCAL_SRC_FILES directly. Instead, it is recommended to use the traditional Android.mk file to manage your native source files.

You can disable the default NDK build tasks and inject a custom task to compile your native source files:

def ndkBuild = android.ndkDirectory
import org.apache.tools.ant.taskdefs.condition.Os
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
    ndkBuild += '.cmd'
}

task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
    commandLine '$ndkBuild', 'NDK_PROJECT_PATH="$jniSrc/..'
}

task cleanNative(type: Exec, description: 'Clean JNI object files') {
    commandLine '$ndkBuild', 'clean', 'NDK_PROJECT_PATH="$jniSrc/..'
}
clean.dependsOn 'cleanNative'
tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn buildNative
}
tasks.all {
    task -> if (task.name.contains('compileDebugNdk') || task.name.contains('compileReleaseNdk')) task.enabled = false
}
Copy after login

Caveats

Note that this workaround may not produce a debuggable setup. To address this, you can build a static library with ndk-build and link it with the necessary objects to pull symbols from the library.

The above is the detailed content of How to Define LOCAL_SRC_FILES in Android NDK DSL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template