include .so in Android Studio with Build:Gradle:2.1.0 - android-studio

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'
}

Related

Design view not getting displayed on Android Studio

I just imported a project, but when i try to enter design view this shows up:
My current gradle version is the latest 26.0.2 by the way. I'm not sure what to do, anyone have an idea?
Here are some Gradle Scripts:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion
defaultConfig {
applicationId "com.example.Ron.myapp"
minSdkVersion 15
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.1.1'
compile 'com.android.support:design:23.1.1'
}
and
// 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:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I solved the problem myself. I upgraded the compileSdkVersion aswell as the minSdkVersion along with the dependencies. It now looks like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion
defaultConfig {
applicationId "com.example.Ron.myapp"
minSdkVersion 18
targetSdkVersion 26
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:26.0.2'
compile 'com.android.support:design:26.0.2'
compile 'com.android.support:cardview-v7:26.0.2'
}
please update your support lib and compileSdkVersion like below.
compileSdkVersion = 25
buildToolsVersion = "25.0.2"
minSdkVersion = 18
// Library versions
supportLibraryVersion = "25.0.0"
also add ConstraintLauout gradle in gradle file.
compile 'com.android.support.constraint:constraint-layout:1.0.2'
For More details please check : https://stackoverflow.com/a/45115111/1343788
known problem
just add
compile 'com.android.support.constraint:constraint-layout:1.0.2'
in your gradle file,
it will fix it

Error:(20,0)Could not find method compile() for arguments [file collection]

Below are my gradle files..
so here is the error which i get when i sync the project:-Error:(20, 0) Could not find method compile() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Open File
Following is my app gradle...
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.sinch.workshopskeleton"
minSdkVersion 9
targetSdkVersion 20
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:20.0.+'
compile files('libs/sinch-android-rtc-3.3.5.jar')
}
Following is my project 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:2.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
dependencies {
compile files('app/libs/sinch-android-rtc-3.11.1.jar')
}
Remove below lines from your project's build gradle and it will work fine.
You can add this in your app level build gradle.
dependencies {
compile files('app/libs/sinch-android-rtc-3.11.1.jar')
}

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

Loading shared libraries with Gradle 2.2.1

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'
}

Location of project.properties file in Android Studio 0.6.1

I am studying how to use ProGuard but I could not find the project.properties file on my project. Was that replaced? I can see a proguard-rules.pro. But I could not find the line where I have to remove the comment #.
So far, this is what I have done. I change the false to true in runProguard and change the proguardFiles to "proguard-android-optimize".
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "tok.advertisement"
minSdkVersion 11
targetSdkVersion 19
}
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android-optimize'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
}
Project properties has been replaced in the transition to the Gradle system.
ProGuard in Gradle:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard

Resources