Home >Backend Development >C++ >Can I define LOCAL_SRC_FILES in NDK DSL?

Can I define LOCAL_SRC_FILES in NDK DSL?

DDD
DDDOriginal
2024-11-23 09:55:17359browse

Can I define LOCAL_SRC_FILES in NDK DSL?

Can define LOCAL_SRC_FILES in NDK DSL?

In order to define LOCAL_SRC_FILES in the Gradle ndk{} block, it is important to note that this feature is not supported by current Gradle plugins. Even the "experimental" plugin only allows directories to be added.

Recommended Approach

Using the traditional Android.mk is recommended for reliably accomplishing this task. Additionally, leaving jni.srcDirs as [${jniSrc}] is suggested to allow Android Studio to display these files for easy access and syntax highlighting.

NDK Build Disabling

If the traditional Android.mk approach is not desired, the regular NDK build tasks can be disabled and a buildNative task can be injected instead:

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
}

Gradle Experimental Plugin

For the 'com.android.tools.build:gradle-experimental:0.2.0' plugin, a similar approach can be taken, but with different task matching:

tasks.all {
    task ->
        if (task.name.startsWith('compile') && task.name.contains('MainC')) {
            task.enabled = false
        }
        if (task.name.startsWith('link')) {
            task.enabled = false
        }
        if (task.name.endsWith("SharedLibrary") ) {
            task.dependsOn buildNative
        }
}

Exclude Files from NDK Build

With experimental plugin 0.4.0, files can be excluded from the NDK build by pattern:

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

The above is the detailed content of Can I define LOCAL_SRC_FILES in 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