I have added ndk support in my project because I needed to convert from one pixel format to another and I thought it would be faster in native code than in java. Now I realized I don't actually need to convert anything so I no longer need the ndk-build in my project. How do I get rid of android building the *.so files that I no longer use?
in Eclipse follow:
Project -> Properties -> Builders
and disable CDT
Remove complete jni folder.
Also from java remove all the native methods.Remove the libraries that are present under libs/armeabi
Also remove obj/local/armeabi
Related
I am trying to test OpenCV Android, on Android Studio, I am confused about how to include the NDK.
What I want to do is run the samples which come with OpenCV. Of the 6 samples provided I managed to run 4 successfully. The exceptions were face-detection and native-activity.
I suspect the reason is I have not set up my NDK correctly.
Googling I found a bunch of discussions but do not really understand them. This is my first time I am trying to work with both the NDK and OpenCV, and my Gradle understanding is limited.
I set an environment variable in my .bash_profile
export ANDROID_NDK=pathTo/android-ndk-r9
I do not understand how to get this to studio.
I see reference to jniFolder but do not understand what these are and should I care right now.
Stackoverflow.com/questions/17767557
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniFolders = new HashSet<File>()
pkgTask.jniFolders.add(new File(projectDir, 'native-libs'))
}
What am I supposed to do with this paste at the end of my build.gradle file ?
In summation, my questions are.
How do I get Android Studio to read the NDK variable ?
What exactly are the jniFolders ?
Is it enough just to paste at the end of my build.gradle file ?
Google Group Discussions on Gradle and NDK
For anyone coming across this this is how I resolved it apart from Xaviers anwser.
First I read OVERVIEW.html which comes with the NDK, in the docs directory.
I then compiled the .mk and .cpp files into an .so file. I did this inplace in the sample jni directory
This created the .so file in the libs folder which I copied to the destination as given by Xavier.
If you have libraries that you build with the ndk and want to put them in a gradle-enabled android project (using version 0.7.+ of the plugin), you can just put them in
src/main/jniLibs/<abi>/libfoo.so
for example:
src/main/jniLibs/armeabi-v7a/libfoo.so
src/main/jniLibs/x86/libfoo.so
and they'll get packaged automatically.
If you want to keep them in the native-libs folder you can put the following in your gradle file:
android {
sourceSets.main {
jniLibs.srcDirs = ['native-libs']
}
}
All this does really is tell gradle where the jniLibs folder for the main source set is (relative to the project root.)
The snippet you showed is doing something different. It's telling the package task to also include some native libraries. This was a hack that used to work in a previous version using undocumented API of the task that are no longer supported.
i create one Project using NDK-sdk using eclipse, i change "c files inside jni folder" then i run ndk-build.
is it necessary to ndk-build after change in java file also ?
Here is how it goes, you compile only when you change code:
Any Change in C file -> ndk-build -> Re-compile the project so that library gets packaged with the APK -> Re-install on the phone
Any change in the Java File of the Project
Recompile the project -> Re-install on the phone
The shared object library file once generated need not be compiled till any code is modified.
you need to re-build if you modified something in the c part, and if you modified in the jni java part (method definition as this requires modifying c part also)
If you change the code into the c/c++ file then you will have to build that library using ndk-build. You do not have to build the library if you are not changing the c/c++ file.
I maintain an Android app and am not using Eclipse. I am not using Eclipse. I am using ant and build.xml and build.properties.
I have places my .jar file into the libs/ directory. My code compiles just dandy. But when I run it on the emulator, the output APK does not include the .jar, so I get a runtime stacktrace:
ERROR/AndroidRuntime(470): java.lang.NoClassDefFoundError: com.google.ads.AdView
my build.properties looks like this:
jar.libs.dir=libs
And the libs/ directory contains my .jar file.
What needs to be in build.xml so that the external .jar file is included in the APK?
Edit: In theory this answer should work, but it doesn't for me. Is it out of date? What gives? How to add external jar libraries to an android project from the command line
I just came over a similar problem and noticed that libraries should not be placed in "myprojectdir\lib". When I moved them to "myprojectdir\libs" everything started to work.
It turns out that I needed to upgrade the version of ant I was using to 1.8. During the compile process, I had been getting this error message:
Warning: Reference out.dex.jar.input.ref has not been set at runtime,
but was found duringbuild file parsing, attempting to resolve. Future
versions of Ant may support referencing ids defined in non-executed
targets.
I googled it, and found that I needed to upgrade Ant, and now I don't get this warning, and my application does not force close.
What needs to be in build.xml so that the external .jar file is included in the APK?
Just putting it in libs/ is sufficient.
my build.properties looks like this:
That line should not be necessary. It does not appear in my build.properties files that build successfully with JAR files.
If you use dexdump -f classes.dex from your project's bin/ directory, you will be able to determine whether com.google.ads.AdView made it in there. If it did not, then something is strange with your build scripts. If it did, then perhaps there is a dependent JAR that you are missing (though I would expect a VerifyError in that case).
You use 3rd party library, but you seem didn't run DX on it. Make sure that not only your code processed by DX tool (I assume Ant does it), but also all 3rd party libraries you use. You can look in 7Bee script I use to convert web applications to Android davlik format, so it can work for you too. You can find more about the script on Atjeews page.
Solution:
right click on the project in project tree and select Project
properties
select Java Build Path
select TAB Order
and Export
check GoogleAdMobAdsSdk-4.0.4.jar (or your
version SDK)
press OK
clean project by menu Project
-> Clean
rebuild project (Project – Build Automatically)
I have built a dynamic library in android using android build system. This library provides jni interface for functions inside it. Now I want to include this library in an application (.apk). I am using eclipse for application development. Now, how can I use the prebuild dynamice library (.so) in my application ? I tried putting it in a lib folder in my application but it is not working.
Any pointers are appreciated.
I am not using ndk to build my .so.
Since you write 'so' I think you're using NDK. If you're using NDK I don't know the answer.
If you're using the "Java" SDK, then in your library project go to Properties -> Android, and Check "Is Library". In your "apk" project, go to Properties -> Android -> Add . And your Library project should be available.
Also, any Library added in the "Java Build Path" Menu (again, in project properties) should be available in the APK in the end.
I know it's slightly old, but have you checked in the built APK to see if your .so library is there? Should be in the libs/armeabi folder.
Also, your .so file should be in lib/armeabi folder in your eclipse solution. I'm guessing the armeabi bit depends on which processor your .so file is build for.
Also, I know that if your library isn't called lib[name].so, it won't get copied when the apk is installed on the device. So:
libfoo.so copies
foo.so doesn't copy
foo.so doesn't copy
Also, you can use DDMS (its a view in eclipse) and it's file explorer to see if it's been copied to your device. It should be under data/data/[packagename]/lib.
Hope this helps a bit!
Andy.
I hit this same problem while building Qiqqa for Android. Under your eclipse android project, make sure you have a libs directory (not that it is plural libS not singular lib). Inside that create the armeabi/etc subdirs with their respectibe .so files.
Then when you build, eclipse will automatically pick up this libs directory and create the corresponding lib in your apk. System.loadLibrary("XXX") will then resolve to libXXX.so on your correct architecture...
Cheers,
Jimme
I am creating a widget that we will provide to developer end users and it consists of a .jar and a native library (.so) built using the NDK. The JAR has a JNI interface to the dynamic library.
It's very clear on how to include an external .jar in a project but not how to include a dependent dynamic library.
How do I package up and build the .jar and .so? What are the best practices here?
I can create the JAR file using the JDK's jar command. Do I need to run dx.bat on the jar to convert to Dalvik bytecode?
I need to create a sample project showing the widget in action. How do I include this .jar and .so in a sample project that demonstrates how to use the widget?
I spent some time on this, and i just can't understand why isn't this written on wikitude documentation.... anyway follow this changes!
go to windows/preferences/android/build
uncheck the first and the second option
extract files from wikitudesdk.jar with winrar as if it is an archive, search libarchitect.so and copy it in /libs/libs/armeabi/
add wikitudesdk.jar to your build path
You should use the standard build tools included with the SDK for this. If you include the .jar files you need in the /lib directory of your project, the ant build process will convert the included class files to Dalvik bytecode format and include them in your classes.dex file for the app. Add a build.properties file to your project root as well, with one line:
external.libs.dir=lib
Depending on the version of your SDK and configuration of it, you may need to place the jar in libs rather than lib.
As for the .so, I presume that it's properly compiled using the Android NDK, or using a build script that uses the proper compiler and compiler flags that are required to successfully link the shared object on Android. If this is the case, you can include the .so file in libs/armeabi and they will be added in the jar as well. Furthermore, the dynamic library loader will know to look in this location in the .jar when you actually try to load the library from Java code.
Using ADT 12, I accomplished this by doing the following:
1) Export JAR from your library with the SO file using Eclipse. Make sure you exclude AndroidManifest.xml. This will include source code and other data, so if you are distributing, you'll want to strip these unnecessary bits out using any ZIP utility.
2) Create a directory in your App's source tree (I use "lib" directory) and copy your library JAR to it.
3) Right-click project in Eclipse and select "Configure Build Path". Add JAR and point it to JAR inside your App source tree.
4) In your Eclipse preferences, select Android/Build section and uncheck "Automatically refresh Resources and Assets folder on build". You will notice an option below that says "Force error when external jars contain native libraries." There is an ADT bug, which will supposedly be fixed in R17, which uses the wrong setting. Once it is fixed, you will use the "Force error" one (make sure it unchecked).
5) Once you build your app APK, you will have a libs/armeabi/libXXXX.so file. This will not be automatically unpacked on install. You will need to write code to extract it from your APK file into your data directory, then use System.load on the .so file in your data directory.
I have bidirectional JNI calls going from the dynamically loaded file, and even use dlopen() on it to do my custom plugin registration stuff.
Add the below lines to android.mk.
include $(BUILD_PACKAGE)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := alias:libs/your.jar
include $(BUILD_MULTI_PREBUILT)