How to Import Github non Android Studio Project as library into Android Studio? - 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.

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...

Error:Conflict with dependency 'com.google.code.findbugs:jsr305'

I created a new project in Android Studio 2.2 Preview 1 with Android App and Backend module with Google Messaging. This is the app file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.xxx.xxx"
minSdkVersion 15
targetSdkVersion 23
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'])
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
compile 'com.google.android.gms:play-services-gcm:9.0.0'
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support:support-annotations:23.4.0'
compile project(path: ':backend', configuration: 'android-endpoints')
}
But it's giving:
Error:Conflict with dependency 'com.google.code.findbugs:jsr305'. Resolved versions for app (1.3.9) and test app (2.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
I am new to Android and not able to find what is this error. How do I fix it?
In your app's build.gradle add the following:
android {
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
Enforces Gradle to only compile the version number you state for all dependencies, no matter which version number the dependencies have stated.
This is due to espresso. You can add the following to your apps build.grade to mitigate this.
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
exclude group: 'com.google.code.findbugs'
}
METHOD 1:
I deleted the androidTestCompile on espresso-core line which was automatically included in a new project. Then my Android Studio compiles clean.
The androidTestCompile is in "build.gradle (Module:app)":
dependencies {
...
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
...
}
I don't know whether this deletion will have any problem down the road, but it surely works for my current project now.
METHOD 2: Adding an exclude on findbugs works too:
dependencies {
...
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.google.code.findbugs'
})
...
}
METHOD 3: Forcing compiling with a specific version:
(In the following I force it to compile with the higher version.)
dependencies {
...
androidTestCompile 'com.google.code.findbugs:jsr305:3.0.0'
...
}
From Gradle Plugin User Guide:
When instrumentation tests are run, both the main APK and test APK share the same classpath. Gradle build will fail if the main APK and the test APK use the same library (e.g. Guava) but in different versions. If gradle didn't catch that, your app could behave differently during tests and during normal run (including crashing in one of the cases).
To make the build succeed, just make sure both APKs use the same version. If the error is about an indirect dependency (a library you didn't mention in your build.gradle), just add a dependency for the newer version to the configuration
Add this line to your build.gradle dependencies to use newer version for both APKs:
compile('com.google.code.findbugs:jsr305:2.0.1')
For future reference, you can check your Gradle Console and it will provide a helpful link next to the error to help with any gradle build errors.
The reason why this happen is that diff dependency use same lib of diff version.
So, there are 3 steps or (1 step) to solve this problem.
1st
Add
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
}
to your build.gradle file in android {...}
2nd
Open terminal in android studio
run ./gradlew -q app:dependencies command.
3rd
Click Clean Project from menu bar of android studio in Build list.
It will rebuild the project, and then
remove code in 1st step.
Maybe you need just exec 2nd step. I can't rollback when error occurs.
Have a try.
When I added module: 'jsr305' as an additional exclude statement, it all worked out fine for me.
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
exclude module: 'jsr305'
})
The problem, as stated in your logs, is 2 dependencies trying to use different versions of 3rd dependency.
Add one of the following to the app-gradle file:
androidTestCompile 'com.google.code.findbugs:jsr305:2.0.1'
androidTestCompile 'com.google.code.findbugs:jsr305:1.3.9'
The accepted answer is one way of fixing the issue, because it will just apply some strategy for the problematic dependency (com.google.code.findbugs:jsr305) and it will resolve the problem around the project, using some version of this dependency. Basically it will align the versions of this library inside the whole project.
There is an answer from #Santhosh (and couple of other people) who suggests to exclude the same dependency for espresso, which should work by the same way, but if the project has some other dependencies who depend on the same library (com.google.code.findbugs:jsr305), again we will have the same issue. So in order to use this approach you will need to exclude the same group from all project dependencies, who depend on com.google.code.findbugs:jsr305. I personally found that Espresso Contrib and Espresso Intents also use com.google.code.findbugs:jsr305.
I hope this thoughts will help somebody to realise what exactly is happening here and how things work (not just copy paste some code) :).
Add this this to dependencies to force using latest version of findbugs library:
compile 'com.google.code.findbugs:jsr305:2.0.1'
delete espresso dependencies in gradle file works for me.
delete those lines in app gradle file:
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
i was trying to use airbnb deeplink dispatch and got this error. i had to also exlude the findbugs group from the annotationProcessor.
//airBnb
compile ('com.airbnb:deeplinkdispatch:3.1.1'){
exclude group:'com.google.code.findbugs'
}
annotationProcessor ('com.airbnb:deeplinkdispatch-processor:3.1.1'){
exclude group:'com.google.code.findbugs'
}
Those who are getting same error in Android 3.0.1,can resolve it by simply update the versions of compileSdkVersion and targetSdkVersion to 27 and also Implement com.android.support:appcompat-v7:27.1.1' in dependencies.
In project ':app' you can add the following to your app/build.gradle file :
android {
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
For react-native-firebase, adding this to app/build.gradle dependencies section made it work for me:
implementation('com.squareup.okhttp3:okhttp:3.12.1') { force = true }
implementation('com.squareup.okio:okio:1.15.0') { force = true }
implementation('com.google.code.findbugs:jsr305:3.0.2') { force = true}
REACT NATIVE
If you looking for react native solution, then write this snippet in your affected node_modules gradle build file, e.g. firebase in my case.
android {
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.0'
}
}

Adding dependency to Google Play Services inside Android library

I am developing an Android library that depends on Google Play Services.
The instructions provided by Google on how to setup Google Play Services only specify the steps to follow when adding it to an application project and not a library.
I tried following the same steps for my library and ended up with the following build.gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
classpath 'com.google.gms:google-services:2.0.0-alpha3'
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-annotations:+'
provided 'com.google.android.gms:play-services-auth:8.4.0'
}
apply plugin: 'com.google.gms.google-services'
The problem with this is that when building the project, I get the following exception:
* What went wrong:
Execution failed for task ':processDebugGoogleServices'.
> File google-services.json is missing from module root folder. The Google Services Plugin cannot function without it.
I understand this because, as stated in the Google Play Services documentation (https://developers.google.com/android/guides/setup) , it depends on a json file for configuration. However, since I am building this as a library, I wanted the google-services.json file to be added in the project that will use my library instead of putting it in the library itself.
So how can I do this? How can I compile my library that depends on classes defined in Google Play Services but not really configure Google Play Services inside the library and let it be done by the application that consumes my library?
Thank you!
It was simpler than I thought.
All I had to do was remove the lines classpath 'com.google.gms:google-services:2.0.0-alpha3' and apply plugin: 'com.google.gms.google-services' from the library's build.gradle file and then add them only to the gradle file in my application project that depends on this custom library.
You can generate and add the google-services.json using instruction on this page
It has a step by step set of instructions for generation of the file.

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.

How to port C library into into Android studio JNI folder

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.

Resources