How to add commons-math library in Android Studio - java

I am trying to import common-math library to my Android Studio project. I placed the file commons-math3-3.6.1.jar file in libs folder and in the gradle file I have this line:
compile 'commons-math3-3.6.1.jar'
But I get this error: `
Error:(32, 0) Supplied String module notation
'commons-math3-3.6.1.jar' is invalid. Example notations:
'org.gradle:gradle-core:2.2',
'org.mockito:mockito-core:1.9.5:javadoc'.
Can anyone tell me what I have to do?

Is there a reason you need to use the jar file? You can get the file directly from Maven repository using something like this:
compile 'org.apache.commons:commons-math3:3.6.1'
Available on both JCenter and MavenCentral.

in Android view open Gradle Scripts - build.gradle and add this to dependencies:
implementation 'org.apache.commons:commons-math3:3.6.1'
You can also check their websites if newer version isn't available list of latest versions

Related

The project uses Gradle 2.10 which is incompatible with Android Studio 2020.3

While learning android studio from udacity they asked me to download a project; I did so but while I was importing it to android studio I got an error message:
The project uses Gradle 2.10 which is incompatible with Android Studio 2020.3.
Why did I get this error?
If you did not figure this out yet, I am just starting the same project too. I changed the classpath like above. I then found a website to use the latest version of gradle and the plugin. It says "You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or by editing the Gradle distribution reference in the gradle/wrapper/gradle-wrapper.properties file." I did the first one.
Then, I got errors for both build.gradle files in the project and app version. I get this error: Could not find com.android.tools.build:gradle:7.0.3
If you get this error, click the link "Add google Maven repository and sync project". Then you have to click "Do refactor".
Now you come across another error in one of the build.grade files. This stackoverflow link will tell you: Gradle - Error Could not find method implementation() for arguments [com.android.support:appcompat-v7:26.0.0]
Finally, replace testCompile with testImplementation and then you should get BUILD SUCCESSFUL.
The project's Gradle version you're trying to clone is outdated. Change it based on your android studio version.
on your build.gradle(Project) change the version and sync
classpath "com.android.tools.build:gradle:4.1.2"
Got the solution :)
In gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
In build.gradle project level:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
In build.gradle module level:
Replace all "compile" with "Implementation" and do other suggested version updated and you are done here :)
Hope this works for you, if yes just press that up button :)
If not getting any solution, the best you can do is copy the "app" folder of your project(using file explorer) and paste it in a newly created project(should be of same name as of your project), in that way you'll get already synced gradle and newly created version of your app.
Because, every important thing in your project is present in its app folder and when you create a new project with same name and copy app folder from your older version of your project, you will have a newer version of your project with all same resources.
Note: You can't move through branches and other commits
I saw this issue several times with Android studio. Below trick worked me the best.
Android Studio suggests to upgrade to newer version, please upgrade it and let it sync
Mostly you would see error message again
Now, Go to File -> Invalidate Caches/Restart
It should work now
If it shows same error then I don't know what to do.
If it shows different error then repeat from step 1 to 3 for new error.
Hope, it helps some..

How to implement fixed version of library to my Android studio project?

This library contains 1 bug.
It was fixed here: https://github.com/heinrichreimer/material-intro/pull/287
What is the simplest way to implement this fixed version of the library to my Android studio project? I'm new to using libraries from GitHub, I'd appreciate some help on this.
I'm currently using this: implementation 'com.heinrichreimersoftware:material-intro:2.0.0'
You have several options:
Wait for/Ask the owner to release a new version and use it.
Fork the project and release one version of your own.
Download the library and import it in your project as a dependency.
I'll explain here the third option:
Go to https://github.com/heinrichreimer/material-intro.
Click the Code button and Download ZIP.
Open the ZIP file and open the material-intro-master folder.
Rename the library folder to material-intro-library.
Open your project in Android Studio.
Go to File > New > Import module... and select the material-intro-library folder.
Finally, open your build.gradle file and replace implementation 'com.heinrichreimersoftware:material-intro:2.0.0' with implementation project(path: ':material-intro-library')
You could use JitPack.io which acts as a repository for libraries just like maven and also to compile any Android or Java library using Gradle on GitHub at the required commit (found in the the pull request) and use the compiled library in your project in two steps. No need for waiting, cloning, importing or even compiling on your local PC, all done and covered by JitPack.io. Here are the the steps (retrieved from website):
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.heinrichreimer:material-intro:b35d1c9d65'
}
You can clone material-intro library to your pc. Place the material-intro/lib folder to your project root directory path.
add include ':library' to your setings.gradl
add implementation project(':library') to your app-level build.gradle
You could use JitPack.io which acts as a repository for libraries just like maven and also to compile any Android or Java library using Gradle on GitHub at the required commit.
Step 1. Add the JitPack repository to your build file
Step 2. Add the dependency

classNotFoundException while accessing the aar file's classes in Android Studio

I created an aar file from an module and added it to my project it got added successfully but whenever I am trying to run my app then every time it is getting crashed saying classNotFoundException.
I tried to add dependencies in my module using implementation, api and compile (deprecated now) but still getting the same issue.
I hosted my aar file at Maven and tried to add that dependency in my project but still no luck and getting the same error every time.
There is a work around for the same I can add the those dependencies into my project which I added into my module but I do not want to that.
If I add my project as dependencies then it works fine but I do not want to that also as the module I am working on will be used by multiple clients and I do not want to share my source code with everyone.
I am using the following:
Android Studio 3.5.3
Android Gradle Plugin version 3.5.3
Gradle version 5.4.1
Min SDK Version 21, compile SDK version 29
JDK 8
Is there anything I am doing wrong or I am missing, any help will be appreciated.
Edit 1
I am not using proguard that is disabled.
It is related to proguard, try adding a rule to keep the class in proguard-rules.pro file
-keep class com.app.package.** { *; }
-dontwarn com.app.package.**
replace 'com.app.package ' with your package (where the Notfound class exist)

Android Studio - add ownCloud library from GitHub

I don't understand how to add this library (https://github.com/owncloud/android-library) to my project in Android Studio.
I have tried to import this library as a module but found error while importing:
Error: Module name is not valid
So I tried to find a solution and here it is:
1. Download library from Github here.
2. Unzip library.
3. Start Android Studio.
4. Follow to File -> New -> Import Module .
5. Go to the path where your extracted library is located and select it.
6. Uncheck other modules and add 'androidlibrarymaster' described in below picture.
7. Add Gradle dependency and Its Done!
compile project(':androidlibrarymaster');
Maybe try jitpack website to import this project with build.gradle file.
You have to add this snippet to your build.gradle file :
repositories {
// ...
maven { url "https://jitpack.io" }
}
And this statement to your dependencies :
compile 'com.github.owncloud:android-library:oc-android-library-0.9.7'

How to include dependencies in android library gradle project?

I'm building an android library project with Gradle using Android Studio. It has some local dependencies:
compile project(':androidlibrary')
with nested, additional external dependencies:
compile group: 'com.google.guava', name: 'guava', version: '14.0.1'
I managed to build the aar file but no external or local dependencies are included. I expect external dependencies to be mentioned in the POM file when published to maven, but what about the local ones?
What is the right way to build such project?
I ended up using "download library from maven" feature of Intellj IDEA as described here. It downloads the library and all it's dependencies to the local directory.
Just a friendly warning: including dependencies is considered a very bad practice at SHOULD BE AVOIDED. It may cause conflicts.
There is no automated way to package the dependencies of the library inside the aar. This is contrary to proper dependency management and can lead to problems down the line.
If you really want to do this, you'd have to gather the dependencies manually (using code in build.gradle) and manually package the files in the aar.

Categories