How to download sources from specific dependency in Gradle - android

I am trying to know how to get gradle to download the source of a given dependency. All that I've searched has not work. I've seen the property DownloadSources. But I do not know where to write it in my Android gradle file.
I've tried to write it in different locations, inside dependencies {}, android { }, defaultConfig {}. But no luck. In a regular gradle file like this, where have I to use downloadsources?:
Project file
sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
//..
}
}
allprojects {
repositories {
// ..
}
}
Module file
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
defaultConfig {
// ...
}
buildTypes {
release {
//....
}
}
}
dependencies {
// ...
compile 'it.neokree:MaterialNavigationDrawer:latest.release'
}
Sorry if its a dummy question, but I can not find a solution.

You can set it for all dependencies. For idea use:
idea {
module {
downloadSources = true
}
}
For Eclipse:
eclipse {
classpath {
downloadSources=true
}
}

Related

How to migrate from maven to maven-publish

I am trying to make Android App bundles for Google Play using Android Studio. Android Studio told that I need to upgrade Gradle for that. I migrated Gradle to 7.0.0 version (and updated Android Studio itself), but I am totally new in this, so now I have issues with Maven. I got the following error:
Build file
'/Users/administrator/noughts-and-crosses/node_modules/expo-application/android/build.gradle'
line: 2
A problem occurred evaluating project ':expo-application'.
Plugin with id 'maven' not found.
I found that maven is not used anymore, so I need to migrate to maven-publish. I changed maven to maven-publish, but now I have the following issue:
Build file
'/Users/administrator/noughts-and-crosses/node_modules/expo-application/android/build.gradle'
line: 28
A problem occurred evaluating project ':expo-application'.
Could not find method uploadArchives() for arguments [build_6lgwxdh9lx7r2mkhkk2he2s1g$_run_closure4#56af98de] on project
':expo-application' of type org.gradle.api.Project.
The build.gradle file with the issue looks like that:
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
group = 'host.exp.exponent'
version = '3.1.2'
// Simple helper that allows the root project to override versions declared by this library.
def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
// Upload android library to maven with javadoc and android sources
configurations {
deployerJars
}
// 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
}
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
repository(url: mavenLocal().url)
}
}
}
android {
compileSdkVersion safeExtGet("compileSdkVersion", 30)
defaultConfig {
minSdkVersion safeExtGet("minSdkVersion", 21)
targetSdkVersion safeExtGet("targetSdkVersion", 30)
versionCode 12
versionName '3.1.2'
}
lintOptions {
abortOnError false
}
}
if (new File(rootProject.projectDir.parentFile, 'package.json').exists()) {
apply from: project(":unimodules-core").file("../unimodules-core.gradle")
} else {
throw new GradleException(
'\'unimodules-core.gradle\' was not found in the usual React Native dependency location. ' +
'This package can only be used in such projects. Are you sure you\'ve installed the dependencies properly?')
}
dependencies {
unimodule 'unimodules-core'
implementation 'com.android.installreferrer:installreferrer:1.0'
}
So, the question is: how to fix the new issue?
Update. I read the article from the comment, and now the file looks like this:
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
// Simple helper that allows the root project to override versions declared by this library.
def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
// Upload android library to maven with javadoc and android sources
configurations {
deployerJars
}
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
groupId = 'host.exp.exponent'
artifactId = 'noughts-and-crosses'
version = '3.1.2'
artifact(sourcesJar)
}
}
}
}
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
repository(url: mavenLocal().url)
}
}
}
android {
compileSdkVersion safeExtGet("compileSdkVersion", 30)
defaultConfig {
minSdkVersion safeExtGet("minSdkVersion", 21)
targetSdkVersion safeExtGet("targetSdkVersion", 30)
versionCode 12
versionName '3.1.2'
}
lintOptions {
abortOnError false
}
}
if (new File(rootProject.projectDir.parentFile, 'package.json').exists()) {
apply from: project(":unimodules-core").file("../unimodules-core.gradle")
} else {
throw new GradleException(
'\'unimodules-core.gradle\' was not found in the usual React Native dependency location. ' +
'This package can only be used in such projects. Are you sure you\'ve installed the dependencies properly?')
}
dependencies {
unimodule 'unimodules-core'
implementation 'com.android.installreferrer:installreferrer:1.0'
}
But it didn't solve the problem.

How to use Android Gradle without multi-project

I would like to create an android build with a single build.gradle file. How to do that?
You need at least two build.gradles, a project-level one and a module-level one. They are not the same. You can omit neither of them.
It actually is possible.
The whole problem when trying to create an Android build with a single build.gradle comes from the fact, that a plugins block of the top-level build.gradle can only use plugins from Gradle plugin portal and cannot address anything from buildscript's classpath.
However, there is a pluginManagement block you can add to settings.gradle, that can be used to modify how plugins are resolved.
A working example:
// settings.gradle.kts
rootProject.name = "android_app"
pluginManagement {
repositories {
gradlePluginPortal()
google()
}
resolutionStrategy {
eachPlugin {
if (requested.id.name.startsWith("com.android")) {
useModule("com.android.tools.build:gradle:${requested.version}")
}
}
}
}
// build.gradle.kts
plugins {
id ("com.android.application") version "4.2.2"
}
group = "pl.gieted.android_app"
version = "1.12-SNAPSHOT"
android {
compileSdkVersion(31)
defaultConfig {
applicationId = group.toString()
minSdkVersion(24)
targetSdkVersion(31)
versionName = version.toString()
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
}

How to add a library to Gradle build in Android Studio project?

I want to add the following library to the Gradle build of my project. The library that I want to add is : signal-protocol-java-2.8.1.jar that I have downloaded. How can I add it in the Gradle build, so that I import the classes included in the .jar it in the java classes that I am developing ?
Here is my build.gradle file :
buildscript {
repositories {
google()
mavenCentral()
jcenter {
content {
includeVersion 'org.jetbrains.trove4j', 'trove4j', '20160824'
includeGroupByRegex "com\\.archinamon.*"
}
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.14'
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha09"
}
}
ext {
BUILD_TOOL_VERSION = '30.0.2'
COMPILE_SDK = 30
TARGET_SDK = 30
MINIMUM_SDK = 19
JAVA_VERSION = JavaVersion.VERSION_1_8
}
wrapper {
distributionType = Wrapper.DistributionType.ALL
}
allprojects {
repositories {
google()
jcenter()
}
}
subprojects {
ext.lib_signal_service_version_number = "2.15.3"
ext.lib_signal_service_group_info = "org.whispersystems"
ext.lib_signal_metadata_version = "0.1.2"
if (JavaVersion.current().isJava8Compatible()) {
allprojects {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
}
}
}
task qa {
group 'Verification'
description 'Quality Assurance. Run before pushing.'
dependsOn ':Signal-Android:testPlayProdReleaseUnitTest',
':Signal-Android:lintPlayProdRelease',
':libsignal-service:test',
':Signal-Android:assemblePlayProdDebug'
}
You have to edit your module-level build.gradle (the one you've posted is project-level). It's typically in the "app" folder of your project. Find there dependencies block and add this line:
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
...
}
Then create a folder "libs" near this gradle file and put your .jar library there. Sync the project with Gradle.

How to define different compileSdkVersion using productFlavors in app build.gradle in android

I want to add define different compile sdk version for each if my product flavour A and B as mentioned below.
productFlavors {
a{
compileSdkVersion 29
}
b{
compileSdkVersion 30
}
}
But its not detecting. Please help me if anyone knows.
As I know you can't have different build.gradle for product flavors.
However you should be able to condition the buildscript and the compileSdkVersion.
It is not so clean but you can use something like:
buildscript {
repositories {
jcenter()
mavenCentral()
}
if (condition) {
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0+'
}
} else if (condition) {
dependencies {
classpath 'com.amazon.device.tools.build:gradle:1.1.3'
}
}
}
android {
def type="Google"
if (type=="Google") {
compileSdkVersion 29
} else if (type=="Amazon") {
compileSdkVersion 30
}
//...
}

What are the minimal Gradle settings needed to support converting Kotlin files in an Android Studio App into Javascript using kotlin2js?

Currently, I have two build.gradles. One for the app, and one for the project.
For the App build.gradle, I have:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 19
buildToolsVersion '28.0.2'
defaultConfig {
applicationId "someId"
minSdkVersion 11
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile files('libs/someSDK.jar')
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
For the Project build.gradle, I have:
buildscript {
ext.kotlin_version = '1.2.71'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
allprojects {
repositories {
mavenCentral()
jcenter()
}
}
sourceSets {
main.kotlin.srcDirs += "${projectDir}/src/main/java/pathToFilesConvertedFromJavaToKotlin"
}
compileKotlin2Js {
kotlinOptions.metaInfo = true
kotlinOptions.outputFile = "$project.buildDir.path/js/${project.name}.js"
kotlinOptions.sourceMap = true
kotlinOptions.moduleKind = 'commonjs'
kotlinOptions.main = "call"
}
ext {
minSdkVersion = 15
targetSdkVersion = 23
compileSdkVersion = 23
buildToolsVersion = '23.0.3'
xmlunitVersion = '1.6'
junitVersion = '4.12'
mockitoVersion = '1.10.19'
hamcrestVersion = '1.3'
}
When I compile and run, the app works and no errors occur. However, I do not see my javascript file that should have been produced from those Kotlin files.
Any ideas as to why?
Please note, I have tried using: http://kotlinlang.org/docs/tutorials/javascript/getting-started-gradle/getting-started-with-gradle.html as a guide. The issue with this example is that it suggests using the taskAssemble below. However, this contains File, which is not recognized as a keyword in my build.gradle for some reason.
task assembleWeb(type: Sync) {
configurations.compile.each { File file ->
from(zipTree(file.absolutePath), {
includeEmptyDirs = false
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") ||
!path.startsWith("META-INF/"))
}
})
}
from compileKotlin2Js.destinationDir
into "${projectDir}/web"
dependsOn classes
}
assemble.dependsOn assembleWeb
As of right now, I believe the issue is that you cannot compile Java Byte Code into Javascript via kotlin2js. Since I have Android references in my code, and those references are compiled into Java Byte Code, my Kotlin scripts cannot successfully be converted into Javascript. However, I have come across an interesting article that could be of use to anyone looking to build Kotlin based applications that can work across multiple platforms: https://kotlinlang.org/docs/reference/multiplatform.html

Categories

Resources