Convert existing project to library project in Android Studio - android

How can I convert an existing Android project into an Android library project in Android Studio? In Eclipse, that is possible.
Actually, I want to convert an old Android project into an Android library project so that I can use existing code of that Android project to build a new Android project with minor changes in Android Studio.

In your module's build.gradle file (not the root project, if you use modules!), simply replace:
apply plugin: 'com.android.application'
// or, if you're on an old version
apply plugin: 'android' // note: this one is deprecated
...with:
apply plugin: 'com.android.library'
// or, if you're on an old version
apply plugin: 'android-library' // note: this one is deprecated
Note that recently, 'android' has changed to 'com.android.application', while 'android-library' has been changed to 'com.android.library'. Avoid using the old names in new projects.
After updating your build.gradle file, you should Sync Project with Gradle files (which is in the toolbar), as not doing it might result in errors and things not working correctly.
Android Studio will then update some files to indicate that the module is now a library; as this will be added into your .iml file:
<option name="LIBRARY_PROJECT" value="true" />
As you might already know, you will not be able to run your (now) library project -- you will need to include it into an app project.
If you're using Android Studio 1.0 and you are getting “Library projects cannot set applicationId”, make sure you do not have applicationId in your Gradle build file.

Looking at this document
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup
I think all you have to do is add this to your build.gradle file,
Creating a Library Project
apply plugin: 'android-library'
From the link
Creating a Library Project
A Library project is very similar to a regular Android project with a few differences.
Since building libraries is different than building applications, a different plugin is used. Internally both plugins share most of the same code and they are both provided by the same com.android.tools.build.gradle jar.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.6'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 15
}

Changing to Library
Goto android project, where you need to change as Library module.
In build.gradle(:app) file,
change this line to
plugins {
id 'com.android.application'
}
to
plugins {
id 'com.android.library'
}
Delete the line for the applicationId in same build.gradle(:app) file
defaultConfig {
applicationId "com.project.example" //remove this line
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Now Sync Project with Gradle Files.

open project in file explorer,open project.properties and try changing android.library=true in project.properties

This is a late response, but I've been trying to do the same thing. None of the above seem to have done the job for me, but I found this that I think works:
Right click on the project name -> Mark Directory As (at bottom) -> Sources Root
I don't know the difference between Resources Root and Sources Root, and a bit of googleing to turn up the answer, but hopefully that's right. I just know a Library isn't supposed to build an apk, and after setting this option, it's not able to so I'm assuming it works.
If anybody else knows more than me, please say so!

If you make it with command line, like chanakya propose, you have to update it with:
android update lib-project \
--target <target_ID> \
--path path/to/your/project
see: http://developer.android.com/tools/projects/projects-cmdline.html#ReferencingLibraryProject
That work for eclipse, but not for android-studio since that update the build.xml.

Related

A dependent feature was defined but no package ID was set. You are probably missing a feature dependency in the base feature

I am following one of the Google Codelabs for making an Instant App.
And I was trying to create topeka-ui (A UI feature module for Instant Apps).
When I try to run one of the instant app module it says :
A dependent feature was defined but no package ID was set.
You are probably missing a feature dependency in the base feature.
I had an issue in that I had an Android app and an Android Library, but I had used the wrong plugin by mistake.
For an app:
plugins {
id "com.android.application"
id "kotlin-android"
}
For a library:
plugins {
id "com.android.library"
id "kotlin-android"
}
Since this is the only stackoverflow question for "A dependent feature was defined but no package ID was set. You are probably missing a feature dependency in the base feature." I will answer what my issue was here rather than create a new question. I had a module that was giving me this error and couldn't figure out the problem. In the dependent module's build.gradle file, I had:
apply plugin: 'com.android.feature'
It should have been:
apply plugin: 'com.android.library'
I just ran through the codelab on AS 3.0 beta 2 without issues (*note). After what point in the codelab did your issue appear?
You might’ve missed a step. Double check that your base module’s build.gradle has:
dependencies {
...
application project(":topekaapk")
feature project(":topekaui")
}
Leaving out feature project(":topekaui") can cause this error:
Error:com.android.builder.internal.aapt.AaptException: A dependent
feature was defined but no package ID was set. You are probably
missing a feature dependency in the base feature.
Note: because data-binding has been disabled for non-base modules (https://issuetracker.google.com/63814741), there requires some additional steps in the multi-feature step-7 to get around it (ie. getting rid of the DataBindingUtil).
I did it in build.gradle(...mylibrary), fixed it and it worked:
plugins {
- id 'com.android.application'
+ id 'com.android.library'}
defaultConfig {
- applicationId "com.example.mylibrary"
minSdk 21
targetSdk 32}
I had this issue in my Dynamic Feature Module when I forgot to add a reference to it in the base module's android.dynamicFeatures = [":module_name"] array
Base from basic instant app project structure,
When you build your instant app, this module takes all of the features and creates Instant App APKs. It does not hold any code or resources; it contains only a build.gradle file and has the com.android.instantapp plugin applied to it. Here's an example:
apply plugin: 'com.android.instantapp'
...
dependencies {
implementation project(':base')
// if there additional features, they go here
implementation project(':feature1')
}
Furthermore, note that
The base feature module's build configuration file needs to apply the com.android.feature gradle plugin. The build.gradle file does not contain any instant app specific modifications.
With this and in line with your encountered error, you may want to check your base feature module's build configuration file. Lastly, make sure that you also sync your project with gradle files.
See Android Instant Apps documentation for more information.
With the following gradle pugin
classpath 'com.android.tools.build:gradle:3.5.1'
In my case, after adding to app's build.gradle
android{
dataBinding {
enabled = true
}
}
I got the posted error, then doing the following
Android studio -> invalidate cache and restart
Issue got fixed!
Not Fixed Yet?
Probably there is a conflicting dependency residing in build.gradle,
like the older and current version of the same library
This solution will work 100%
Step 1) Open gradle.properties
Step 2) Add android.enableJetifier=true to the file
Done!
See The Screen Shot:
You might have added dependent module as a application, it should be added as a library.
Check build.gradle file of module and
Replace
plugins {
id 'com.android.application'
to
plugins {
id 'com.android.library'
also remove applicationId from build.gradle's of inner module if its added in defaultConfig block

Android Test Module (Gradle Plugin 1.3) doesn't work: "debug-classes not found"

I'm attempting to set up a unit test module as described in the android studio blog post. However, doing a gradle build fails telling me "Configuration with name 'debug-classes' not found". Debug is the name of the targetVariant it's trying to build, but I don't understand what is going wrong here.
Here's my test module's gradle file.
apply plugin: 'com.android.test'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
targetProjectPath ':app'
targetVariant 'debug'
}
This is the blogpost describing the new test module functionality.
http://android-developers.blogspot.com/2015/07/get-your-hands-on-android-studio-13.html
I'm using the Gradle plugin v1.3.0
I was also curious about separating app code and test code and i had hard time to figure it out. I look at the stack trace and found the DependencyManager (line 238) having a TODO to fix that in gradle.
1) You are right about the build flavors.You have to enter the correct variant
targetVariant '<flavor>Debug'
e.g.
targetVariant 'flavor1Debug'
2) You also need to change you targetProjectPath's module build.gradle. Add the following snippet:
android {
// ...
publishNonDefault true
// ...
}
which publishes all build variants! It its disabled by default due to some limitations of gradle.
Here is a sample app that works https://github.com/googlesamples/android-testing-templates/tree/master/AndroidTestingBlueprint
You must use
buildToolsVersion = '23.0.0rc3'
And of course
publishNonDefault true

Android Studio 1.3 test module (com.android.test) [duplicate]

I'm attempting to set up a unit test module as described in the android studio blog post. However, doing a gradle build fails telling me "Configuration with name 'debug-classes' not found". Debug is the name of the targetVariant it's trying to build, but I don't understand what is going wrong here.
Here's my test module's gradle file.
apply plugin: 'com.android.test'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
targetProjectPath ':app'
targetVariant 'debug'
}
This is the blogpost describing the new test module functionality.
http://android-developers.blogspot.com/2015/07/get-your-hands-on-android-studio-13.html
I'm using the Gradle plugin v1.3.0
I was also curious about separating app code and test code and i had hard time to figure it out. I look at the stack trace and found the DependencyManager (line 238) having a TODO to fix that in gradle.
1) You are right about the build flavors.You have to enter the correct variant
targetVariant '<flavor>Debug'
e.g.
targetVariant 'flavor1Debug'
2) You also need to change you targetProjectPath's module build.gradle. Add the following snippet:
android {
// ...
publishNonDefault true
// ...
}
which publishes all build variants! It its disabled by default due to some limitations of gradle.
Here is a sample app that works https://github.com/googlesamples/android-testing-templates/tree/master/AndroidTestingBlueprint
You must use
buildToolsVersion = '23.0.0rc3'
And of course
publishNonDefault true

Android Studio library "error: package does not exist"

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

Android Studio IDE unable to refer to classes from a library module

I'm using Android Studio 0.2.2
I've imported an existing eclipse project, which has the classic eclipse ADT project
structure and added a gradle build script into it.
Let's name it ProjectA
Then I created a module in it, which has the new Android Studio structure,
And added it as library in File>Project Structure dialog.
Let's name it LibA
The problem is this - ProjectA is somewhy lacking the classpath of LibA and therefore the IDE's java parser fails to see classes from LibA, therefore I'm getting a lot of errors on
the IDE's side.
Gradle of course, compiles everything ok because the build process is no longer related to the IDE as it were in Eclipse.
Anyone got any pointers in the matter?
Thanks!
If you are trying to add a library, go to
File -> Project Structure -> Libraries -> click + then add the jar.
Once it has been added, it will ask you for the module which is dependent on jar. Select ProjectA from the list. That should link the library to Module.
After setup dependencies in build.gradle:
try "Tools -> Android -> Sync Project with Gradle Files"
Set up you configs like this:
/settings.gradle
include ':LibA', ':progect'
/project/build.gradle
apply plugin: 'android'
dependencies {
compile project(':LibA')
}
android {
compileSdkVersion 18
buildToolsVersion "18"
defaultConfig {
minSdkVersion 7
targetSdkVersion 18
}
}

Categories

Resources