Android Studio library "error: package does not exist" - android

I have created Android library as Android Studio module. Added as dependency to my root module. While coding I can import any class from library package but while I'm trying run the application I'm getting an error package some.mylibrary.project does not exist.
build.gradle root module
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services:5.+'
compile project(':libraries:mylibrary')
}
android {
compileSdkVersion 17
buildToolsVersion "20.0.0"
lintOptions {
disable 'InvalidPackage'
checkReleaseBuilds false
abortOnError false
}
***
}
build.gradle library module
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'idea'
android {
compileSdkVersion 17
buildToolsVersion "20.0.0"
*****
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
settings.gradle
include ':libraries:mylibrary'
P.S. I have to mention that the project was exported from Eclipse IDE so the project structure is different from default one.

For Android Studio 2.2.2
Yes, in library module, it can't use the apply plugin: com.android.application statement in the module definition, yes, use apply plugin: com.android.library instead. (still in lib module)
But then you have to do the following:
Expose the same SDK versions in Gradle files for both modules.
Right click on your projects "app" module folder and click on -> open module settings
Click on the "dependencies" tab
Click on the + sign to add a new dependency and select "Module Dependency"
Look for the library you need and add it.
Also while naming your lib module avoid capitals.

If you have a library module, it can't use the apply plugin: 'com.android.application' statement in the module definition, or the build will silently fail as you're seeing. use apply plugin: 'com.android.library' instead.
A bug has been filed to request that the build system fail loudly instead of silently when this happens: https://code.google.com/p/android/issues/detail?id=76725

The answer above is somewhat lacking
If you project java add in Kotlin getting get this error
Tools Tab Kotlin and Configure Kotlin
(Select Android with Gradle) after select with Modules
Project build.gradle add
ext.kotlin_version = ‘1.3.21’
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Apps build.gradle
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Referance : https://medium.com/mindorks/enabling-kotlin-support-for-a-current-only-java-android-project-98c75e04384a

This error happens when you change the package name and try to run the code. this occurs because the android studio still has the cache of your package with an old name.
Also, cross-check all the imports as well as an imported package in your code so that no different package name exists. For example, is common this error is referring to another imported file near where the error is occurring. Check previous imports near.
To fix this error you can try to do an 'invalidate caches / Restart' option from the File menu in android studio. Choose “Invalidate and restart option” and close Android Studio.
Another reason for this error, is when one changes the project's path root folder or in any of the modules it depends. In this particular case, to fix this error you need to remove the affected modules, and re-add them again. Next don't forget to do an 'invalidate caches / Restart' option from the File menu in android studio. Choose “Invalidate and restart option” and close Android Studio.
Clean your project from android studio :
“Build -> Clean Project”. This will clear your build folders.
Remove your .gradle directory from the root of your project. It contains some Gradle cache files.
Delete also the .idea directory (make a backup before). It contains some project configuration files.
Restart Android Studio.
Finally
if the error still persists, you need to move the affected files to an external directory of the project's root folder. And on Android Studio, create manually each filename as previous, and copy the code inside from the old file. This will defiantly solve this error.

In build-gradle app, add this row:
implementation project(":your_name_library_here")

If you are facing this issue while using Kotlin and have
kotlin.incremental=true
kapt.incremental.apt=true
in the gradle.properties, then you need to remove this temporarily to fix the build.
After the successful build, you can again add these properties to speed up the build time while using Kotlin.
Reference: https://stackoverflow.com/a/58951797/3948854

Related

Adding Kotlin runtime library to aar in android

WHAT I WANT :smile: :
building kotlin library,
deliver it as an .aar file
Java project that uses my .aar don’t need configure anything, just include my .aar and start playing.
first Q : IT THAT EVEN Possible ? cause i’m loosing hope :smile:
if the project that uses my library doesn’t have Kotlin configured, then it says ClassNotFoundException.
-WHY IS THAT ?
if kotlin have the same byte code as Java byte code, (and 100% compatible),
then why i need to have kotlin when using .aar writen in kotlin in a JAVA Project ?
After some reaserch, i discovered that i must include kotlin runtime library in my project but i don’t know how,
i’ve allready tried basically all the solution overs the net ,
i think fat aar isn’t supported on Android,
Thank You All for your attention.
Update
in my aar project i have a module with the following build.gradle
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
/////
.
.
dependencies {
////
.
.
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
in my application that uses the .aar
i have the following in project build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
}
in the build.gralde module
implementation(name: 'my-aar-library', ext: 'aar')
and when i run the app, it crash and here is the stack :
09-25 15:14:22.814 3239-3239/com.example.mymodule E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mymodule, PID: 3239
java.lang.NoClassDefFoundError: kotlin.jvm.internal.Intrinsics
at com.com.example.mymodule.views.MyCustomView.<init>(PCMajorHeadereView.kt)
at .
.
.
.
.
.
UPDATE 2 :
PS :
clearly i must add the kotlin runtime-library to my .aar
i tried all over the net, it doesn’t work :'(
Final Update :
solution found thanks to cilling,
note that you must include the runtime-library into the local maven repo,
it can't access online content
Thnx for all
The problem is that your aar doesn't include dependency tree (.pom file), so Gradle is not able to download them during the sync.
So, what's the proper solution? You should use repository manager, like Maven.
You can see #Robyer post how to include all dependencies and publish it on MavenLocal:
https://stackoverflow.com/a/42160584/7508302
That post is about providing source code for library, but there is also ready to use gradle publish script.
Then, in your 'local maven' a library will be published.
And then, you can add to your gradle file (in project you want to use that library): repositories { mavenLocal() } and then add dependecy like this:
implementation ('com.example.android:library:0.0.1-SNAPSHOT#aar') {
transitive = true
}
Full instruction:
1) In your library add a gradle file responsible for publishing on mavenLocal(). See https://stackoverflow.com/a/42160584/7508302 for details
and ready to use script.
2) Push the library to mavenLocal. It's a local maven repository. You don't need to install anything, as the maven repository just has to have proper dir structure.
3) Check mavenLocal dir. There should be a dir tree with your domain name, for example: com -> mycompany -> library -> 0.0.1 and in that folder you should find .pom file. Open it, to see dependencies of your library.
4) Add mavenLocal() to your repository section in project level gradle file. Note, that mavenLocal just points to some place in your files.
5) Add library dependency using just qualified name (for example: com.mycompany:library:0.0.1#aar. Add parameter transitive if you want to fetch transitive dependencies (that transitive parameter means that that library may depend on other modules).
That way gradle should fetch declared dependencies and include them to project.
Call the below call for smile.aar file in build.gradle file.
implementation project(':smile)
Assuming that smile is the .aar file name.
If you want to run Kotlin you must include following in project build.gradle
buildscript {
ext.kotlin_version = '1.3.31'
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
and also include these in app level build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
//in dependencies
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

3rd-party Gradle plug-ins may be the cause

After updating to Android Studio 3.1 I got this error message:
The project works fine and this is mostly just a warning, so my question is what's the meaning of the warning and how can I get rid of it?
The relevant parts from gradle files:
This is my project's build.gradle
buildscript {
ext {
kotlin_version = '1.2.31'
anko_version = '0.10.4'
room_version = '1.0.0'
support_version = '27.1.0'
firebase_version = '12.0.0'
gms_version = '12.0.0'
}
repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/kotlin/kotlin-dev' }
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
maven { url 'https://dl.bintray.com/kotlin/kotlin-dev' }
}
}
And this is my app's build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
To solve the issue, remove Instant App Provision from the "Run Configurations" and leave only the Gradle-Aware Make.
Run -> Edit Configurations..
I have AndroidStudio 3.1, Gradle Plugin 3.1.0 and Kotlin library version 1.2.30.
I restarted Android Studio and the problem disappeared.
Click File -> Invalidate Caches/Restart
Every time I change the gradle file, I must restart Android Studio to or the problem returns.
You can also try this:
Re-ordered repositories to:
mavenCentral()
maven { url 'https://jitpack.io' }
google()
jcenter()
Clearing this folder: user's ~/.gradle/caches and deleting app
build folder manually, then clean and rebuild.
What fixed the issue for me:
Change gradle plugin version to 3.1.0
Change Kotlin version to 1.2.30
Then Android studio changed gradle wrapper to version 4.4
Then Android studio was saying that the build tools version used was
27.0.3 and that I should change it to 27.0.3 so I also changed the target SDK to 27
I added this to my gradle.build:
kapt {
generateStubs = true
}
I hope it helps
at android studio v3.1.2 , happen Error:
Folder D:\AndroidProjects\app\build\generated\source\kaptKotlin\debug
Folder D:\AndroidProjects\app\build\generated\source\kaptKotlin\release
3rd-party Gradle plug-ins may be the cause
because dataBinding use apply plugin: 'kotlin-kapt' so add
kapt {
generateStubs = true
}
Change gradle plugin version to 3.1.2
Change Kotlin version to 1.2.30
Then Android studio changed gradle wrapper to version 4.4
Then Android studio was saying that the build tools version used was
27.1.1 and that I should change it to 27.1.1 so I also changed the target SDK to 27
Here are some steps that I've followed. In my case it's fixed the issue!
Platform modules targeting Android
The update of the experimental multiplatform projects feature introduces support for Android platform modules. These modules should apply the corresponding plugin in the Gradle build script and can use the shared code from a common module:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-platform-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
// ...
// ...
Kapt diagnostic locations
As of now, kapt, the Kotlin annotation processing tool, can offer links to locations in the original Kotlin code rather than generated Java stubs as it reports errors encountered during annotation processing. You can enable this feature by adding these lines to the Gradle build script (build.gradle):
kapt {
mapDiagnosticLocations = true
}
Add this:
allprojects {
repositories {
jcenter()
google()
}
}
Don't forget the next:
// Architecture Component - Room
implementation "android.arch.persistence.room:runtime:1.1.0-beta1"
kapt "android.arch.persistence.room:compiler:1.1.0-beta1"
// Lifecyles, LiveData and ViewModel
kapt 'com.android.databinding:compiler:3.1.0'
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.1.1"
// alternatively, just ViewModel
implementation "android.arch.lifecycle:viewmodel:1.1.1"
// alternatively, just LiveData
implementation "android.arch.lifecycle:livedata:1.1.1"
kapt "android.arch.lifecycle:compiler:1.1.1"
// Room (use 1.1.0-beta1 for latest beta)
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
// Paging
implementation "android.arch.paging:runtime:1.0.0-alpha7"
// Test helpers for LiveData
testImplementation "android.arch.core:core-testing:1.1.1"
// Test helpers for Room
testImplementation "android.arch.persistence.room:testing:1.0.0"
Clean your project
Build and That's it!
Add all of this, Clean your project, build and That's it! :) Let me know if this works! (If it is not working for you, I will help you with another solution)
More Info: Android Site
:) Let me know if it works! (If it does not work, I will
try to help you finding a better way)
If you give a downVote explain why
What actually helped for me is adding this
kapt {
generateStubs = true
}
into build.gradle
Try removing Instant run from settings and gradle will good to go.
It worked for me.
Here are some steps that i have followed and it's fixed the issue in my case.
First of all install kotlin plugin version to '1.2.31' and update it in build.gradle file like below.
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$1.2.31"
}
Clean Project.
Finally Rebuild the project.
In my case none of the above solutions solved my problem, I was using 1.2.50 Kotlin version without any mention to Instant Run, and the build wasn't generating the Dagger classes, so I find out this question that solved my issue, apparently, in my situation it's an issue related to the new Kotlin version, so I downgraded to version 1.2.41 and worked fine.
By the way, I just tracked to that point because I used the Toggle View on Build screen.
1:Select the Toggle View and build your project
2:You're going to be able to see exactly what happened
Stackoverflow question:
Kotlin 1.2.50 asks for baseFeatureInfoDir
Issue tracker:
https://issuetracker.google.com/issues/110198434
remove apply plugin: 'kotlin-kapt'
add mavenCentral() in build.gradle like:
allprojects {
repositories {
mavenCentral()
google()
jcenter() } }
Sync and Clean project
Here is the some approach how I fix this issue for my case:
First of all update your android gradle plugin version from project build gradle file and then update your gradle version from gradle properties.
Finally update your kotlin version(Mandatory) to kotlin_version = '1.2.30' or later from project build gradle file.
Now try to clean your project and build. Issue should be resolved.
Each time after build if you build again then probably issue will occur again so, just clean your project again and then build.
This happens because the Kapt annotation processor uses this directory to store Kotlin generated files. Android currently does not recognize the path by default.
See Further Details
Adding another answer for those who could not remove Instant App Provision, because it keeps reappearing.
Build the project manually: ./gradlew assembleDebug
It is a hotfix, but it will work (because the issue is probably related to Android Studio).
I had this issue when using Realm with kotlin in android studio.
To solve follow these steps :
After adding Realm to project build.gradle, Make sure your app build.gradle file is like this:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
.
.
.
androidExtensions {
experimental = true
}
Use kapt instead of annotationProcessor in your app build.gradle dependencies.
Go to Run -> Edit Configurations.. and remove Instant App Provision option.
Run this command in Android studio's terminal :
gradlew assembleDebug
It's OK !
Note: If you see "3rd-party Gradle plug-ins may be the cause" message again, Do step 3 & 4 again.
Configuration on demand with Gradle 4.6 and above: If you're using
Android Gradle Plugin 3.0.x or 3.1.x with Gradle 4.6 and above, you
should disable configuration on demand to avoid some unpredictable
build errors. (If you are using Android Gradle Plugin 3.2.0 or higher,
you do not need to take any action to disable configuration on
demand.)
Disable configuration on demand in your gradle.properties file as
shown below:
org.gradle.configureondemand=false To disable configuration on demand
in the Android Studio settings, choose File > Settings (Android Studio
Preferences on Mac), select the Compiler category in the left pane, and clear the Configure on demand checkbox.
In Android Studio 3.2 Beta 1 and higher, the options for enabling
configuration on demand have been removed.
Please read known issues section from below link.
enter link description here
Actually,I was also facing the same error.
What i did is updating my kotlin version to the latest.
This may resolve Your problem.
Well, I found it is because of apply plugin: 'kotlin-kapt',if you delete this line in build.gradle(app), then you will build successfully...
Have no idea why this plugin results in these warnings.

delete external library in Android Studio

how can I delete an external library from Android Project using Android Studio ? In my gradle script I have :
def neo4jVersion = "2.1.7"
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile "org.neo4j:neo4j:${neo4jVersion}"
}
This is pulling about 15 jars but I need to delete one of them, each time I do it, it deletes it but when I run the project again it reappears ! How to delete it permanently ? This is screenshot
The geronimo library is the dependency of the Neo4j kernel.
You can't get rid of it without messing up the whole library.

Error:(2, 0) Plugin with id 'android-test' not found

I am trying to implement Android-DirectoryChooser in my app, but I am new to Android Studio and I have come to a problem that I am not sure how to resolve.
I have created a new folder inside my project called "libraries" and I have copied this library project inside this folder. I then added a dependency like this:
compile project('libraries:android-directorychooser')
I then synced gradle and I have gotten the following error:
Error:(2, 0) Plugin with id 'android-test' not found.
This is probably the problematic line in the project library build.gradle file:
apply plugin: 'android-test'
I've searched for a plugin named "android-test" but I've only found a library and it seems to be deprecated. How can I solve this?
Know you for what this plugin is necessary? When not remove it and your robolectric dependencies.
Plugin comes from https://github.com/robolectric/gradle-android-test-plugin
Robolectric is for unit tests, when you like to use it.(which I would recommend) then just add something like that
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.+'
}
}
to your lib module build.gradle file and the plugin should be found
Android-test is no longer a separate plugin. The standard 'android' plugin now includes testing features. See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing for more information.

Add support library to Android Studio project

I just installed the new Android Studio and I'm looking for a way to import the support library for Android.
Where is the option for that? In Eclipse that are just two clicks. I googled for it but found nothing. Surely it is too new.
=============UPDATE=============
Since Android Studio introduce a new build system: Gradle. Android developers can now use a simple, declarative DSL to have access to a single, authoritative build that powers both the Android Studio IDE and builds from the command-line.
Edit your build.gradle like this:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 18
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.+'
}
NOTES: Use + in compile 'com.android.support:support-v4:21.+' so that gradle can always use the newest version.
==========DEPRECATED==========
Because Android Studio is based on IntelliJ IDEA, so the procedure is just same like on IntelliJ IDEA 12 CE
1.Open Project Structure (Press F4 on PC and Command+; on MAC) on your project).
2.Select Modules on the left pane.
3.Choose your project and you will see Dependencies TAB above the third Column.
4.Click on the plus sign in the bottom. Then a tree-based directory chooser dialog will pop up, navigate to your folder containing android-support-v4.jar, press OK.
5.Press OK.
I no longer work on Android project for a while.
Although the below provides some clue to how an android studio project can be configured, but I can't guarantee it works flawlessly.
In principle, IntelliJ respects the build file and will try to use it to configure the IDE project. It's not true in the other way round, IDE changes normally will not affect the build file.
Since most Android projects are built by Gradle,
it's always a good idea to understand this tool.
I'd suggest referring to #skyfishjy's answer, as it seems to be more updated than this one.
The below is not updated
Although android studio is based on IntelliJ IDEA, at the same time it relies on gradle to build your apk. As of 0.2.3, these two doesn't play nicely in term of configuring from GUI.
As a result, in addition to use the GUI to setup dependencies, it will also require you to edit the build.gradle file manually.
Assuming you have a Test Project > Test structure.
The build.gradle file you're looking for is located at TestProject/Test/build.gradle
Look for the dependencies section, and make sure you have
compile 'com.android.support:support-v4:13.0.+'
Below is an example.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
You can also add 3rd party libraries from the maven repository
compile group: 'com.google.code.gson', name: 'gson', version: '2.2.4'
The above snippet will add gson 2.2.4 for you.
In my experiment, it seems that adding the gradle will also setup correct IntelliJ dependencies for you.
This is way more simpler with Maven dependency feature:
Open File -> Project Structure... menu.
Select Modules in the left pane, choose your project's main module in the middle pane and open Dependencies tab in the right pane.
Click the plus sign in the right panel and select "Maven dependency" from the list. A Maven dependency dialog will pop up.
Enter "support-v4" into the search field and click the icon with magnifying glass.
Select "com.google.android:support-v4:r7#jar" from the drop-down list.
Click "OK".
Clean and rebuild your project.
Hope this will help!
You can simply download the library which you want to include and copy it to libs folder of your project. Then select that file (in my case it was android-support-v4 library) right click on it and select "Add as Library"
In Android Studio 1.0, this worked for me :-
Open the build.gradle (Module : app) file and paste this (at the end) :-
dependencies {
compile "com.android.support:appcompat-v7:21.0.+"
}
Note that this dependencies is different from the dependencies inside buildscript in build.gradle (Project)
When you edit the gradle file, a message shows that you must sync the file. Press "Sync now"
Source : https://developer.android.com/tools/support-library/setup.html#add-library
Android no longer downloading the libraries from the SDK manager, it has to be accessed through Google's Maven repository.
You will have to do something similar to this in your build.gradle file:
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
dependencies {
...
compile "com.android.support:support-core-utils:27.0.2"
}
Find more details about the setting up process here and about the different support library revisions here.
AndroidX[About]
implementation 'androidx.appcompat:appcompat:1.0.2'

Categories

Resources