Gradle not picking the libraries which are copied dynamically under lib folder - android

I have a requirement to load the different runtime libraries for a apk execution and unit test cases execution.
I am familiar with compile and testCompile scopes but problem is I don't want to load specific compile scope libraries while running the unit tests.
So I came up with some build.gradle like the below. I could able to copy the required jars/aars under the libs folder but these are not considered for class path.
Any thing wrong with this script?
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "example.com.myapplication"
minSdkVersion 21
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
task getDynamicJars(type: Copy) {
delete fileTree("$projectDir/libs/")
if(project.gradle.startParameter.taskNames.contains("test")){
println("Loading Unit test dependencies");
from "$rootDir/runtimeLibs/test/"
into "$projectDir/libs/"
include '**/*.*'
}else{
println("Loading Execute dependencies from:"+"$projectDir/runtimeLibs/execution/"+" To:"+"$projectDir/libs/");
from "$rootDir/runtimeLibs/execution/"
into "$projectDir/libs/"
include '**/*.*'
}
}
dependencies {
println("Loading dependencies");
compile fileTree(dir: "libs", include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.0'
testCompile 'junit:junit:4.12'
}
project.afterEvaluate {
preBuild.dependsOn getDynamicJars
}

How do you like this solution:
def libsFolder
// ..
buildTypes {
release {
// ..
libsFolder = "${releaseLibsDir}"
}
debug {
// ..
libsFolder = "${debugLibsDir}"
}
}
// ..
dependencies {
compile fileTree(dir: "${libsFolder}", include: ['*.jar'])
}

Related

It says "Trying to load lib" and then application quit without any information

I have download an android program which used tensorflow to realized the emotion recognition. I have finished the work of configuration of environment. And I find that when I use the recognition the application the log says that "Trying to load lib /data/app-lib/com.example.alex.opencvdemo-1/libtensorflow_inference.so 0xa4f77c88" and then application will quit. I don't know how to solve this problem
Here is my code for loadLibrary
static {
//load libtensorflow_inference.so
//System.load("/app/com.example.alex.opencvdemo/libs/libtensorflow_interface.so");
System.loadLibrary("tensorflow_inference");
Log.e("tensorflow","libtensorflow_inference.so is successfully load");
}
And following is my build.gradle of app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.alex.opencvdemo"
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
multiDexEnabled true
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
buildTypes {
debug {
ndk { abiFilters "armeabi-v7a","x86"}
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets{
main {
jni.srcDirs = []
//jni库的调用会到资源文件夹下libs里面找so文件
jniLibs.srcDirs = ['libs']
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
compile project(':openCVLibrary342')
implementation files('libs/libandroid_tensorflow_inference_java.jar')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
destinationDir file("$buildDir/native-libs")
baseName 'native-libs'
from fileTree(dir: 'libs', include: '**/*.so')
into 'lib/'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
Possible causes as i faced this in earlier
Better check that your native library matches with
"tensorflow_inference".
Because in your build.gradle it showsing native_lib something like that.
verify tensorflow_inference is added in Cmakelists.txt.
if your using openCV make sure you're imported native libraries fine

Migrating to gradle 3.+ with libraries that compile different flavors

I have a very complex project with many libraries that are dependent on each other. I have gone through all of the documentation and videos but nothing is pointing me in the right direction to compile libraries based on flavors. I am confused with the project aspect. If anyone can point me in the right direction to update compile to implementation, that would be great. How do I directly replace configuration: to match the flavors?
Here in an example of two gradles.
vnfmdata
android {
compileSdkVersion build_versions.compile_sdk
buildToolsVersion build_versions.build_tools
defaultConfig {
minSdkVersion build_versions.min_sdk
targetSdkVersion build_versions.target_sdk
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
publishNonDefault true
flavorDimensions flavor.default
productFlavors {
regular {}
no_meridian {}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
regularCompile project(':vncore')
regularCompile project(path: ':vnlocationservice', configuration: 'meridianDebug')
no_meridianCompile project(':vncore')
no_meridianCompile project(path: ':vnlocationservice', configuration: 'no_meridianDebug')
}
vnlocationservices
android {
compileSdkVersion build_versions.compile_sdk
buildToolsVersion build_versions.build_tools
defaultConfig {
minSdkVersion build_versions.min_sdk
targetSdkVersion build_versions.target_sdk
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
publishNonDefault true
productFlavors {
no_meridian {}
meridian {}
}
buildTypes {
release {
//minifyEnabled false
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile deps.support.app_compat
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
no_meridianCompile project(':vncore')
meridianCompile project(':vncore')
meridianCompile project(':third:Sas-Android')
//Localytics
meridianCompile deps.support.compat_v26
meridianCompile deps.play.ads
meridianCompile deps.play.location
meridianCompile deps.localytics
///////////////////
meridianCompile 'com.arubanetworks.meridian:meridian:+#aar'
}
edit:
I found that adding a dependencies node into a flavor affected the other flavor. Instead it is better to use <flavor's name>Implementation.
As you stated, you probably went into the Migration Guide resolve matching errors.
vnfmdata
Here the changes are located on the flavors and how the dependencies changed:
android {
...
productFlavors {
regular {
// Forces regular's flavor to point on LocationService meridian's flavor
// because their flavors' name are different
matchingFallbacks = ["meridian"]
}
no_meridian {
// Will automatically point on LocationService no_meridian's flavor
// because they both have the same name
}
}
...
}
dependencies {
// We used the flavors' matching feature
// so gradle knows that if you select regular, you wants the meridian flavor on these 2 projects
implementation project (":vncore")
implementation project (":vnlocationservice")
}
vnlocationservices
Here we see how to declare a dependency which is only use by one flavor.
android {
...
productFlavors {
meridian {}
no_meridian {}
}
...
}
dependencies {
implementation project (":vncore")
meridianImplementation project(':third:Sas-Android')
//Localytics
meridianImplementation deps.support.compat_v26
meridianImplementation deps.play.ads
meridianImplementation deps.play.location
meridianImplementation deps.localytics
///////////////////
meridianImplementation 'com.arubanetworks.meridian:meridian:+#aar'
}
}

maven paho-releases doesn't compile in gradle

I'm trying to use paho Mqtt's android client in android studio and using gradle to add dependencies, I use the following in app build.gradle:
repositories {
maven {
url "https://repo.eclipse.org/content/repositories/paho-releases/"
}
}
dependencies {
compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
exclude module: 'support-v4'
}
}
yet this is giving me failed to resolve error.I tried with SNAPSHOT version (1.0.3 and I got the same error. I've tried as many scripts as googling yielded to no result.
any help is greatly appreciated.
my refrence is :
http://www.hivemq.com/blog/mqtt-client-library-enyclopedia-paho-android-service
this is my build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.sayres.mqttapp"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
maven {
url "https://repo.eclipse.org/content/repositories/paho-releases/"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
exclude module: 'support-v4'
}
}
Remove this from your app build.gradle and added to project build.gradle by allprojects
repositories {
maven { url "https://repo.eclipse.org/content/repositories/paho-releases/" }
}

Execution failed for task :app:transformClassesAndResourcesWithProguardForRelease

I am trying to release my Android application with Gradle.
app build Grade::
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "xxxx.xxxxx"
minSdkVersion 9
targetSdkVersion 24
versionCode 1
versionName "2.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
}

ERROR: Error:(3, 10) 'opencv2/opencv.hpp' file not found

I am using Android studio 2.2.3 and OpenCV 3.1.0. i resolved many errors but could not find solution for this one.
This is my Build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.example.ahmedarif.fyp3"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:\\Android\\sdk1\\ndk-bundle/ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jniLibs/'] } }
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile project(':openCVLibrary310')
}
}
I also tried buigl.gradle from ph0b
but that build.gradle showing error about ndk-bulid.cmd. I tried almost every thing but didn't work for me. help to get rid of this. Tahnx
And then my build.gradle ;
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.c.a.engineer.imageprocessingpre"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { jni.srcDirs = []
jniLibs.srcDir 'src/main/jniLibs' } }
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile project(':openCVLibrary310')
}
It's work for me. You can try this. And I developed image processing app. This my app screenshot;
If you are using latest version of android studio just use its built in support for c and c++ from here
And i am usnig the same build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.example.ahmedarif.fyp3"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:\\Android\\sdk1\\ndk-bundle/ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jniLibs/'] } }
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile project(':openCVLibrary310')
}
}

Categories

Resources