1

I'm looking to link an INDIVIDUAL source file to an Android Studio project. I'm using the new Android Studio 1.3+ with NDK support.

In my module's build.gradle file, I'm able to link entire source FOLDERS via:

android{
   sourceSets {
      main.jni.srcDirs += 'C:/users/jforce/native'
   }
}

The above blocks will add all native source files in the 'native' folder.

However, I'm looking to add individual source files. eg. JUST a file located at C:/users/jforce/native/test.c, without adding any neighboring files in the same folder.

Here's what I've tried so far:

main.jni.sourceFiles.getFiles() += "C:/users/jforce/native/test.c"

Android Studio does not like this one. The left operand is underlined with red and 'Invalid value to assign to' is displayed when I mouse over. This is confusing to me, because the Android/Gradle documentation says that this method returns a Set with generic param File. sourceSets.main.jni.srcDirs also returns a Set with generic param File, and I'm able to legally use the += operator on that Set, but not here.

https://docs.gradle.org/current/javadoc/org/gradle/api/file/FileCollection.html#getFiles()

http://google.github.io/android-gradle-dsl/1.4/com.android.build.gradle.api.AndroidSourceDirectorySet.html

Okay, so then I tried this:

main.jni.sourceFiles.getFiles().add("C:/users/jforce/native/test.c")

This failed silently. My project builds without error, but the source file isn't added to my project.

As a Hail Mary, I then tried this:

main.jni.sourceFiles.join("C:/users/jforce/native/test.c")

Similar to the last attempt, this fails silently as well. The file isn't added.

Does anyone know how to properly link an INDIVIDUAL native source file to an Android Studio 1.3+ project? Any help will be greatly appreciated. Thanks!

jforce
  • 41
  • 7
  • You can also try the hybrid Android.mk + grade plugin approach as demonstrated for *[define LOCAL_SRC_FILES in ndk{} DSL](http://stackoverflow.com/a/32640823/192373)* – Alex Cohn Oct 06 '15 at 19:38

1 Answers1

1

I figured this out.

android{
   sourceSets {
      main.jni.srcDirs += 'C:/users/jforce/native/test.c'
   }
}

Works. No other files in the 'native' folder are added. Very counter-intuitive with the sourceFiles property exposed, but I've got it working now!

jforce
  • 41
  • 7
  • How do you link the files into the app. Do you add the sourcesets as a part of a library module, or how do you implement the sourcefiles in the app? – miqueloi Jul 14 '16 at 13:23