run common gradle code in android base project gradle file - android

i have a library which uses all of our apps.
except for shared code and resources i wanted to share gradle code.
each app is constructed:
some_app/
|
- settings.gradle
- build.gradle
|
- mylib/
|
-build.gradle
- other_lib/
- app/
|
- build.gradle
right now i'm talking about the build.gradle in the root dir. The one that defines buildScript closure with dependecies and allprojects{} closure as well.
i tried to use apply from: './mylib/gradle_files/baseProject.gradle'
it works, but then my app can't find the android plugin. for some reason the classpath dependencies were not injected into the build script!
i then tried to create a list and use that:
ext.projectBaseDependencies = ['com.android.tools.build:gradle:2.1.2', 'com.google.gms:google-services:3.0.0']
I then used the list in my closure:
apply from: './mylib/gradle_files/baseProject.gradle'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath projectBaseDependencies
}
}
it doesn't work, gradle does not find my variable defined in baseProject.gradle.
I have a hunch that apply from works AFTER buildScript closure is evaluated :-(
Is there any way to share gradle script code across ALL apps ? if not apply from, what CAN work ? skip configuration and config in afterEvaluate {} clause ?

Why not use a Gradle file in your common library? If your common library includes a build.gradle file, you can apply the android library plugin:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
consumerProguardFiles "proguard-rules.pro"
}
}
dependencies {
// Your common dependencies here
}
Then you can include the library in your project's settings.gradle:
include ':mobile'
project(':common').projectDir = new File("/Users/example/path/to/common")
Then add it to your dependencies in the project's build.gradle:
dependencies {
compile project(':common')
// Your other project dependencies
}

Related

Framework code "can't resolve symbols" in Android Studio

I am trying to debug through library code in Android Studio. But it shows unresolved symbols, i.e., red lines at lots of places.
My app build.gradle looks like:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.kuldeep.customview"
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.1.1'
}
My project build.gradle file is:
// 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.0.0-beta2'
// 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
}
You're looking at the code for TextView, which is an Android platform class and not part of your app. Platform classes can refer to other "package private" classes that are not part of the visible API for an app. Android studio provides for you the ability to view platform source code like this so you can see how it works, but you can't make changes to it and you don't get access to all the actual compiled classes in the SDK.
The short story is that is regular and expected. If you were seeing this for your own app code (not platform classes or add-on libraries) then you'd have something to be concerned about.
2 possible reasons:
Gradle not synced.
Try syncing your gradle files using Gradle Sync.
SDK not updated.
Make sure your SDK is updated and that you have all the extra packages that you need installed. You can do that using SDK Manager.

Error:Cause: buildToolsVersion is not specified

I create a simple project and then I right-click on the project and then I use the make mudole app option. Now I have two build.gradle folders: 1- build.gradle:project My Application. 2- build.gradle: Mudole app. the first build.gradle is as follows:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'com.android.library'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
And the second Build.gradle folder is as follows:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.exam.exam.myapplication"
minSdkVersion 7
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'])
}
And now I click on the run option to create aar file from this project But I get the following error:
Error:Cause: buildToolsVersion is not specified.
I had the same issue. What I was doing wrong was the placing of apply plugin statement in the root gradle of the project. When I placed it in the app gradle then the issue got resolved.
What I would suggest is to check if you are placing some statement in the root gradle that you should be placing in the app gradle.
Add buildToolsVersion to your build.gradle file this will solve your problem.
buildToolsVersion "27.0.1"
Gradle file code
android{
compileSdkVersion 27
buildToolsVersion "27.0.1"
useLibrary 'org.apache.http.legacy'
}
Add buildToolsVersion property to your local build.gradle file.
android {
compileSdkVersion 27
buildToolsVersion "26.0.1"
defaultConfig {
targetSdkVersion 27
}
}
As per your requirements!! Then Try to build -> you will get build failure and this message :
Failed to find Build Tools revision 26.0.1
Install Build Tools 26.0.1 and sync project
Then Click to Install build tools and your project is configured and you are good to go!!!
in file... build.gradle(Project: XXX):
repositories{
//put pluging repository here
//e.g.:
mavenCentral()
}
dependencies{
//classpath to plugin here
classpath 'com.XXXX.xXXxxXX.XXXX'
}
in file.... build.gradle(Module: app)
//add on top:
apply plugin: 'com.YOUR.PLUGIN.XXX'
The issue for me was that andorid-versionCode can only be integer value.
in build.xml:
eg:android-versionCode="1"
It cannot be "1.1" etc...
The error code is not helpful at all.
Not really sure what the underlying problem was, but I found the solution in a concurrent problem that was occurring:
https://stackoverflow.com/a/32490472/1544046
Essentially, I had to change my gradle target to use the wrapper and not a defined path. I've had it set to the Gradle Wrapper in the past, but it seems to revert from time to time without notice. Once I switched back and recompiled it worked fine.
In the project's settings: Build, Execution, Deployment->Build Tools->Gradle and select "Use default gradle wrapper".
Might be helpful for someone:
in my case long time ago I had added keystore properties in build.gradle file for Android:
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
and after some period of time I pulled repo and forgot to add this file ("keystore.properties").
Gradle for some reason (!) gave me the same error (buildToolsVersion was defined a bit below in build.gradle file).
The workaround was just to add missing "keystore.properties" file.
Android Project > setting.gradle
include ':app' , ':xxx'
I imported one of module and this coused this error. I remove it is fixed.
include ':app'
Check if android studio version in build.gradle is same as the one you are running.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
In my case, I opened/imported the project created on android studio 2.3.3 in android studio 3 and i was able to get rid of this issue after updating the build.gradle with 3.0.0
for me it was an additional classpath line for example :
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
so i was deleting the first
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
and the issue solved
I had same error, I using cordova and resolve by removing the space character
id="com.xx.yy " => to id="com.xx.yy"
Try to invalidate caches / restart , it fixed this like this after updated
File-> Invalidate caches/restart

Android Studio Gradle does not download maven depedencies

I am quite new to Android development and trying to write some test code. I have downloaded Android Studio 1.3 version and created testapp. I am trying to add com.facebook.android:facebook-android-sdk:4.1.0 depedency but somehow it's not getting downloaded.
Here is Project level build.gradle file.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}}
Here is Module level build.gradle file.
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.testapp"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.facebook.android:facebook-android-sdk:4.1.0'
compile 'com.android.support:appcompat-v7:22.2.1'
}
After reading other posts on similar topics I also confirmed that Gradle > Global Gradle settings > Offline work is not checked. Still somehow facebook depedency is not getting downloaded.
Any suggestions what could be the issue? Appreciate any pointers.
Thanks.
In your build.gradle you have to add
repositories {
jcenter()
}
It this way gradle knows where are the dependencies to download.
It is somenthing different from the repositories inside the buildscript block.
Otherwise you can add in the top-level build.gradle file:
allprojects {
repositories {
jcenter()
}
}
In this case, the repository block is valid for all modules in the project.
Could you post the error AndroidStudio is showing? (check on the Gradle Console, on the Run section and Android Monitor section)
One possible option that comes to my mind is that you don't have the lastest Android Support repository / Google repository installed: Check for updates on your Android SDK Manager.

Importing project and migrating to Gradle: Error:(1, 0) Plugin with id 'com.android.application' not found

I'm importing a project that may have been created in Eclipse to Android Studio. I'm not quite sure how to migrate to Gradle and the official docs aren't much of a help. The structure is as follows:
> app-android
> app
> swipelist_lib
> coollib_2
> another lib
> ...
The project has no build.gradle files so I added one in app-android and app. Then followed Studio to add a Gradle home. What exactly do I add in these files? Should I be adding the libraries manually by going through Project Structure > Dependencies or through the build.gradle file?
I'm currently getting Error:(1, 0) Plugin with id 'com.android.application' not found. and my project only has app folder showing (I'm assuming it should have the same structure as above)
My current app build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile project(":lib")
compile 'com.android.support:appcompat-v7:19.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
The app-android build.grade file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.1’
}
}
Change 2.2.1 to 1.1.0, which is the latest current version of the Gradle for Android plugin. 2.2.1 is the version of Gradle, which goes in gradle/wrapper/gradle-wrapper.properties, in the distributionUrl, if you have those.
Should I be adding the libraries manually by going through Project Structure > Dependencies or through the build.gradle file?
Either way works. Personally, I work with the build.gradle files directly.

debug: file not found in Android 1.0

Today I updated to Android Studio v 1.0 and I'm getting the following error when trying to compile whatever project.
....\app\build\intermediates\classes\debug: file not found
The thing is that before updating it I had no problems. Here is the code I'm actually trying to compile.
build.grade
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "razvitrance.testnavdrawerplz"
minSdkVersion 16
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v13:21.0.3'
}
build.gradle (for the project)
// 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:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
And the activity code is a simple Navigation Drawer.
Thank you for helping.
This is the erorr i'm getting.
http://gyazo.com/bdf375a160b1662ce4eb0d4e9aed8f30
Check this and this answers.
Generally, you will need to delete your grandle file and let Android Studio generate the one it thinks that it is correct, as it is being mentioned in the given posts. Happened to me too when i upgrated to newer version and that solved my problem.
It seems that Android Studio does not recognize non-alphanumeric symbols in path (cyrillic, for example). I had the same problem, and it was solved with creating project in another folder not containing cyrillic symbols in its path. In your case it may be '#' synmbol that makes a trouble

Categories

Resources