NDK: How to reference native sources OUTSIDE Android project? - android-studio

I'm using Android Studio 1.2.2 and just downloaded 1.3RC3, as it reputedly has full NDK support.
I have a working project while my .c / .h files remain in app/src/main/jni; however, I want them in AndroidProject/../common/src, i.e. outside the android project folder. How do I make this work?
To reiterate: this question is about EXTERNAL, NATIVE CODE; not importing external java!

This was so simple, I didn't think to even try it at first, but it worked at once:
android{
...
sourceSets.main.jni.srcDirs = ['../../common/test']
...
}
...which implies that the path is relative to AndroidProject/app.
A problem with this solution is that it is incompatible with a standalone Android.mk, since preventing Android.mk generation by gradle requires sourceSets.main.jni.srcDirs = [].

Related

Android Studio “cannot resolve symbol R” but project compiles and runs

Android Studio displays in all java files in red : “cannot resolve symbol R” but the project compiles and runs.
I have tried every solution here:
Android Studio "cannot resolve symbol" but project compiles and works, but with no luck.
I tried to :
invalidate caches and restart android studio
delete .gradle and .idea folders
clean and rebuild the project
install a new version of android studio
mess up with gradle file then undo changes and sync project
One thing that I want to try, but I don't know how to achieve, is to reopen the project from scratch as I was opening it for the first time, but I don't know how to do this.
I also tried answers from here:
Android Studio says "cannot resolve symbol" but project compiles,
but again with no luck.
Android studio version = 3.2.1
dependency versions in gradle project file:
classpath 'com.android.tools.build:gradle:3.4.0-alpha01'
classpath 'com.google.gms:google-services:4.0.0'
For some reason that i do not understand ,
this combination of build versions made the issue :
grade version = 4.10.1
classpath 'com.android.tools.build:gradle:3.4.0-alpha01'
when i switched to these build versions :
grade version = 4.6
classpath 'com.android.tools.build:gradle:3.2.1'
The issue was solved !
File -> Close project
Open an existing Android Studio Project
Open you project
Hope it will work.
sometimes, R file is not generated because of package name on android manifest is not match with package module that you have.
Is any wrong syntax or spelling in your xml?
Check your layout or any xml file.
Android Studio seems to have a caching issue with R.java occasionally. I rarely have a problem with this, but when I do, I actually open R.java (double press shift and type "R.java") or navigate to R.java under "app/build/generated/source" directory, opening the file and checking if the relevant XML id has been created. If it has, it forces Android Studio to now recognise the id's that are not being resolved. It's quick to try, and doesn't require clean and rebuild.
From your programs menu, open android studio. instead of opening your project from the recent files, select to open a project from your computer and then locate the path to your project. When all else fails, sometimes this works.
Also, try commenting out the support libraries from your gradle implementation, sync your project, and after sync fails comment them back in and sync again. (not sure if that's what you tried already when you said you messed with the gradle file)
In my case,
I am using
- Android Studio version 3.3.1 at home
- Android Studio version 3.2.1 at office
When i pulled projects to my office computer that are firstly created at my home computer ,
Android Studio can not resolved R file but runs application with no error. Because projects gradle and Android Studio version are incompatible.
Only thing that you sholud do, change the gradle version in project level gradle file
classpath 'com.android.tools.build:gradle:3.3.1'
to
classpath 'com.android.tools.build:gradle:3.2.1'
I was facing same issue,
first thought the issue might because of some xml file or naming of drawable resources incorrectly. After analysing, this case wasn't applicable to me.
So
Updating Android studio from older version 3.2 to newer version 3.3.2 along with new build tool version from SDK manager resolved this issue
for me.
I've tried invalidate cache and Restart AS with clean rebuild all the options but didn't work for me.
It says cannot resolve symbol, but it can run.
In my case, I just reload the needed *.jar files to the libs folder.
File >> invalidate caches/restart
Rebuild Project
Sync
Run
Works for me!
It looks like the library did not load my "imports" properly the first time.
That's why the import methods cannot be seen in my main_activity.xml.
This happened to me when I was going back and forth between different SDK versions.
Apparently SDK manager copied all the source files but it didn't finish cleanly. As a result I could compile and run my project just fine but IDE didn't recognize the SDK and reported all java symbols unresolved.
None of the above and other solutions in SO didn't work for me, but just uninstalling/reinstalling the specific SDK version did a job.
If you renamed your package (inside java folder), make sure you change your Manifest package name to the same
The only thing that worked for me was,
replacing,
import package_name.R
with
import package_name.*
simply go to project settings : settings.gradel and change the rootProject.name to your current name
rootProject.name = "write the project name here"

Android NDK - building native libraries without Android Studio

I'm working on a c/c++ cross-platform project, constructed of 2 main libraries (with a few external dependencies: ssl, yajl, fribidi).
The android solution will include Java files and a JNI layer, all bundled in a AAR file (including assets and the native libs).
I managed to build the whole project, but in a very awkward way:
I created a 'hello world' Android app', with native support, from within Android Studio, and added all native dependencies to the CMAkeList.txt. I added my Java code + JNI and managed to create the AAR (only for ARM, for now).
Now I need to separate the build of the different libraries, to their separate projects, respectively: libA, libB and C.aar.
How is it done without the IDE (and via command-line)?
There's the stand-alone NDK, the make_standalone_toolchain.py script, android.toolchain.cmake and other options, but none are documented or up-to-date. Most documentation still talks about the outdated Android.mk methodology.
I'd presume including android.toolchain.cmake in my CMakeList.txt, which will set all needed environment...
I'm using the newest Android Studio 3.0.1 and NDK r16b (installed via SDK Manager)
Alex - thanks, exactly what I was looking for. Just had to add a few flags and a call to make:
> cmake -G "MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%\build\cmake\android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=android-19 -DCMAKE_MAKE_PROGRAM=%ANDROID_NDK%\prebuilt\windows-x86_64\bin\make.exe -DCMAKE_BUILD_TYPE=Release -DANDROID_ABI="armeabi-v7a with NEON" ..
> cmake --build .
Android Studio (the standard Android gradle plugin, that is) does not support native-only modules, but you can split your CMake script and work with libA and libB separately. You can run cmake from command line (but better use the version that is shipped with Android SDK).
sdk/cmake/3.6.4111459/bin/cmake -DCMAKE_TOOLCHAIN_FILE=sdk/ndk-bundle/build/cmake/android.toolchain.cmake ...
The easiest way to build the AAR file that includes a compiled Java wrapper and the two native libraries would be with Android Studio, but you can run the gradle task from command line. This is what we typically do on a build server.

android studio - prebuilt .so files in jniLibs disappear every time i make the project

I use Android Studio 1.5. I use ndk and I write my .so file and that's OK. The problem: I need some other prebuilt .so files I wrote with vim. I just put the .so files in the jniLibs/armeabi-v7a. However, whenever I make the project, the other prebuilt .so files disappear, and only the one I write with ndk stays.
Does the problem come from the content I write in Android.mk or build.gradle? In build.gradle, I write sourceSets.main.jni.srcDirs = [].
Could anyone help?

Android NDK UnsatisfiedLinkError: findLibrary returned null

Since two days, I get this error :
java.lang.UnsatisfiedLinkError: Couldn't load MyCPPProject from loader dalvik.system.PathClassLoader[dexPath=/system/framework/com.google.android.maps.jar:/data/app/com.mypackage.myApp-3.apk,libraryPath=/data/app-lib/com.mypackage.myApp-3]: findLibrary returned null
I'd like to compile a project c ++ natively in android application.
The project compiles fine on another PC but not on mine.
I tried to create a new workspace, install another eclipse and plenty others solutions but it still does not compile.
How can I fix it ?
The android project generate myApp.apk but not myApp-3.apk.
[dexPath=/system/framework/com.google.android.maps.jar:/data/app/com.mypackage.myApp-3.apk,libraryPath=/data/app-lib/com.mypackage.myApp-3]
Why compiler try to load myApp-3.apk ?
I fixed this issue by creating armeabi inside libs folder. And copied the .so files into armeabi folder.

Imported jar in gradle, compiles, but not recognized by Android Studio (0.2.10)

This is an annoying one.
I have a simple project structure which is based on Gradle, very similar to other projects I have.
The project has these imports:
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile files('libs/jackson-annotations-2.1.0.jar')
compile files('libs/jackson-core-2.1.0.jar')
compile files('libs/jackson-databind-2.1.0.jar')
compile files('libs/zip4j_1.3.1.jar')
compile files('libs/httpclient-4.1.3.jar')
compile files('libs/httpclient-cache-4.1.3.jar')
compile files('libs/httpcore-4.1.4.jar')
compile files('libs/httpmime-4.1.3.jar')
compile project(":grid_lib")
}
Which works well.
The only problem is that the Android Studio does not recognize those classes, so they are all in red and have no intel.
Very annoying!
I've been banging my head against the wall for too long, so I'm trying here now. What do you say guys? Any ideas?
Thanks :-)
Edit
I've answered my own questions, for those who find the same problem.
well, to answer my own question...
Sometimes Android Studio's Gradle goes haywire, and I found a simple fix:
Restart Android Studio
Create a new project (doesn't matter where or name)
Wait for it finish downloading its gradle stuff
Restart android studio
For other gradle problems, an additional step may be required, which is copying the gradle wrapper (gradlew files, and gradle folder, not the .gradle folder) from the newly created project to your own project, and re-import your project.
You can now delete the new project... of course.
Edit:
Gradle's version keep changing.
If you are still having trouble, try this-
Edit your gradle/wrapper/gradle-wrapper.properties file, and update the distribution url's version. Currently it should be 1.8, but it may change in the future. So you can use a "new project" to find out what the version should be. Example: distributionUrl=http\://services.gradle.org/distributions/gradle-1.8-bin.zip
Edit your build.gradle file, and change the minimum version to 0.6.+ - and again it may change in the future. Example for current versions: classpath 'com.android.tools.build:gradle:0.6.+'
Restart your Android Studio and recompile.
I encountered the same problem once I tried to upgrade Crashlytics to Fabric which cost me few hours to solve it.
I tried cleaning the project and rebuilding it from IDE and Terminal, closed Android Studio and opened it again, synced the gradle, checked out to other git branches and so on. Non of them solved the problem!
As a final attempt I closed the project and imported it to Android Studio again and the error was gone!!! I couldn't find any explanation for why it happened but it could be some sort of caching issue on the gradle which was not being cleared in normal ways.
In my case, the problem was that i was using an old version of a jar library, and when updating to a newer version Android Studio was not recognising the classes, but it was compiling anyway. But i got this annoying red font color and underlining.
None of the solutions in this thread worked for me.
The solution for me was adding in the build.gradle the following:
repositories {
flatDir{
dirs 'libs'
}
}
And of course, the common (but these lines come by default):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

Resources