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.
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.
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 }
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 } }
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!