Link individual native source file to Android Stud

2019-08-08 14:45发布

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!

1条回答
Evening l夕情丶
2楼-- · 2019-08-08 15:16

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!

查看更多
登录 后发表回答