Android Studio: Add .cpp file tree without building - android

Is it possible to add a tree of .h/.cpp files to the Android Studio editor for reference - so that they can be edited but not actually built/compiled by Android Studio?
I have JNI/NDK files which I want to build manually from the command line but I don't want Gradle/Android Studio to build these files.
There is a Gradle 'exclude' command/function which may be what I need but I cannot get it to work.
sourceSets {
main {
exclude 'jni'
//exclude ("jni/**")
//jni.srcDirs = []
}
}
I get the following error:
Error:(10, 0) Gradle DSL method not found: 'exclude()'

I didn't manage to figure this out with a vanilla Android Studio set-up but by using the Gradle Experimental Plugin I have ended up with the desired outcome. Once I got the 0.7.0 experimental plugin working (and my build.gradle configured for the new 'model' scoping), I was able to remove the sourceSets statement, successfull sync, edit within Android Studio and build from the command line via ndk-build.

Related

Android : Run CMake manually via gradle

I am working on an android project which use C++ code.
I have added required CMake settings in build.gradle:
externalNativeBuild {
cmake {
path file('jni/CMakeLists.txt')
version '3.18.1'
}
}
But the problem is there are a lot of C++ files. Using gradle for build makes Android Studio index those files and it makes IDE very slow.
Is there a way to invoke CMake task manually outside IDE so that IDE don't index them? Actually what I want is to use a gradle task to build native code via CMake.

Gradle sync fails with cmake "cause: executing external native build for cmake"

I'm trying to build my colleagues' project in Android Studio, which requires CMake SDK to build the external c/cpp files included in the project. The problem I'm running into is despite having installed the LLDB, NDK, and CMake SDK tools through SDK manager, the gradle for the module that references cmake path fails to sync. My colleagues who already have this project installed and working haven't run into this issue, so I suspect that it has to be something in my environment setting.
So far, I've tried uninstalling and reinstalling CMake SDK, refresh linked C++ projects, and removing the reference to the cmake path from the gradle file and adding the reference by right click the moduel -> Link C++ Project with Gradle, but none of these worked.
When I comment out the reference to my CMakeLists.txt in the gradle, it syncs, indicating that the problem is in regards to the reference to the CMake file. I also tried commenting out library references in my CMakeLists.txt file to see if the error is occurring due to a reference in the file, but even when I comment everything out, the gradle fails to sync.
This is what my gradle file looks like.
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
When I press sync (try again) in build.gradle, the error message simply shows
SIMPLE: Error configuring
When I ignore the fact that gradle sync failed and just try to build, the error message shows as this.
Cause: executing external native build for cmake <my_project_path>\src\main\cpp\CMakeLists.txt
Edit: Added a link to the image capture of my Android Studio NDK path (C:\Users\username\AppData\Local\Android\Sdk\ndk-bundle for Windows).
NDK path Capture
In short - Maybe the problem is in the build.gradle file imported together with the project.
try editing 'build.gradle' line 9: classpath 'com.android.tools.build:gradle:3.1.1'
replace the gradle version number (in my case I replaced '3.1.1' with '3.5.0').
In more detail - I had the same issue when cloning from:
https://github.com/farzaa/DracoPortedToAndroid
I did try the above (setting the ndk path) but that wasn't the problem, since the path to the ndk lib was correct. So the most probable error-cause left was some project settings imported with the cloned project.
Try comparing the "Gradle Scripts" (In the project explorer) of the imported project with a new project built in your Android Studio environment. The new project will have the correct local settings.
In my case I replaced:
classpath 'com.android.tools.build:gradle:3.1.1'
with:
classpath 'com.android.tools.build:gradle:3.5.0'
to set the NDK path in android studio go to :
file -> project structure -> sdk location -> android ndk location -> set path for example my ndk location on mac is /Users/username/Library/Android/sdk/ndk-bundle
This could be because you have a shared library in the project which needs to be linked with the shared version of the STL. Try adding the following to your build.gradle:
android {
defaultConfig {
externalNativeBuild {
cmake {
arguments "-DANDROID_STL=c++_shared"
}
}
}
}
I had the same error message, and it turned out that I had a syntax error in my CMakeLists.txt
I have solved the problem by adding 'abiFilters' to app/build.gradle file:
defaultConfig {
externalNativeBuild {
cmake {
abiFilters 'armeabi-v7a','arm64-v8a'
}
}
}

can you help me with this error in android studio?

I'm trying to run a project that is using a C++ module i try everything and nothing works it still get this error.
Error:Execution failed for task ':pulseandroid:compileDebugNdk'.
Error: Your project contains C++ files but it is not using a supported native build system.
Consider using CMake or ndk-build integration with the stable Android Gradle plugin:
https://developer.android.com/studio/projects/add-native-code.html
or use the experimental plugin:
https://developer.android.com/studio/build/experimental-plugin.html.
1 - Make sure NDK is install
2 - Put this part in build.gradle(Module:app) above buildTypes{}
sourceSets {
main {
jni.srcDirs = []
}
}
buildTypes{}

Project builds fine from command line, fails within Android Studio

I have a project that started in Eclipse over a year ago and I'm trying to import and use it within Android Studio. I've exported build.gradle from Eclipse and imported it into Android Studio. The project uses the support library, and while this didn't make it into build.gradle, I have added this line manually:
compile 'com.android.support:appcompat-v7:+'
If I run the build from the command line (./gradlew assembleDebug) it succeeds. Building from within Android Studio fails with errors that sound like it's not picking up the support library:
Error:(3, -1) android-apt-compiler: [module] /Volumes/User/Steve/src/.../android/res/menu/foo_menu.xml:3: error: No resource identifier found for attribute 'showAsAction' in package 'com.company.app'
I just updated Android Studio to the 0.5.9 release this morning. I believe I'm using a recent Gradle. There's this part in my build.gradle in the buildscript section:
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
And then this in gradle/wrapper/gradle-wrapper.properties:
distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-all.zip
So my question is: why do I get different behavior from within the IDE than from the command line? How can I further troubleshoot what is going wrong from within Android Studio so that it runs the build exactly the same?

Error when importing a Gradle project into Android Studio

I am trying to import a Gradle project into Android Studio and get the following error:
This project does indeed compile when I run Gradle from the command line. I can also Open the project in Android Studio (vs Import) but then it tries to build using the Eclipse build script.
The gradle build was created by hand and not via the Eclipse export function.
My problem can be resolved in one of two ways:
1) Tell me how to force Android Studio to use Gradle instead of using the Eclipse build.
2) Help me find the reason for the Gradle import error.
I am using
Android Studio 0.2.2
Gradle 1.6 via gradlew
I finally found out why this didn't work.
There are two ways to include a project from within the settings.gradle file:
include ':..:source:compA-api'
and
include "source:compA-api"
project(':source:compA-api').projectDir = new File(settingsDir, 'source/compA/api')
I had used the second form and apparently Android Studio didn't like that :)

Categories

Resources