I want to attach the sources to an kotlin library project and it looks I succeeded as I have the source-jars now in here:
https://jitpack.io/com/github/walleth/kethereum/bip44/0.21/
the version before I did not export the sources - so I thought it is successful.
https://jitpack.io/com/github/walleth/kethereum/bip44/0.20/
Unfortunately AS does not show the sources. I am generating them like this:
allprojects {
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
apply plugin: "kotlin"
apply plugin: "jacoco"
apply plugin: "maven"
apply plugin: "com.github.ben-manes.versions"
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'org.assertj:assertj-core:3.8.0'
testCompile 'junit:junit:4.12'
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
}
Also the source-jars contain the kotlin files.
It is all in this project: https://github.com/walleth/kethereum
This is what we have for Maven Publication.
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}
And then you can use artifact androidSourcesJar.
Related
I have a kotlin multiplatform project A setup for iOS and Android, it works well. It has a common module for sharing business logic, and platform-android and platform-ios module for implementing the platform API.
After I adding the common and platform-android module from project A to another Android project B, the Android Studio IDE reports tons of syntax error, but the codes build and run from Android studio without a problem.
The syntax looks like the kotlin-stdlib is not there while it's indeed in the build.gradle, otherwise it won't build.
For instance:
val filterMap = mutableMapOf<String, MenuFilter>()
Android studio will say Unresolved reference: mutableMapOf
Some facts:
common module has the problem
no problem for platform-android.
and of course, no problem when I use IDEA to edit project A
my build.gradle for common module looks like this:
apply plugin: 'kotlin-platform-common'
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
}
sourceSets {
main.kotlin.srcDirs += 'main/'
test.kotlin.srcDirs += 'test/'
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.kotlin
}
artifacts {
archives sourcesJar
}
kotlin {
experimental {
coroutines "enable"
}
}
The rootProject build.gradle in Android Studio is:
buildscript {
ext{
kotlin_version = '1.2.41'
anko_version = '0.10.4'
dagger_version = '2.15'
support_lib_version = '27.1.1'
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And this is the settings.gradle file for Android project B
def projectA_path = "path/to/projectA"
include(":common")
project(":common").projectDir = new File("$projectA_path/common")
include(":platforms:android")
project(":platforms:android").projectDir = new File("$projectA_path/platforms/android")
Even the two are from different projects, according to the setup here. Shouldn't the two just work? What am I missing here?
IDE version:
IDEA Ultimate 2018.1
Android studio 3.1.2
Try adding this in build.gradle
apply plugin: 'org.jetbrains.kotlin.multiplatform'
I am trying out Hawk authentication by https://github.com/wealdtech/hawk. I would like to include this library by source in an empty project so that I can experiment with the apis. I do not want to use a jar or gradle dependency.
I import the project as a module and I run into this error:
Error:(2, 0) Gradle DSL method not found: 'compile()'
Possible causes:<ul><li>The project 'HawkTest' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file</li><li>The build file may be missing a Gradle plugin.
Apply Gradle plugin</li>
I tried solutions from these links but I could not derive any information that could help resolve this issue:
Android gradle build Error:(9, 0) Gradle DSL method not found: 'compile()'.
Android gradle build Error:(9, 0) Gradle DSL method not found: 'compile()'.
I have spent many hours on this problem but do not seem anywhere near a solution. Any direction or a solution would be greatly appreciated.
This my top-level build file:
// 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:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
The app level file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.test.android.hawktest"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}
The Hawk module's gradle file:
dependencies {
compile 'com.wealdtech:wealdtech-core:2.0.0'
compile 'com.wealdtech:wealdtech-configuration:2.0.0'
compile 'com.google.guava:guava:17.0'
}
uploadArchives {
repositories {
mavenDeployer {
pom.project {
pom.artifactId = 'hawk-core'
name 'Hawk Core'
description 'Java implementation of Hawk protocol - core'
}
}
}
}
And my directory structure:
hawk top level build.gradle contains all subproject configuration (check https://github.com/wealdtech/hawk/blob/develop/build.gradle)
For the whole process, from your project root directory :
git clone git#github.com:wealdtech/hawk.git
In your settings.gradle, this will configure the gradle modules :
include ':app',':hawk-core', ':hawk-server-jersey', ':hawk-client-jersey'
project(':hawk-core').projectDir = new File('hawk/hawk-core')
project(':hawk-server-jersey').projectDir = new File('hawk/hawk-server-jersey')
project(':hawk-client-jersey').projectDir = new File('hawk/hawk-client-jersey')
Then edit your top level build.gradle to specify the configuration for the Java project. Add the following :
configure(subprojects.findAll {it.name.startsWith('hawk')}) {
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'signing'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
testCompile 'org.testng:testng:6.8'
}
task copyLibs (type: Copy) {
into "$buildDir/output/libs"
from configurations.testRuntime
}
task testJar (type: Jar) {
classifier = 'tests'
from sourceSets.test.output
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from 'build/docs/javadoc'
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives jar
archives javadocJar
archives sourcesJar
}
}
You cant add Java plugin (apply plugin: 'java') for your Android project . If you do so, you will have this error : The 'java' plugin has been applied, but it is not compatible with the Android plugins.. This is why I use the configure(subprojects.findAll and perform a filter on hawk modules. I've copied the remaining configuration from https://github.com/wealdtech/hawk/blob/develop/build.gradle
I host my library with Github repo and created a release with JitPack. Now when I want to get it with Android Studio, I get this error message:
Failed to resolve: com.github.AhmedCommando:emojis_managers:v1.1
This is my build Gradle:
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.AhmedCommando'
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'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
// build a jar with source files
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
failOnError false
source = android.sourceSets.main.java.sourceFiles
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
classpath += configurations.compile
}
// build a jar with javadoc
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
This is how I app build Gradle:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
Thank you for your help.
Try this:----
Step 1. Add the JitPack maven repository to the list of repositories:
url "https://jitpack.io"
Step 2. Add the dependency information:
Group: com.github.Username
Artifact: Repository Name
Version: Release tag, commit hash or -SNAPSHOT
That's it! The first time you request a project JitPack checks out the code, builds it and sends the Jar files back to you.
To see an example head to jitpack.io and 'Look Up' a GitHub repository by url.
Gradle example:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
dependencies {
compile 'com.github.User:Repo:Version'
}
Note: when using multiple repositories in build.gradle it is recommended to add JitPack at the end. Gradle will go through all repositories in order until it finds a dependency.
Snapshots
Snapshot versions are useful during development. A snapshot is a version that has not been released. The difference between a real version and a snapshot is that snapshot might still get updates. Snapshot versions are useful during development process and JitPack provides two ways to get them. You can specify a version for your dependency as:
commit hash
branch-SNAPSHOT (replace 'branch' with any branch name, e.g. master)
For example:
// dependency on the latest commit in the master branch
compile 'com.github.jitpack:gradle-simple:master-SNAPSHOT'
have you added the compile portion to the gradle?
dependencies {
compile 'com.github.AhmedCommando:emojis_managers:v1.1'
}
I do not see that in the gradle that you have posted
Since I cannot use a private maven in order to share my library, I was thinking in sharing the aar and importing into another project.
The problem comes when the aar and jar files does not contain any dependency. So once I manually import the aar in android studio (using Import .JAR/.AA Package) there is no dependency, and I have to manually add all dependencies again.
I already generated a pom file through a gradle task, although I cannot find any way to manually import it on the project.
On the build.gradle file automatically generated by the "Import .JAR/.AA Package" is:
configurations.maybeCreate("default")
artifacts.add("default", file('TestSample_1.0.0.aar'))
Is there a way to add the pom/iml file too? something like:
artifacts.add("default", file('pomDependencies.xml'))
1. Publishing
In your aar project, add maven-publish plugin and add necessary plugin configuration.
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
...
dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.novoda:bintray-release:0.2.7'
}
...
publishing {
publications {
maven(MavenPublication) {
groupId 'com.example' //You can either define these here or get them from project conf elsewhere
artifactId 'example'
version '0.0.1-SNAPSHOT'
artifact "$buildDir/outputs/aar/app-release.aar" //aar artifact you want to publish
//generate pom nodes for dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each { dependency ->
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.name)
dependencyNode.appendNode('version', dependency.version)
}
}
}
}
//publish to filesystem repo
repositories{
maven {
url "$buildDir/repo"
}
}
}
Few things to note:
We're using a custom maven publication, so you have to define what is being published with the artifact clause
We have to generate the pom ourselves, in the code above I'm using all compile config dependencies, you may want to make sure all the configs you care about are covered.
Running gradle publish will publish to a maven repo structure to the repo folder, which you can then consume from a different project.
2. Using published .aar
In a different android project, to use the aar published in #1:
In top level build.gradle:
allprojects {
repositories {
jcenter()
maven {
url "D:/full/path/to/repo"
}
}
}
add the path to earlier repo as a maven repository. Note that you may have to use the full path, because $buildDir has a different value for this project. In your app build.gradle:
dependencies {
...
other dependencies
...
implementation ('com.example:example:0.0.1-SNAPSHOT#aar'){transitive=true}
}
transitive=true is required for to fetch the transitive dependencies from the pom file.
Things have changed a little, here's how you do it with the latest versions of gradle
Create the package localy (aar and pom)
Modify your library build.gradle file to include
apply plugin: 'maven-publish'
android {
...
...
}
dependencies {
...
...
}
publishing {
publications {
maven(MavenPublication) {
groupId 'com.domain' //You can either define these here or get them from project conf elsewhere
artifactId 'name'
version '1.0.0'
artifact "$buildDir/outputs/aar/sdk-release.aar" //aar artifact you want to publish
//generate pom nodes for dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each { dependency ->
if (dependency.name != 'unspecified') {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.name)
dependencyNode.appendNode('version', dependency.version)
}
}
}
}
//publish to filesystem repo
repositories{
maven {
url "$buildDir/repo"
}
}
}
Run from terminal
./gradlew clean
./gradlew build
./gradlew --console=verbose publishToMavenLocal
The aar and pom files have been created at $HOME/.m2/repository/
How to load the library from a different project
Modify the projects's build.gradle in the following way:
allprojects {
repositories {
maven {
url "/Users/username/.m2/repository/"
}
google()
jcenter()
}
You can use $rootDir and set a relative path.
Add the library as a dependency in your app module build.gradle
implementation 'com.domain:name:1.0.0'
I have a library and a Android app using Gradle and Android Studio. I can include the library directly in the project as following
compile project(':library')
Because I don't want to mesh up with library source code, I want to publish the library into local repository so that I can use as
compile 'com.mygroup:library:1.0'
Any advise?
I just found a solution. In the build.gradle of the library project, add this
apply plugin: 'maven'
group = 'com.mygroup'
version = '1.0'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://[your local maven path here]")
// or repository(url: mavenLocal().getUrl())
}
}
}
In the project folder, type following command
gradle uploadArchives
Read Publishing artifacts for more information
For an Android Library you should use the Android Gradle-Maven plugin https://github.com/dcendents/android-maven-gradle-plugin:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
Then to publish to your local repository run:
./gradlew install
which will install it in $HOME/.m2/repository. In your app or other project you can then include it by adding mavenLocal() to your repositories.
Alternatively, if your library is on GitHub then you can simply include it in other projects using JitPack. Then you don't have to run the above command and can just use what's been pushed.
Publish de library on your local maven repository and then on your gradle use
repositories {
mavenLocal()
}
If you have other repositories listed, make sure your mavenLocal appears first.
Docs: section 51.6.4 on https://gradle.org/docs/current/userguide/dependency_management.html
I prefer adding the java sources and the javadoc to the maven repository. The following script publishes your android library to a maven repository using the android maven plugin. It creates the .aar, javadoc.jar, sources.jar and .pom and updates the maven-metadata.xml after uploading the files to the maven repository. I also put the script on GitHub.
apply plugin: 'com.android.library'
apply plugin: 'maven'
//Your android configuration
android {
//...
}
//maven repository info
group = 'com.example'
version = '1.0.0'
ext {
//Specify your maven repository url here
repositoryUrl = 'ftp://your.maven.repository.com/maven2'
//Or you can use 'file:\\\\C:\\Temp' or 'maven-temp' for a local maven repository
}
//Upload android library to maven with javadoc and android sources
configurations {
deployerJars
}
//If you want to deploy to an ftp server
dependencies {
deployerJars "org.apache.maven.wagon:wagon-ftp:2.2"
}
// custom tasks for creating source/javadoc jars
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
destinationDir = file("../javadoc/")
failOnError false
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
//Creating sources with comments
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
//Put the androidSources and javadoc to the artifacts
artifacts {
archives androidSourcesJar
archives javadocJar
}
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
repository(url: repositoryUrl) {
//if your repository needs authentication
authentication(userName: "username", password: "password")
}
}
}
}
Call it with
./gradlew uploadArchives
In settings.gradle add
include 'riz.com.mylibrary'
project(':riz.com.mylibrary').projectDir = new File('C:\\Users\\Rizwan Asif\\AndroidStudioProjects\\mylibrary')
Then in build.gradle in the dependencies add
compile project(':riz.com.mylibrary')