Loading shared libraries with Gradle 2.2.1 - android-ndk

I'm trying to use a shared library, libavcodec-56.so, in my Android app and I can't find the way. I'm using Gradle 2.2.1 and Android Studio 1.0. What I have done so far is the following:
-I built libavcodec-56.so from source code using a NDK toolchain.
-I copied libavcodec-56.so into src/main/jniLibs/armeabi
-I'm able to create a .c file in the project and communicate with the java files using private native void nameOfTheNativeMethod()
-I can load some external libraries like ldLibs "m"
However, if I try to use #include <libavcodec/avcodec.h> I get the error
No such file or directory
#include <libavcodec/avcodec.h>
My gradle file looks like:
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName "ffmpeg"
cFlags "-std=c99"
ldLibs "log", "m"
}
}
sourceSets.main {
jni.srcDirs = ["src/main/jni"];
//jniLibs.srcDirs = ['src/main/jniLibs'];
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
Any ideas?
Thanks in advance

the current NDK support with the gradle android plugin is incomplete and now deprecated. You can't have native dependencies between native local libraries.
The only solution with gradle right now is to directly use ndk-build with your own Makefiles to generate all your libs, with such build.gradle file:
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
jniLibs.srcDir 'src/main/libs' //integrate your libs from libs instead of jniLibs
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// 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'
}

Related

How to compile a module as an AAR and include it in the project in one build?

I have an app module and a module that I'm using as a library -- I'm still making changes to the library, so it isn't precompiled. The library module has its own res folder, which I'd like to keep separate from the main app's res folder. I believe the only way to do this is to package the library as an aar before including it in the main app. It would be convenient if I could run a single Gradle build to package the library and include it. Is this possible?
gradle.build for app (which compiles "my-library"):
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.app.id"
minSdkVersion 18
targetSdkVersion 25
versionCode 5
versionName "1.4"
}
sourceSets {
main {
assets.srcDirs = [project.ext.ASSET_DIR]
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile project(':my-library')
}
gradle.build for my-library:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
minSdkVersion 18
targetSdkVersion 25
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
It turns out that Android Studio is smart enough handle a library module with a res folder. My error was coming from a conflict in the Manifest files.

Cannot find <stdio.h> in android studio project

i am trying to run FM radio project in android sudio but it gives me error
(i downloaded this project from this git https://github.com/mikereidis/spirit2_free)
IMAGE : Android studio + Cannot find stdio.h
here is my gradle file settings
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.testFM"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}
and here is the local.properties file where NDK path is declared
ndk.dir=C\:\\Users\\*****\\AppData\\Local\\Android\\sdk\\ndk-bundle
sdk.dir=C\:\\Users\\*****\\AppData\\Local\\Android\\sdk
Please help
Thanks
Try to run File->Invalidate Caches / Restart...

Gradle doesn't load 0.9.8 version library

apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.user.myapplication"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java', 'src/main/java/2', 'src/main/java/com/example/user/myapplication/function']
}
}
}
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.androidplot:androidplot-core:0.9.8'
}
this is my code.
Gradle doesn't load 0.9.8 version library. However when I use 0.6.1 library it can load. I want to use latest version. What's the problem?
For anyone for whom updating did not work, when I had this problem I discovered that it was because I did not have
mavenCentral()
in the project level build.gradle file (under allprojects and repositories).

include .so in Android Studio with Build:Gradle:2.1.0

I know there are many similar questions but I simply don't understand it.
I have managed to integrate ndk-build within build.gradle but it seems I cant include the .so in my .apk. Here is my build.gralde:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "masterproject.student_at_university_kiel.knauf.torsten"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// for preventing (cheating) gradle-build to overwrite Android.mk with auto-generated Android.mk (NdkCompile task)
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
jniLibs.srcDir 'src/main/libs/' //integrate your libs from libs instead of jniLibs
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
task ndkBuild(type: Exec) {
def ndkDir = "/home/expert/Android/Sdk/ndk-bundle"
workingDir "src/main/jni"
commandLine "$ndkDir/ndk-build"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
}
and this leads to the following directory structure:
|--app:
|--|--src:
|--|--|--main
|--|--|--java
|--|--|--libs
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files
At runtime of my App I get an java.lang.UnsatisfiedLinkError: when calling a native function...
I have found many things like:
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/'
}
But I don't understand that and it feels unnecessary to me to create .jar archives for the native code. Or have I missed something completely?
Thanks in advance!
I am not sure why, the task nativeLibsToJar does the trick, but it does. Maybe there will be some answer on my new asked question magic of nativeLibsToJar
Here's my build.gradle file, which works fine. Maybe someone will find it usefull :)
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "masterproject.student_at_university_kiel.knauf.torsten"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// for preventing (cheating) gradle-build to overwrite Android.mk with auto-generated Android.mk (default NdkCompile task)
sourceSets.main {
jni.srcDirs = [] //disable automatic ndk-build call
jniLibs.srcDir 'src/main/libs/' //integrate your libs from libs instead of jniLibs
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
task ndkBuild(type: Exec, description: 'compile native code') {
def ndkDir = "/home/expert/Android/Sdk/ndk-bundle"
workingDir "src/main/jni"
commandLine "$ndkDir/ndk-build"
}
task nativeLibsToJar(type: Zip, description: 'create a jar archive of the native libs') {
destinationDir file("$buildDir/native-libs")
baseName 'native-libs'
extension 'jar'
from fileTree(dir: 'libs', include: '**/*.so')
into 'lib/'
}
nativeLibsToJar.dependsOn {
ndkBuild // comment that, when you don't want to rerun ndk-build script
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
}

Cannot compile support-v4 lib with Android Studio

I migrate my android app from eclipse to Android Studio. During this "migration" I decided to split my project in 4 modules: GUI module, engine module, commons module and resources module.
The problem is that I cannot include v4 or v7 libs... It does not recognize the import:"import android.support.v4."
Below are the gradles:
1.Genral Gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
1.Gradle for GUI module(main module):
apply plugin: 'com.android.application'
dependencies {
compile 'com.android.support:support-v4:+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile project(':audiorecengine')
compile 'com.google.android.gms:play-services:7.0.0'
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
defaultConfig {
applicationId "com.audioRec"
minSdkVersion 10
targetSdkVersion 22
versionCode 73
versionName "4.0.4"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
lite {
applicationId = "com.audioRec"
versionCode 73
versionName "4.0.4"
}
pro {
applicationId = "com.audioRec.pro"
versionCode 13
versionName "3.0.3"
}
}
}
Gradle for engine module:
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
defaultConfig {
minSdkVersion 11
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile project(':audioreccommons')
}
It appears that the issue was caused by GooglePlayService lib which already contained the v4 support library. The solution was to exclude support-v4 when compiling playServices.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':audiorecengine')
compile ('com.google.android.gms:play-services:7.0.0'){
exclude module: 'support-v4'
}
}
Also I removed v4 from gradle dependency because it is already contained by the v7 support lib

Resources