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

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

Related

Unable to switch to debug build variant in Android Studio

I've switched to release build variant and configured signingConfigs. Now when I try to check the debug build variant from the drop down menu it switches immediately back to the release build variant. So I'm not able to run my app in debug mode any more.
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
android {
signingConfigs {
config {
...
}
}
compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion rootProject.buildToolsVersion
defaultConfig {
applicationId "com.kost.foo"
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
versionCode 2
versionName "1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}
externalNativeBuild {
cmake {
...
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable true
signingConfig signingConfigs.config
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
main {
jniLibs.srcDirs = ['src/main/jni']
}
}
externalNativeBuild {
cmake {
path 'src/main/jni/CMakeLists.txt'
}
}
}
kapt {
generateStubs = true
}
repositories {
maven { url 'https://github.com/linchaolong/stetho-realm/raw/master/maven-repo' }
mavenCentral()
}
I've tried to revert all changes in build.gradle as it was before configuring, but with no luck.
Any ideas how to fix the issue?
I had a similar problem where most of the Build menu items were greyed out.
'Sync project with Gradle files' didn't fix.
I noticed a 'Build Variants' toggle button on the bottom left of Android Studio (v 3.1.2) and with this was finally able to choose the variant I needed.
Maybe this will work for you too.
Maybe you have got your solution to this, just in case, i provide my solution here.
For Android Studio 2.x
It may because that you compile your dependent project using:
compile project('module_a')
Above setting will force your project to compile the release version of your modules. Just change it to below:
releaseCompile project(path: ':module_a', configuration: 'release')
debugCompile project(path: ':module_a', configuration: 'debug')
For Android Studio 3.x
You don't need to explicitly specify the build variant for a "module project". Just using
implementation project(':library')
will automatically help select the correct build variant.
Here is a detailed explanation: https://developer.android.com/studio/build/?utm_source=android-studio#variant_aware
For Android Studio 3.x Upgraded from 2.x
Delete the .idea folder under your project root directory and restart your Android Studio.
Below is the GUI screenshot:
Hope it helps!
Just need to cut ".idea" Folder and paste it outside Project root folder( For Back up if you need).These files will be auto regenerated. On Opening the project it"ll ask add module (app) to your project. You can ignore the same.
It set set default build variant to be "debug".
You can see build variant tab on left corner or hover on "Monitor" Symbol on left bottom to get build variant option.
I just had the same issue. Solved it by closing Android Studio, removing the generated files and folders: .gradle, .idea, app/.externalNativeBuild, app/build, build, app/app.iml, ProjectName.iml, local.properties, then relaunching Android Studio and allowing it regenerate all these files from scratch.
Had the same problem, solved creating a new temporary Build Type, Build > Edit Build Types, select Build Types and add a new one. Sync, then you can select the new build type, and then revert back to original Debug build type.
Open your module setting. (Click F4)
Go to build types and create a new Build by clicking on plus(+) sign.
Name it anything, like "demo".
Duplicate all the data of debug build.
Now when you open Build Variant you will be able to switch to Debug as well as Demo.
For me I was unable to switch to our 'devDebug' variant but I could switch to another variant like 'devRelease' and then 'devDebug'. So try switching to another variant first.
I ran into a similar issue. My module level build.gradle reflected configurations specified for "debug" AND "release" accordingly. I was able to successfully run my app on emulator and device (LG) prior To Building and Signing My Release Version Of My App. After Building and Signing The Release Version and attempting to Run The Signed APK on My Phone and Device, i received an Error PM Session 'mobile': Error Launching activity....Error while Launching activity..So I started retracing my steps and realized that before deploying the signed release variant, i opened up the run/debug Configurations Dialog
and mistakenly selected the the Value APK from app bundle value From The Deploy attribute, under the Installation category
I Signed My APK In its ENTIRETY and NOT Via Bundle, so the option selected was attempting to Deploy an invalid App Bundle That Was Never Generated To Begin With, even if it was for the correct Build Variant.
I have this question too. My solution is checkout to the branch which i had changed the build variants. And at that branch, I can change from release back to debug. Then just checkout to current branch and everything is OK. It seems to be a Android Studio bug.
English is not my native language; please excuse typing errors.
I also meet this situation. i do this to solv .
Solv:
1. delete each module's impl suffix type file and build folder;
2. then click this button to sync project with gradle build file. button position
finally. and then this problem will be fix.
I fixed this issued by:
Add a new build type named debug1 via edit build.gradle;Enable debug function of this build type;
Sync and select the new build type debug1;
This issue seems to occur when opening the project in a symlinked location on linux. Opening the project directly fixed this issue for me.
relevant link: https://issuetracker.google.com/issues/156857164
For me, it was an issue with the gradle version. Make sure your plugin version and the required gradle version fit together, as defined here.
https://developer.android.com/studio/releases/gradle-plugin
You can find out which gradle versions you are using under:
File -> Project Structure... -> Project
I tried all the above to no effect. Eventually stumbled across File > Invalidate Caches/Restart... for other reasons and found it worked for me.
in main root of app in build.gradle
replace it from:
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
to my Android Studio v3.0.1 in my case:
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
March 26, 2021:
Would not switch from Release to Debug,
File | Invalidate Caches fixed it for me
Unrelated to your build.gradle file,
Sharing hoping it might help someone else -
I was having similar issue it was because one of the line in build.gradle -
android {
...
publishNonDefault true // remove this line and it should work!
}
Here you can get more detail about publishNonDefualt -
It is also possible to publish all variants of a library. We are planning to allow this while using a normal project-to-project dependency (like shown above), but this is not possible right now due to limitations in Gradle (we are working toward fixing those as well).
Publishing of all variants are not enabled by default. The snippet below enables this feature:

NDK files not syncing with Gradle Android

I am doing a project on Beaglebone Black and i have ported Android KitKat on it. Now i need an android app that could help me access those gpios on beaglebone for a project.
So i am using android studio 1.3.1 to create my application for my project and so to access the hardware pins via android i have to use NDK for it. Now when i am trying to sync gradle 2.2.1 with my NDK files it prints an error that i should set android.useDeprecatedNdk=true. Now when i do that it again shows the error that DeprecatedNdk() is not defined.
Although i have downloaded the latest version of NDK i.e. android-ndk-r10e from android developers website but the error still persists.
Following is the build.gradle file from the module.
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "com.packt.gpio"
minSdkVersion 19
targetSdkVersion 19
ndk {
moduleName "packtHAL"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:19.+'
}
Also the error that it is showing is:
Error:(14, 0) Error: NDK integration is deprecated in the current plugin.
Consider trying the new experimental plugin. For details, see http://tools.android.com/tech-docs/new-build-system/gradle-experimental.
Set "android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration.
And if i add the property to the file then it shows the following error:
Error:(6, 0) Gradle DSL method not found: 'useDeprecatedNdk()'
Possible causes:The project 'gpio' may be using a version of Gradle that does not contain the method.
The build file may be missing a Gradle plugin.
If anyone has any idea as to what should be done regarding this. Please share your ideas.
Thank you
Please check the new documentation: http://tools.android.com/tech-docs/android-ndk-preview
Here are the latest project setup instructions: http://tools.android.com/tech-docs/new-build-system/gradle-experimental
This just popped up too: http://ph0b.com/new-android-studio-ndk-support/

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

Create properties file using gradle

I would like to create a properties file named "dev.properties" using gradle. Here is my build.gradle code:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 16
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 16
targetSdkVersion 16
}
def prop = new Properties()
def propFile = new File("dev.properties");
propFile.createNewFile();
prop.store(propFile.newWriter(), null);
buildTypes {
release {
runProguard false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
}
The file is created when I right click on the build.gradle and choose run. However it's not created when I make the entire project. How come?
I'm using android studio 0.4.6 with gradle 1.10.
It's creating the file, just not where you expect. Your script is creating the file inside the current working directory, and in Android Studio, that will be in Android Studio's distribution. There's a bug filed to make Android Studio consistent with the command line (https://code.google.com/p/android/issues/detail?id=65552) and put the working directory at the project root (well, that's assuming your working directory is set there when you issue Gradle commands), but the fix is actually difficult, and the real answer is you should probably never implicitly rely on the working directory, so that you can make your builds as bulletproof as possible.
If you do something like this:
def propFile = new File("${project.rootDir}/dev.properties")
it will put the file in your project's root directory. There's also project.projectDir, which will be your module directory; see http://www.gradle.org/docs/current/dsl/org.gradle.api.Project.html for more details on what's available to you.
As a side note, you should keep in mind this will run every time the build file is evaluated (because the android block is executed every time the build script is run), which could be more often than you want. It's more than just build time; it's project import time as well, and any time Android Studio decides to evaluate the build file, which happens when you open the project and also when you click the Sync Project with Gradle Files button.
Additionally, you should consider at what phase of the build process you want it to happen: is it script evaluation time, or do you want it to run after Gradle has done its analysis and is ready to actually start building things? You can read http://www.gradle.org/docs/current/userguide/build_lifecycle.html to find out more about that.
Sorry, I know it's a lot of information to drop on you when you're just trying to get something going, but those concepts will help you out pretty soon down the road.

Convert existing project to library project in Android Studio

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.

Categories

Resources