Custom plugin adding dependency beforeResolve from Android Studio - android

I have a custom Gradle plugin that uses the following code:
project.getGradle().addListener(new DependencyResolutionListener() {
#Override
void beforeResolve(ResolvableDependencies resolvableDependencies) {
depsToAdd.each { dep ->
compileConfig.getDependencies()
.add(project.getDependencies()
.create(dep)
}
}
#Override
void afterResolve(ResolvableDependencies resolvableDependencies) {
}
})
This seems to work fine from command line. However, if I refresh gradle from Android Studio, it barfs with Cannot change configuration :app:compile after it has been resolved
My guess is there is some sort of caching going on or Studio builds more variants (I just run the assemble for the variant I want from command line which works every time).
Does anyone know what might be going on and how best to resolve this?

I managed to figure this out. The way do this is to add a DependencyResolutionListener in which you add the dependencies and then remove the listener so it doesn't try to add them on later resolution steps.
compileDeps = project.getConfigurations().getByName("compile").getDependencies()
project.getGradle().addListener(new DependencyResolutionListener() {
#Override
void beforeResolve(ResolvableDependencies resolvableDependencies) {
compileDeps.add(project.getDependencies().create("org.foo:bar:$version"))
project.getGradle().removeListener(this)
}
#Override
void afterResolve(ResolvableDependencies resolvableDependencies) {}
})
I have a working example of a plugin that uses this here

Related

Debugging Android in custom libraries

I have just created a react-native library using react-native-create-library and imported it into my master react-native project.
There are some issues I'm having because (honestly) I lack the knowledge.
The problem is that there are no errors (using logcat) and I don't know how I can debug the android part of my imported library.
Example
public class RNZappsCameraModule extends ReactContextBaseJavaModule
implements ActivityEventListener {
#ReactMethod
public void myJavascriptMethod() {
// I want a breakpoint here
// cameraIntent initialization happens here
try
{
currentActivity.startActivityForResult(cameraIntent, requestCode);
}
catch (ActivityNotFoundException e)
{
e.printStackTrace();
}
}
#Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data)
{
// I want a breakpoint here
}
}
The camera intent starts fine, but I believe onActivityResult is never hit.
I know I could log everything and read it, but that won't explain why the result is never returned to my app since there are no errors in the first place.
Google and the official RN documentation are not being my friend at the moment, so please put me on the right track.
Found it.
No rocket science here. I don't know how I managed to do it in the end...
Anyheeew, to give this question a reasonable answer for passers-by...
First off, you need a react-native (master) project in order to actually run your library in a react-native context.
So create it and import your library. The easiest way to do this is by pushing your library into a git repository and adding your library in the package.json of you master project like this:
"react-native-your-package": "git+https://your-git-url-here",
Now install it: npm install react-native-your-package
In order to debug your library:
Open the android project of your react-native project in Android Studio
In menu => view => Tool window, click Build Variants
The new window displays the build types for you project and loaded modules
Click the Build Variant dropdown next to the module you want to debug and select 'debug'
Debug the master Android project
In the projects view, you can expand your module and place breakpoints where ever you like
Click the debug button and fix errors you never head of

AppCompatActivity.onCreate can only be called from within the same library group

After upgrading to appcompat 25.1.0 I've started getting weird errors.
In my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
I get lint error:
AppCompatActivity.onCreate can only be called from within the same library group (groupId=com.android.support)
How to prevent such behavior?
As previous responses highlighted, it is bug. I recommend not to disable the specific lint warning project-wide, but for that method only. Annotate your method as follows:
#SuppressLint("RestrictedApi")
#Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
//your code here
}
As Felipe already pointed out in his comment this is a bug in the pre-release version of the tools.
You can workaround it for now, until Google release a fix, by adding the following into your project module's build.gradle file:
android {
lintOptions {
disable 'RestrictedApi'
}
}
It's worth noting that this may hide true errors in your project as it suppresses all errors of that type, so the better option would be to downgrade the version of Android Studio and the tools used in the project.
Disabling the warning in lintOptions doesn't look a good option it's better to suppress inspection at the statement level.
Add this comment above the line of code which gives the warning:
//noinspection RestrictedApi

AndroidStudio was not right after change code

Situation:
I use Android studio, when i change a line of code in it sometimes it was wrong, the code i just change is not work it still run my old version code.
such as
int a = 1;//old version
int a = 2;//new version
sometimes a still value 1 when i run the new version code.
fix:
I know i can clean the project and restart Android Studio to fix it, but why it's happened?
My Question:
It's just a AS bug or something i was wrong in my project setting?
For more detail example:
I have class a with the method putLog() like below
private void putLog()
{
Log.i("tag","string");
}
Then i find i don't need the Log.i("tag","string") anymore, so i delete it
private void putLog()
{
// Log.i("tag","string");
}
but after i delete it, the log output is still there, my delete is not work.
I restart Android Studio and clean the cache, the log is not show anymore.
That's mainly because i have use multiple-channel gradle script to generate apk, now i remove it, it not show again.

Test events were not received - Android Studio

I have no idea how to test and I was following a tutorial.I am trying to run:
package name.company.sunshine.app.data;
import android.test.AndroidTestCase;
public class TestPractice extends AndroidTestCase {
/*
This gets run before every test.
*/
#Override
protected void setUp() throws Exception {
super.setUp();
}
public void testThatDemonstratesAssertions() throws Throwable {
int a = 5;
int b = 3;
int c = 5;
int d = 10;
assertEquals("X should be equal", a, c);
assertTrue("Y should be true", d > a);
assertFalse("Z should be false", a == b);
if (b > d) {
fail("XX should never happen");
}
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
}
}
but I get somewhere in the bottom left corner, in the console Test events were not received. What am I doing wrong ? Should I run something else ?
When you run your test select the Android Test option.
The JUnit and Gradle options should not be used for this type of test.
I am doing the course too and ended up with the same problem.
After an hour of tinkering I think I found the solution.
Don't try to run the the test cases from the whole package as they did in the video; you have to run it from a single class and choose the AndroidTest option. It does not work with the Gradle option.
See picture attached.
I was able to get past this problem after making two changes.
uncheck use in-process build in Settings -> Build Tools -> Compiler
Source: https://code.google.com/p/android/issues/detail?id=172162
force Gradle to re-run all tasks by updating your run configurations.
Add --rerun-tasks to the Script Parameters.
Source: https://www.bignerdranch.com/blog/triumph-android-studio-1-2-sneaks-in-full-testing-support/
For me, another test was not compiling with an error but the error was not obvious to see. I fixed the error and the tests ran after that.
This solution is tested in android studio 1.5.1
If you have problem with tests in android studio because use in-process build disappeared, include the following:
<project-folder>
|-- .idea
|-- workspace.xml
Just add the following component at the very top, just inside the project tag:
<project version="4">
<component name="AndroidGradleBuildConfiguration">
<option name="USE_EXPERIMENTAL_FASTER_BUILD" value="false" />
</component>
...
</project>
just as Matt Accola sayd, if you already selected the gradle option and cant find that sub menu in his answer, u will need to go to run >> Edit Configuration... and then under the Gradle sub menu, delete the items (TestPractice & others if existed) and then re do the test by selecting the AnroidTest.

How to tell when gradle is being run from AndroidStudio?

I need to build some hacks into my gradle build file so that Android Studio understands some things. I don't need these hacks when I run the build from the command line directory. Is there a way to detect when the build is being run from within Android Studio? Maybe through environment variables, etc?
Use gradle -P blah=val from command line and in your build.gradle use project.hasProperty("blah") or project.getProperty("test") or if (blah ... ) to decide whether run your hack or not.
Updated:
OK I found the direct way :)
def env = System.getProperties()
if (env['com.android.studio.gradle.project.path'] != null) {
// build from Android Studio, do magic here
}
With AndroidStudio 2.1.1, you can use the idea.platform.prefix property:
def sysprops = System.getProperties()
if (sysprops['idea.platform.prefix'] != null) {
// Built from AndroidStudio
} else {
// Built from command line
}
Jake Wharton suggests android.injected.invoked.from.ide to speed butterknife at development time by using reflection:
dependencies {
if (properties.containsKey('android.injected.invoked.from.ide')) {
implementation 'com.jakewharton:butterknife-reflect:<version>'
} else {
implementation 'com.jakewharton:butterknife:<version>'
kapt 'com.jakewharton:butterknife-compiler:<version>'
}
}
From Twitter:
Hey ButterKnife users: I'm working on a reflection-based implementation for use during development so the annotation processor is not needed.
A follow-up:
What is this? A property from (link: http://gradle.properties) gradle.properties?
The answer we want:
No it's added by the IDE

Categories

Resources