How to port C library into into Android studio JNI folder - android-ndk

I have a program, which I have compiled using the arm toolchain in NDK. Now I want to use it as a library; put it in the JNI folder and call the functions from my main activity. I want to know how to go about this efficiently and intelligently. I copied all the files, and folders over to the JNI folder. What do I do about the make files in my native C code? Can I modify them to fit the JNI setup? Can anyone help with what items I need to address please? Note, my question is not about porting the native code, rather how to do it effectively. And I am trying to figure out if there are any automation tools in IDE that would help me do this please.
Right now, I get the following error, which I believe tells me that the old make file I copied over is not producing a file that is needed by the project? Am I right?
Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/home/sansari/ndk/android-ndk-r10d/ndk-build NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/home/sansari/AndroidStudioProjects/NDKSample/app/build/intermediates/ndk/debug/Android.mk APP_PLATFORM=android-21 NDK_OUT=/home/sansari/AndroidStudioProjects/NDKSample/app/build/intermediates/ndk/debug/obj NDK_LIBS_OUT=/home/sansari/AndroidStudioProjects/NDKSample/app/build/intermediates/ndk/debug/lib APP_ABI=all
Error Code:
2
Output:
make: *** No rule to make target `/home/sansari/AndroidStudioProjects/NDKSample/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/objs/myLib//home/sansari/AndroidStudioProjects/NDKSample/app/src/main/jni/tools/arm-eabi-4.7/lib/gcc/arm-eabi/4.7/crtn.o', needed by `/home/sansari/AndroidStudioProjects/NDKSample/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/libmyLib.so'. Stop.
Here is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.sansari.ndksample"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName ="myLib"
}
}
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'
}
The story behind this project is that I created it using a tutorial. In it, the author used mylib. So the error I am getting says that the make file I just moved into the JNI folder does not have a line for building mylib.so. Is that right? I looked at the make file at the root of my C code, and that does not look anything like target/dependency structure that I read about in the first few chapters of GNU make. Can you help me find the make file I need to update in order to build mylib.so?
I found some information about my question. By default build.gradle ignores the existing make files in a native project. And you have to put in your own Android.mk and Application.mk files in the Android Studio.

I found out I have to add Android.mk and application.mk to a native project in Android Studio if I am looking to port a large project into Android Studio.

Related

Android Studio 2.3 CMake doesn't show errors and won't compile C++ code

So I'm trying to write my first native library to reuse my C++ code in Android and iOS but just cannot get it to run under Android Studio 2.3
I followed the instructions to the letter (I hope) which mostly consist of:
Write a CMakeLists.txt file containing (in my case):
cmake_minimum_required(VERSION 3.4.1)
add_library(
test-lib
STATIC
src/main/cpp/test.cpp
src/main/cpp/cpp_wrapper.cpp)
include_directories(src/main/cpp/include/)
find_library(
log-lib
log )
target_link_libraries(test-lib ${log-lib} )
add_library( app-glue
STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
target_link_libraries( test-lib app-glue ${log-lib} )
Then link the CMake file to gradle in your modules build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId 'com.mysample.id'
minSdkVersion 19
targetSdkVersion 25
versionCode 9
versionName "1.3.0"
}
buildTypes {
release { ... }
}
productFlavors {
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
repositories {
maven { ... }
}
dependencies { ... }
That should be sufficient. Sadly its not.
I just can't figure out what I'm missing here.
Once (and really only once) I actually got an error message from CMake because of an intentionally placed typo in the cpp file, but that was the only thing I ever saw from it. No library *.so file is created, and of course none is packaged with the apk, so the app crashes as soon as it tries to load it.
Update: I was looking in the wrong place, there actually are some files from CMake in app/.externalNativeBuild/cmake which reflect build configurations and cpu architectures and they even include an test-lib.dir directory, but this is empty. I looked through the files to see if there is some clue but couldn't find anything interesting.
Any suggestions welcome!
In the end, I created a new project, carried out all instructions (again), copied my already existing work over to the new one, voilĂ .
We'll probably never know what the cause was...

New Android Studio + New project - Failed to resolve

I am trying to make my first Android App.
Downloaded and installed the Android Studio, and followed the tutorial to start a new simple "Hello world" app, just to get started, and see that everything works.
But it does not.
I have not written one single line of code myself, so the source is completely generated by Android Studio itself.
I get this error:
Failed to resolve: com.android.support:appcompat-v7:25.+
and
Failed to resolve: com.android.support.test.espresso:espresso-core:2.2.2
When creating the project, I choose API level 15.
In the SDK manager, I have both API level 15 and 25 installed ( I think 25 was default installed, and I have installed API 15 or vice versa).
I have googled a lot, and tried different SDK's, but always the same error. No matter what versions I try to use, it fails with the same message.
Why doesn't the very first tutorial work?
I am a bit confused, if it is so difficult to make the first "Hello world" app :0)
Screenshor of error
Thanks in advance !
Go to File -> Settings -> Build,Execution,Deployment -> Build Tools -> Gradle and then uncheck Offline and then resync the project and rebuild even if all of the above doesn't work try reinstalling the whole setup
#Rahul
Here is the Screenshot of my Tools installed.
Google Repository installed too
Screenshot of Tools
#Rahul Kumar
My build.gradle below.
Project is created using API 15 as minimum.
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "dk.surlution.app3"
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'
}
}
}
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.+'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
testCompile 'junit:junit:4.12'
}

Cannot Resolve R after Update Android Studio to v13

I've got problem when executing gradle after my android studio updated to version 1.3
So this is the error message
Error:Execution failed for task ':app:processDebugResources'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Users\Otniel\AppData\Local\Android\sdk\build-tools\22.0.1\aapt.exe'' finished with non-zero exit value 1
I don't know what was happened, but this is my approach to fix my error (which is not produce any working solution)
change my build tools from project structure settings :
compile sdk version : API 22: Android 5.1
build tools version 22.0.1
(sync gradle and error still the same with the above message)
Then I continue the first setting (point 1) and add this 2nd setting
2. change gradle 1.2.3 to classpath 'com.android.tools.build:gradle:1.3.0'
(1.2.3 produce the same error as the message above)
(with 1.3.0 still produce the same error)
try to revert back the build tools to v 21 and android lolipop v 5
with combination of gradle : com.android.tools.build:gradle:1.3.0, and com.android.tools.build:gradle:1.2.3
When I revert to 1.1.3 or 1.0.0 (for the gradle plugin) I still got the same error.
When I change the gradle to 1.1.3, the console returns a message to use the gradle plugin version that match with build tools sdk (v 24)
But every combination that I use is still return an error message (still the same, like the message above)
Also I've tried to delete impl and .idea folder, and reimport the project, still have the same error.
And my android studio cannot resolve R symbol as the effect.
So what should I do to fix this?
[EDITED]
This is my gradle code :
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
defaultConfig {
applicationId "com.vanwellis.vinnomobile"
minSdkVersion 15
targetSdkVersion 21
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:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.0'
compile 'com.android.support:support-v4:21'
compile 'com.daimajia.easing:library:1.0.0#aar'
compile 'com.daimajia.swipelayout:library:1.2.0#aar'
compile 'commons-io:commons-io:2.4'
compile 'com.daimajia.androidanimations:library:1.1.2#aar'
compile 'com.nineoldandroids:library:2.4.0'
compile 'jp.wasabeef:recyclerview-animators:1.0.3#aar'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.google.code.gson:gson:2.3.1'
}
I got these 2 errors, after seeing it carefully :
D:\Projects\Android\MyProj\app\build\intermediates\res\merged\debug\drawable-hdpi-v4\ic_launcher.png: error: Duplicate file.
D:\Projects\Android\MyProj\app\build\intermediates\res\merged\debug\drawable-hdpi\ic_launcher.png: Original is here. The version qualifier may be implied.
Maybe my additional information are usefull
After updating Android Studio recently, I also had this problem. What worked for me was to expand res, then go through every child folder in res and expand them (you can close them right after.)
Close all classes you have open and in the system bar select Build > Make Project.

How to Import Github non Android Studio Project as library into Android Studio?

I am trying to import standout library which is a github project as library in my project.What i tried is below:
created a folder in my root project directory named 'libs'
copied the complete folder 'library' of the Standout project on git into 'libs'
renamed the folder 'library' that i just copied to e.g. "standout" (just to avoid confusion)
now added this library in my settings.gradle with the following command:
include ':libs:standout'
going to my build.gradle file of my AppProject and added the following line to my 'dependencies':
compile project(':libs:standout')
but i got error and then added like this
compile fileTree(dir: 'libs', include: ['standout'])
gradle sync was successful but there is red mark on the src files of the library project..I couldn't access the library src files from MainActivity of my Project.As the github project wasn't the android studio project do i need to do anything extra for that?
I have tried below way suggested by Wesley:
Download or clone the git repository, copy the library folder into
your project root folder.
Then in your project settings.gradle file, add a ':library' at the
end of the include line, like this: include ':app', ':library', then
gradle sync your whole project.
At the end, in your app module or you own application module, edit
the build.gradle file, add a line in your dependencies:
compile project(':library')
BTW, you can change the folder name ("library" in this case) to any
name you want (like "standout-library" if you want) when copy it into
you project's root folder, then, when you edit the gradle files, just
use the changed name.
But got the below error:
Error:Configuration with name 'default' not found.
My build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.mapandmap.standout"
minSdkVersion 15
targetSdkVersion 21
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 project(':standout')
compile 'com.android.support:appcompat-v7:22.1.1'
}
My settings.gradle:
include ':app',':standout'
screenshot of my Project Tree:
You need to resolve R in each file of "standout" library which is showing as red now.
You can simply add by doing this. Go to Android Studio File->Project Structure-> + , You will get four options to add a new module. From there choose "Import Existing Project"->Next-> browse to the directory you have downloaded library and choose "library" directory and then "Finish".Android Studio will take some time to sync library to your project. Finally add dependency in settings. I just tested by adding this library in a project and its working perfectly.
You could move the library to your main "app" project but its better to add as dependency.
I followed the answer of sbr1308 but I have to do some more things.What you have to do:
Go to Android Studio File->Project Structure-> + , You will get four options to add a new module. From there choose "Import Existing Project"->Next-> browse to the directory you have downloaded library and choose "library" directory and then "Finish"
If there is any error in the classes of the library then Go to File->Invalidate Caches/Restart->Invalidate and Restart.It will take time to indexing.
You have to add the dependency in the app's build.gradle
compile project(':standOut') and then sync.

java.lang.UnsatisfiedLinkError: Couldn't load from loader dalvik.system.PathClassLoader findLibrary returned null

I understand this is asked many times, but I have done thorough research before posting my query.
I have updated Android Studio to latest version as of March 2nd 2015, version 1.1.0. I think still grade does not package .so files on its own.
I have written NewNDK.c in /jni folder. Ran the ndk-build script and .so files were created in /libs. As per suggestion in one of the posts, I modified libs to lib.
Even then, I am getting java.lang.UnsatisfiedLinkError: Couldn't load from loader dalvik.system.PathClassLoader findLibrary returned null error.
The posts does not say about which files to modify. I am new to Android, request your help.
I have narrowed down the problem to: 1. Gradle is not packaging 2. Gradle scripts should be modified.
The build.gradle (Module: app) is as below:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.raghu.newndk"
minSdkVersion 17
targetSdkVersion 17
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:21.0.3'
}
The build.gradle (project) is as below:
// 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()
}
}
Please let me know what is missing.
Thank You!
As per suggestion in one of the posts, I modified libs to lib.
It doesn't really make sense to do that. lib is the folder where your .so files will go inside the APK, but this step will be handled directly by the packaging tools if your project is correctly organized.
Inside an Android Studio project, you should put the .so files inside jniLibs/<ABI> where <ABI> is the target architecture of each .so file (armeabi, x86... the same way that they are generated from the NDK inside libs).
So you can either rename your libs folder to jniLibs, or configure your build so your .so files from libs will be directly integrated:
android {
...
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
}
}
Open the project properties > go to Andriod options > select Advance tab > just uncheck "armeabi","armeabi-v7a","arm64-v8a" and save the project.
The Error will be removed.

Resources