I have some code I'd like to use across multiple different projects. Let's say it's some e-commerce code that handles things like payments and shopping carts.
It seems inefficient and dangerous to copy-paste everything across different projects. And if I add one feature or fix one bug in the core e-commerce module, I'd like that change to be reflected in other projects using it too.
I would also like to re-use some of the Activities, Fragments, Adapters too.
What is a good approach to this?
When we have a library project that needs to be shared to every project on a local computer, we can make use of Maven.
A. Here the step in your library that we will you for the project:
Make a library project from Android Studio.
Add Gradle Android Maven plugin to root build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
Add apply plugin for step 1 in your library build.gradle. (NOT root build.gradle):
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
Add the following after the apply plugin, this line to determine your library when adding to project:
group = 'com.yourpackage.yourlibrary'
version = '1.0'
Add the following code in your settings.gradle:
rootProject.name = 'yourlibrary'
Then publish it to your local maven with:
./gradlew install
Or you can use gradle option in Android Studio.
Your library will be installed in $HOME/.m2/repository. Remember that to use the library you need to add like this:
Groupid:artifactid:versionid
Artifactid will be package name of your library.
B. Here the step in your Project which using the library:
Add the following code in your root build.gradle:
mavenLocal() // for local maven.
This for getting the local library maven that we have installed in step A
Then in your app project.gradle, add compile for the library:
compile 'com.yourpackage.yourlibrary:yourlibrary:1.0'
Read more:
Gradle: How to publish a Android library to local repository
https://github.com/dcendents/android-maven-gradle-plugin
https://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en
From my Knowledge 1. As others said try creating your own Module or Library and use it where ever you need 2.Use Version Control Tools Like Git(If your code changes it will be refleted in your git account)
Related
I want to add an enum called modules with the path of the sub module and some compilation types.
I used to have this in the buildSrc before gradle 6 and it was accessible in the settings.gradle
But from gradle 6.0, settings.gradle is compiled before buildSrc project. I have moved my enum to the settings.gradle, now it is not accessible to other project level gradle scripts.
The behaviour change is outlined in the below release notes.
https://docs.gradle.org/current/userguide/upgrading_version_5.html#changes_to_plugins_and_build_scripts
They suggest to add the enums / classes used in the settings.gradle to the build script closure, but I am not really sure how to do that.
https://docs.gradle.org/current/userguide/upgrading_version_5.html#plugins_and_classes_loaded_in_settings_scripts_are_visible_to_project_scripts_and_buildsrc
I've recently hit a similar issue, my company have custom code for authenticating with our Nexus which we were keeping in buildSrc. I can't turn this into a plugin since I'd need to store that in our Nexus and then would be in a catch-22 situation as I'd need to authenticate to get the authentication plugin!
I can see 2 potential workarounds for this:
Publicly published jar.
Build your custom classes as a separate jar, or a Gradle plugin if this fits the use case. Publish the jar to a maven repository that you can access from settings.gradle buildscript (for me this is difficult as it's sensitive company specific code).
This might look something like the following in your settings.gradle:
include "project-name"
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.companyname:gradle-utils:0.0.1'
}
}
Commit the binary
This isn't a desirable option but you can manage the source code for your custom buildSrc classes in another repository, then every time you make a change to them (hopefully infrequently) - you can build the new version and commit the built jar into the repositories that need to use it (perhaps under buildSrc). If your custom code has dependencies that aren't naturally on the classpath when gradle runs, you'd need to package these into the jar that you commit as well.
Your settings.gradle might then look like:
include "project-name"
buildscript {
dependencies {
classpath files("buildSrc/gradle-utils.jar")
}
}
I create my Android Project on Android Studio 3.0.1 , and Now I try to Open it in
Android Studio 3.5.3 to do that I Added this :
mavenCentral()
maven { url 'https://maven.google.com' }
to build.gradle file for Project in repositories two Parts (buildscript / repositories) and (allprojects/repositories) .
My Question is : Does the app need to test all its features again??
For manual update:
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
}
}
No need to test anything because of gradle version change! But if downgrade the version then you might be change your some api,dependencies for lower gradle version.
Gradle:
Gradle is an advanced build toolkit for android that manages dependencies and allows you to define custom build logic. features are like. Customize, configure, and extend the build process. Create multiple APKs for your app with different features using the same project. Reuse code and resources.
And more about gradle: Link
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"
I'm trying to create an Android library for two days already.
Realy need your help.
I have an existing App which I want to do as a Library.
According to Android developers documentation:
f you have an existing app module with all the code you want to reuse,
you can turn it into a library module as follows:
Open the module-level build.gradle file. Delete the line for the
applicationId. Only an Android app module can define this. At the top
of the file, you should see the following: apply plugin:
'com.android.application' Change it to the following: apply plugin:
'com.android.library'
I have done this step.
The next step is saying:
When you want to build the AAR file, select the library module in the
Project window and then click Build > Build APK.
I don't really understand how to build the AAR file.
Also in my library, I have others dependencies which I need to be in my Library.
I tried a lot of suggestions in StackOverflow but didn't find the answer.
Unfortunately, I didn't find a good example of creating an Android Library with dependencies.
I don't really understand how to build the AAR file
Just compile the library or use ./gradlew build.
The output will be stored under the library's subdirectory under build/outputs/aar.
I have others dependencies which I need to be in my Library.
The aar file doesn't contain the transitive dependencies.
I suggest you publishing your library into a public or private maven repository.
I am building android library project, which has a dependency on another internal library project.
I am wondering if there is a way to package a single AAR library, which already contains internal library inside it. I would like to share only 1 AAR library package to my application developers.
This is how my build.gradle files look currently, but currently they produce separate AAR files and both needs to be included in Application's build.gradle. As application is being built by another company, we need to share the final AAR file with them and not the complete library projects.
----- internalLib -------->>>>>>>>>>
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}
----- externalLib --------
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile project(':internalLib')
}
There is no mechanism to combine library. It's a bit complicated as you probably want to control which dependencies get merged (for instance you probably don't want to include support-v4 in there). Also you'd need to merge the resources and Android manifest.
At this time there's no way to easily hack something, unless you are sure the resources have no conflicts between the two res folders (for instance you could have strings_a.xml in one lib and strings_b.xml in the other lib). This way you can just "merge" the two res folders by copying them both into the same location (as opposed to do a merge at the android res level).
For the Manifest it'd be more complicated, but doable with some custom code.
Providing a built-in mechanism for this is very low on our priority so don't expect it anytime soon.
For the sake you have to upload each library as separately on maven and use its implementation in parent library modules till the main library module. Only then when you publish your main library on maven will include your all child dependencies.
As far as we have only one option add aar as api dependency inside the module.
For that we have to generate aar file and publish it to Maven and make it accessible by another module and consume it in app.
https://developer.android.com/studio/projects/android-library
As mentioned above android developer document.
The library module with source code is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.
If there anything else we can do, please let us know by jot down in the command section.
It is not supported
It is not recommended to include one library into another because it leads to a serious issues with managing versions and complexity of creating and supporting such solution.
You should stick to native approaches like dependency manager or rearchitect your codebase
[iOS Umbrella framework]