Changing the instrumentation class in an Android Studio run configuration - android-studio

I have somewhat of a complex test setup for my Android project and it requires different test runners. I have setup my Gradle file so that it switches the test runner according to a project property. That way I can control the test runner when running tests via a Gradle script.
However, I'd love to do the same with an Android Studio run configuration. But the Instrumentation Class field seems to be grayed out and AS won't let me change it. See here.

I had the same problem and solved it by setting up testInstrumentationRunner in build.gradle, e.g.
android {
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Of course it might work or not depending on your Gradle setup - you wrote you're doing some complex things, but this might be a place to start.

Related

The difference between running tests from AndroidStudio and gradle task

I have local tests which use mockito and powerMockito in my project. There are about 300 of them.
When I run them from the Android Studio (Run -> Run test configuration), all of them passed.
But our CI system launch it using gradle task testDebugUnitTest, which fails about 90 of them with different errors.
The question is: what is the difference between these mechanisms?
Are you using Android Gradle(https://developer.android.com/studio/releases/gradle-plugin.html) plugin?
One must be aware that Android Studio build system is based on Gradle, and the Android plugin for Gradle adds several features that are specific to building Android apps. Although the Android plugin is typically updated in lock-step with Android Studio, the plugin (and the rest of the Gradle system) can run independent of Android Studio and be updated separately.
Official docs here: https://developer.android.com/studio/releases/gradle-plugin.html
So, if there are version and platform related differences, we should expect the results to be different.
Hope this helps!

Unable to migrate Flutter project to AndroidX

I have a Flutter project in Android Studio. I am planning to migrate to AndroidX. Whenever I do Refactor -> Migrate to AndroidX, Android Studio shows error message:
You need to have compileSdk set to at least 28 in your module build.gradle to migrate to AndroidX.
However I have already set the compileSdkVersion 28 in my app/gradle.build file.
Is there anything else I need to do?
I was able to resolve my issue:
Open the Flutter project into Android Studio
Right click on the android directory, click on Flutter -> Open Android module in Android Studio. It asked me to upgrade my gradle plugin.
Then I installed Google Play Services. Tools -> SDK Manager -> SDK Tools and check the Google Play Services.
After the Google Play Services Installed, I did Refactor -> Migrate to AndroidX.
It started working.
Note: Do the refactor from the project window you opened in the step 2.
Here is how I got rid of "compileSdkVersion 28" error. But before I solved it this way, I upgraded Android Studio IDE from 3.4.1 -> 3.5.1. I am not sure if this was really needed. So whatever version of IDE you have, see if you can use following steps to solve it.
Go to "Project Structure" - (2nd icon on left of AVD Manager icon on top right corner of IDE)
On "Project Structure" dialog under left navigation click on Project Settings->Modules
After selecting Modules, on the right pane you should see 3 tabs Sources, Paths, Dependencies
Click on Dependencies, if you are getting compileSdkVersion 28 error the SDK version under Dependencies is pointing to version lower than 28 -> Select 28 or higher
5. Click OK
Now Refactor->Migrate to AndroidX worked for me
In gradle.build (app) add this
compileSdkVersion 28
defaultConfig {
......
minSdkVersion 21
targetSdkVersion 28
......
}
And there are some implementations required to use androidx :-
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
And add these given two lines(below) to gradle.properties:-
android.useAndroidX=true
android.enableJetifier=true
I'm having exact same problem. Been trying to migrate since all the new firebase stuff is forcing it, but the current guides to upgrade are sparse. First I was trying on a backup of my project with a lot of addons and kept getting that error, then I created a brand new flutter project with all the latest (dev channel), followed every step, checked every detail, read every forum, but still getting "You need to have compileSdk set to at least 28 in your module build.gradle to migrate to AndroidX."...
Matched the steps from https://androidxhackathon.blogspot.com/2018/05/refactor-to-android-x.html too and can't get it to refactor right.
Even went to File>ProjectStructure>Project SDK and set to Android API 28 Platform.
Anyone know what we might be missing? Did you have any luck Sam?
Click on Invalidate cache/Restart from file option worked for me
Try replacing the default in dependencies {...} in app/build.gradle with this:
dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
The replacement is similar to what is posted in the "Not recommended..." section from this link: https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility
If you can explain why this works, please add a comment. Thank you!
Go to "Project structure" in the Files menu. Clear out every issue you see there, like
set the android sdk, fix the problems specified
A simple solution.
follow the steps.
Create a new app --> (tick androidX in the window) --> finish
Add some lines in the android --> app --> build.gradle as follow,
update the defaultConfig
minSdkVersion 19
targetSdkVersion 28
multiDexEnabled true
Go to Refractor --> migrateToAndroidx
Do Flutter clean
Restart
Run
hope it works now ...

android studio: how to regenerate build.gradle

Is there way to have android studio automatically rebuild build.gradle?
I have a case where the compileSdkVersion is wrong (says 18, should be 22). I changed it in the AndroidManifest.xml file but that change did not propagate to the build file. I tried build-clean project, build-rebuild project to no avail.
Obviously I can change it by hand but I would prefer to see a solution that is more automatic and less prone to errors I might unwittingly introduce.
From the official Android page: http://developer.android.com/tools/building/configuring-gradle.html
The defaultConfig element configures core settings and entries in the
manifest file (AndroidManifest.xml) dynamically from the build system.
The values in defaultConfig override those in the manifest file. The
configuration specified in the defaultConfig element applies to all
build variants, unless the configuration for a build variant overrides
some of these values.
You should provide sdk version only in gradle.build file once. If you provide any sdk settings in AndroidManifest.xml it will be overridden by build system with the values in your gradle.build file.
If you create a new project in your latest Android Studio you will notice that Android doesn't put any API version related settings in manifest file anymore. They all go to gradle.build file.

android studio target application

I try to use android studio. I created simple app. When I launch it I see that it is application which I created before not the current which I want to launch. I am really confused how to change target for app.
You just follow this steps it will helpful
Whenever you want to update your minSdkVersion in current project
Update src/build.gradle - Make sure is the one under src folder
Sync gradle button
Rebuild project

Android Studio - no debuggable applications

I tried to debug a release version of my Android application but Android Studio failed to attach a debugger. (It could not find Android procces of my running application).
Under devices console, there was only a message:
No debuggable applications
You also should have Tools->Android->Enable ADB Integration active.
The solution is to turn on debuggable flag (debuggable true) in application's gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "org.example"
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
debuggable true
}
}
}
After Android Studio synced Gradle with project files and reinstalled an application, the debugging start working.
Just click the "Bug" icon and you will be good to go.
I had the same problem and that did the trick.
hope it helps someone.
if intellij or AS you should make sure your Build Variants Setting is debug.
Check if Tools->Android->Enable ADB Integration is enabled.
Also disable and re-enabling it works most of the times.
I want to add one point to above 2 answers :
If Eclipse IDE is running parallel to Android Studio then above answers may not help you. The debuggable application will not refresh event after setting debuggable true in Gradle file.
So, close Eclipse IDE and check in Android Studio. The debuggable application will refresh.
Thank you
Believe it or not,a faulty USB cable can cause this problem to linger even if you follow all the suggestions here.Changing my USB cord did it for me
It happen to me when I wanted to debug my app after I Generated a Sign APK, so I had to change back the Build Variants value from release to debug.
Multiple Options to do this:
1.Tools->Android->Enable ADB Integration
OR
2.Build->Clean Project , the Build-> Rebuild Project. Then "adb kill-server" and "adb start-server"
One of above will work.
You should check also in your AndroidManifest.xml file that there is no option like android:debuggable="false" in your application tag.
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:debuggable="false">
To make it work just remove this option.
Ctrl+shift+A
Type Enable ADB Integration
click on toggle
Done
This is the problem I encountered.The AAR file you import may change the attribute of debuggable, So just remove the debuggable attribute in your Main App Manifest file.
See the picture:
Working solution
1.For testing run as debug if everything is correct process is attached and you can able to debug.Can see in the list
2.If it says application not debuggable then alter gradle
Repeat step 1
4 .If not toggle Tools->Android->Enable ADB Integration and make it enabled.
5.Restart adb server
6.Dis connect and connect device It will list
Believe it or not, if you are on Windows OS, region setting has something to do with this too.
make sure your Regional Format is on English (United States) or actually any setting which makes your Calendar non-unicode (e.g. not On Persian Calendar or Arabic ...)
Apparently someone in Android Studio or ADB Dev team used .toString() with no respect to culture somewhere in their code !
Click the green bug, but make sure the drop down is set to app, if not it will not deploy your app, and just say something about.
Connected to the target VM, address: 'localhost:xxx', transport:
'socket'
Make sure your build variant is set to "debug". And debug has debuggable true on your build.gradle file
I used Mac to run Android Studio. I agree with #Kushal, I did not open Eclipse though.
I shut off Parallels Desktop and other APPs which may use the same Gradle.

Resources