I'm trying to include a c file, ImageProc into my java file to use it's functions.
I got the code from a MjpegInputStream file (where everything is supposed to work, but of course its never that easy)
Here is the code that crashes (somehow the catch isn't catching it)
static {
try {
System.loadLibrary("ImageProc");
}
catch (Exception e){
e.getMessage();
}
}
The makefile is:
include $(CLEAR_VARS)
LOCAL_MODULE := ImageProc
LOCAL_SRC_FILES := ImageProc.c
LOCAL_LDLIBS := -llog -ljnigraphics
LOCAL_SHARED_LIBRARIES := prebuilt-libjpeg
LOCAL_C_INCLUDES := external/include jni/external/include
So to my understanding I should just be able to use ImageProc.
I know it went through and compiled the c file because at first it couldn't find the h file references (fixed now).
One other thing i'm not sure about is that I call the function pixeltobmp which in the c file is defined as Java_com_camera_simplemjpeg_MjpegInputStream_pixeltobmp which strikes me as odd. I tried changing the name of the method to include the package i'm using rather than the one I copied it from, but that didn't fix anything.
Any help is appreciated.
Additional info: I'm using gradle build 2.10
1. Compile your library
2. Check if you have the .so compiled library(this is important!)
3. In your gradle file add this:
android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
sourceSets.main {
jni.srcDirs = []
File configFile = file('pathToYourLibsFolder')
jniLibs.srcDir configFile.absolutePath
}
Then it should work.
In build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
...
}
buildTypes {
...
}
ndk {
moduleName "ImageProc"
// stl "stlport_static"
// ldLibs "log"
}
}
}
As an alternative option, the code that you're using can be easily modified to remove the dependency on OpenCV (or it can now 2 years after you asked the question).
MjpegInputStream.java has two relevant methods, readMjpegFrame() and readMjpegFrame(Bitmap bmp).
Only the later relies on OpenCV and therefore the native library dependency. If you modify the code to remove this method, and then remove:
static {
System.loadLibrary("ImageProc");
}
public native int pixeltobmp(byte[] jp, int l, Bitmap bmp);
public native void freeCameraMemory();
You will no longer have this problem and can still use readMjpegFrame() to get the content you want.
Whether this is less performant, I'm not certain, but it's working fine in my use case.
Related
I am working on a app for school in android studio using java
I want to generate multiple apk for school with their school name and logo, (about 50+ schools)
Is there any way to save time generating one by one
in future need to update all
Read official guideline about productFlavors.
Creating product flavors is similar to creating build types: add them
to the productFlavors block in your build configuration and include
the settings you want. The product flavors support the same properties
as defaultConfig—this is because defaultConfig actually belongs to the
ProductFlavor class.
productFlavors {
elvispresley {
applicationId 'your_package_id'
versionCode 1
versionName '1.0'
}
whitneyhouston {
applicationId 'your_package_id_2'
versionCode 2
versionName '2.0.1'
}
projectOne {
applicationIdSuffix ".one"
}
projectTwo {
applicationIdSuffix ".two"
}
}
I'm updating an app, to add Android 8+ support, I was trying to get the app Notifications to work properly (using the now required Channels), but for some reason I can't use the new NotificationCompat.Builder constructor (Context, String).
I've already updated my buildToolsVersion (28.0.3 at the moment), as seen here, yet I'm still unable to use the new constructor.
//IDE won't let me use:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channelId");
//While this works fine:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Any ideas on why this could be happening? I already tried to find a solution, but most of them just tell you to update the buildToolsVersion.
Relevant build.gradle code:
android {
compileSdkVersion 27
buildToolsVersion '28.0.3'
defaultConfig {
...
minSdkVersion 16
targetSdkVersion 27
...
}
dependencies {
...
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
...
}
I found out what was going on, turns out that the build.gradle file had a configuration script that was overriding all my support dependencies versions to use the v25.3.0. After removing that script the correct lib was imported and the problem was solved.
//This script
...
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '25.3.0'
}
}
I have a project in Android Studio with a couple of modules.
The app module. With apply plugin: 'com.android.application'.
The androidLibrary module. With apply plugin: 'com.android.library'.
The javaLibrary module. With apply plugin: 'java'.
I want to declare some variables in the project gradle.properties file and be able to read them from the javaLibrary module.
I have declared the properties in the following ways according this documentation...
mysuperhost=superhost
systemProp.mysuperhost=superhost
ORG_GRADLE_PROJECT_mysuperhost=superhost
org.gradle.project.mysuperhost=superhost
... and tried to read them this way with no success:
System.getenv("mysuperhost");
System.getProperty("mysuperhost");
I know how to read properties from the BuildConfig class, but this is a generateed class in the app module (with apply plugin: 'com.android.application'), so this does not work for this particular case.
If you have some value inside of the your gradle.properties file like
mysuperhost=superhost
then write the following lines in the your build.gradle file (to grab property from gradle.properties file and add it into BuildConfig.java class):
// ...
// ...
android {
// Just for example
compileSdkVersion 23
// Just for example
buildToolsVersion "23.0.2"
// ...
// ...
defaultConfig {
// Just for example
minSdkVersion 14
// Just for example
targetSdkVersion 23
// This is the main idea
buildConfigField('String', 'MY_SUPER_HOST', "\"${mysuperhost}\"")
// ...
// ...
}
// ...
// ...
}
// ...
// ...
After that build your project and you are able to use your value via BuildConfig.MY_SUPER_HOST
Your statement about being unable to use BuildConfig is not entirely accurate, as you can use Java reflection to find the public static members of BuildConfig as long as you know its fully qualified package.
Say the full package name of your generated BuildConfig is com.company.app.BuildConfig. You can get its Class object with:
Class<?> klass = Class.forName("com.company.app.BuildConfig");
Then you can use that Class object to pick out the fields of it by name:
Field field = klass.getDeclaredField("BUILD_TYPE");
Then you can get its value:
String value = field.get(null);
I'm using Android Studio 1.1.0. When I try to sync my Gradle file, I get the following error:
Gradle DSL method not found:storeFile()
Here is my gradle configuration:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "skripsi.ubm.studenttracking"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
signingConfigs {
release {
storeFile (project.property("Students_tracking_keystore.jks") + ".keystore")
storePassword "####"
keyAlias "####"
keyPassword "#####"
}
}
}
Can anyone help?
A couple of things to note:
The storeFile DSL method has the following signature:
public DefaultSigningConfig setStoreFile(File storeFile)
i.e. it expects a File to be passed in. You probably need to place a File constructor in your code to ensure you are actually creating a File object. Because you're currently not passing in a file, Gradle is complaining that it can't find a method with the appropriate signature.
Secondly, you are currently appending two suffices to the filename: .jks and .keystore. You should only include one of these based on the suffix of the file you are referencing (it's probably .jks, but you should check to be sure).
In short, one of the following replacement lines will probably work for you:
storeFile file(project.property("Students_tracking_keystore") + ".keystore")
or
storeFile file(project.property("Students_tracking_keystore") + ".jks")
or
storeFile file(project.property("Students_tracking_keystore.keystore"))
or
storeFile file(project.property("Students_tracking_keystore.jks"))
I'm trying to add native code to my Android app. NDK worked fine with simple C++ code like "Hello from C++", but I need to use openCV, and I am stuck.
Project build output:
:app:generateArmDebugSources UP-TO-DATE
:app:compileArmDebugJava UP-TO-DATE
:app:compileArmDebugNdk
D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::Mat::~Mat():jni_part.cpp(.text._ZN2cv3MatD2Ev+0x3c): error: undefined reference to 'cv::fastFree(void*)'
D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::Mat::release():jni_part.cpp(.text._ZN2cv3Mat7releaseEv+0x6c): error: undefined reference to 'cv::Mat::deallocate()'
D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::FastFeatureDetector::~FastFeatureDetector():jni_part.cpp(.text._ZN2cv19FastFeatureDetectorD1Ev+0xa0): error: undefined reference to 'cv::FeatureDetector::~FeatureDetector()'
...25+ similar lines...
collect2: ld returned 1 exit status
make.exe: *** [D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/libjni_part.so] Error 1
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileArmDebugNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
D:\android-ndk-r10c\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\Android.mk APP_PLATFORM=android-21 NDK_OUT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj NDK_LIBS_OUT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\lib NDK_DEBUG=1 APP_STL=stlport_static APP_ABI=armeabi-v7a
Error Code:
2
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileArmDebugNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
D:\android-ndk-r10c\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\inter
mediates\ndk\arm\debug\Android.mk APP_PLATFORM=android-21 NDK_OUT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj NDK_LIBS_OUT=D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\lib NDK_DEBUG=1 APP_STL=stlport_static APP_ABI=armeabi-v7a
Error Code:
2
Output:
D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::Mat::~Mat():jni_part.cpp(.text._ZN2cv3MatD2Ev+0x3c): error: undefined reference to 'cv::fastFree(void*)'
D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::Mat::release():jni_part.cpp(.text._ZN2cv3Mat7releaseEv+0x6c): error: undefined reference to 'cv::Mat::deallocate()'
D:/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/objs-debug/jni_part/D_\AndroidStudioProjects\OpenCVFeatureDetection\app\src\main\jni\jni_part.o: in function cv::FastFeatureDetector::~FastFeatureDetector():jni_part.cpp(.text._ZN2cv19FastFeatureDetectorD1Ev+0xa0): error: undefined reference to 'cv::FeatureDetector::~FeatureDetector()'
...and 25+ similar lines again...
collect2: ld returned 1 exit status
make.exe: *** [D:\AndroidStudioProjects\OpenCVFeatureDetection\app\build\intermediates\ndk\arm\debug\obj/local/armeabi-v7a/libjni_part.so] Error 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 12.261 secs
I noticed a similar question https://stackoverflow.com/a/22427267/4595220 but I still haven't found a solution. What I am doing wrong?
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.gogiant.opencvfeaturedetection"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName "jni_part"
cFlags "-DANDROID_NDK"
ldLibs "log"
stl "stlport_static"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
// make per-variant version code
applicationVariants.all { variant ->
// get the single flavor
def flavorVersion = variant.productFlavors.get(0).versionCode
// set the composite code
variant.mergedFlavor.versionCode = flavorVersion * 1000000 + defaultConfig.versionCode
}
productFlavors {
x86 {
ndk {
abiFilter "x86"
}
// this is the flavor part of the version code.
// It must be higher than the arm one for devices supporting
// both, as x86 is preferred.
versionCode = 3
}
arm {
ndk {
abiFilter "armeabi-v7a"
}
versionCode = 2
}
mips {
ndk {
abiFilter "mips"
}
versionCode = 1
}
fat {
// fat binary, lowest version code to be
// the last option
versionCode = 0
}
}
debug.jniDebuggable true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
compile project(':libraries:opencv')
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=off
include D:\OpenCV-2.4.9-android-sdk\sdk\native\jni\OpenCV.mk
LOCAL_C_INCLUDES += D:\AndroidStudioProjects\OpenCVFeatureDetection\libraries\opencv\include
LOCAL_MODULE := jni_part
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_STATIC_LIBRARIES += libopencv_contrib libopencv_legacy libopencv_ml libopencv_stitching libopencv_nonfree libopencv_objdetect libopencv_videostab libopencv_calib3d libopencv_photo libopencv_video libopencv_features2d libopencv_highgui libopencv_androidcamera libopencv_flann libopencv_imgproc libopencv_ts libopencv_core
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := all
APP_PLATFORM := android-8
Project structure: http://i.stack.imgur.com/Yg5Jl.png
Complete project: https://yadi.sk/d/brR5GUjber2Qo
Thanks for any help.
I've tested your project. To make it build successfully, I had to delete the folder app/src/main/jni and the folder app/src/main/3rdparty and the file opencvlibrary-2.4.9.jar in the folder app/libs/. It looks like you have several opencv libs (thus conflicting with each others) in your project.
Hope this helps.
Have you tried to build your project with Eclipse ? I worked a little with OpenCV on Intellij and had some trouble to make it work, it was also a problem of NDK.
The current NDK support in Android Studio and the Android gradle plugin 1.1.0 is limited and deprecated. Auto-generated Makefiles are used on the fly, and linking an external NDK library (like OpenCV) from your NDK sources isn't supported out-of-the-box.
You should remove your ndk{} block inside build.gradle and deactivate the current NDK integration, in order to rely directly on ndk-build and your Makefiles instead. You can also use splits instead of flavors to generate per-ABI APKs, like in this gist: https://gist.github.com/ph0b/9e59058ac59cac104398
Your build.gradle would be like this:
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.gogiant.opencvfeaturedetection"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] //disable automatic ndk-build call
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
splits {
abi {
enable true
reset()
include 'x86', 'armeabi-v7a', 'mips' //select ABIs to build APKs for
universalApk true //generate an additional APK that contains all the ABIs
}
}
project.ext.versionCodes = ['armeabi':1, 'armeabi-v7a':2, 'arm64-v8a':3, 'mips':5, 'mips64':6, 'x86':8, 'x86_64':9] //versionCode digit for each supported ABI, with 64bit>32bit and x86>armeabi-*
android.applicationVariants.all { variant ->
// assign different version code for each output
variant.outputs.each { output ->
output.versionCodeOverride =
project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + defaultConfig.versionCode
}
}
// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
compile project(':libraries:opencv')
}