AndroidStudio preview class not found org.json.JSONObject - android-studio

I work with Android Studio 2.1.1. I am writing a class which extends LinearLayout. This class has a string attribute which I try to parse with basic android JSON lib in the class constructor. The code seems fine as it compiles and installs to the device. But Android Studio cannot find the class in the Preview (see screenshot below).
What is the solution of problem?
How do I set 'JSON lib' of Android Studio Preview?
Android Studio Screenshot
Android Studio error stack:
java.lang.ClassNotFoundException: org.json.JSONException
at org.jetbrains.android.uipreview.ModuleClassLoader.load(ModuleClassLoader.java:154)
at com.android.tools.idea.rendering.RenderClassLoader.findClass(RenderClassLoader.java:52)
at org.jetbrains.android.uipreview.ModuleClassLoader.findClass(ModuleClassLoader.java:92)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2483)
at java.lang.Class.getConstructor0(Class.java:2793)
at java.lang.Class.getConstructor(Class.java:1708)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:396)
at ...

Using Android Studio 2.2.3, I had the same problem, and sky's workaround worked for me, but I looked deeper and discovered the ClassNotFoundException was caused specifically by a reference to JSONException in code not ever executed in the view draw code needed for the layout preview. Interestingly, non executed references to JSONObject do not cause a ClassNotFoundException, but apparently the compiler wants to load the JSONException class regardless.
My solution was to catch Exception instead of JSONException in my code (which would be unaffected by the change). I decided this was preferable to adding the otherwise unnecessary library.

I did found only the WORKAROUND for this kind of issue. I did download 'json-20160212.jar' from maven repo. Then placed it to project's root directory (or other directory you like), and did add the dependency in project's build.gradle:
dependencies {
provided files('json-20160212.jar')
}
I'm using 'provided' scope to avoid json library been compiled with my project, but have an ability to 'preview' my layout without ClassNotFoundException.

Related

Kotlin Compose Preview reference unresolved

I am working on a Kotlin Multiplatform project for Android and Desktop using Compose and have the Compose Multiplatform IDE Support plugin installed.
What I am trying to do is add the #Preview annotation so I can get a preview of the composables that I'm working on. I tried following this guide to implement that:
https://developer.android.com/jetpack/compose/tooling/studio
I want to have my shared Compose classes in the module shared-ui, so I added the following dependencies to the build.gradle.kts in that module:
dependencies {
debugImplementation("androidx.compose.ui:ui-tooling:1.3.2")
implementation("androidx.compose.ui:ui-tooling-preview:1.3.2")
}
However, even after a synchronization, when I try to use or import the #Preview annotation in my class (shared-ui/src/commonMain/kotlin/.../MyScreen.kt), I get this error:
Unresolved reference: preview
That happens twice: Once in the import statement, where the final word Preview is in red, and once in the annotation, where it is also the word Preview that is in red:
[...]
import androidx.compose.ui.tooling.preview.Preview
[...]
#Preview
#Composable
[...]
Incidentally, I also tried the quick fix option that Android Studio offered me there: "Add dependency on androidx.compose.ui.ui-tooling-preview and import". What that does is add the dependencies exactly where I added them (provided I remove them first, otherwise it does nothing), which apparently means that Android Studio agrees with me that this is where the dependencies should go.
Which brings me to my ultimate question: Why does this not work? What am I doing wrong? Based on my understanding, this should work. But it doesn't. Am I missing something?

Android Studio Kotlin databinding: Unresolved reference on BR

I have some code that is generating a "red squiggly" error in Android Studio:
#get:Bindable
var title: String = ""
set(value) {
field = value
notifyPropertyChanged(BR.title)
}
It complains that "title" is an unresolved reference on BR.title. Building and running works fine though, and this is the only error I can see. I debug there and see that it's gotten the value for BR.title correctly.
Still, I can't figure out how to make it go away. I verified that the generated BR class has the "title" field, but Android Studio refuses to recognize this. I've looked up people having this issue and have tried the following: (unsuccessfully)
Closing Android Studio, deleting the .gradle, .idea and build folders and restarting
Build -> Clean Project, Rebuild Project
File -> Invalidate Caches and restart
Disabling and enabling the Kotlin plugin
Closing and reopening the project
I have also checked and I have apply plugin: 'kotlin-kapt' in build.gradle.
Anyone know what's going on? I assume it must be holding onto some cache files somewhere but I don't know where.
What fixed the problem for me was adding the following import to my files:
import androidx.databinding.library.baseAdapters.BR
I'm not entirely certain why this works, but it got rid of all the analysis problems and the application still compiles and works fine, so I'm personally happy.

ProGuard issues with Android Archieve(.aar) library

I've a library project which I want to distribute, in AAR format. I've applied ProGuard on it.
ProGuard settings works well if I use this lib as a module, but not when I use resulting AAR. Basically it fails with exception "java.lang.IllegalArgumentException: already added Lmy/lib/package/name/R;" in Application project.
ProGuard is renaming one of my lib class to R, and I feel which is causing problem in Application project as R is used for Resources.
How do I specify proguard mapping in Studio to tell it not to rename to R.
Update:
after adding AAR file as a new module, am getting this error:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lmy/lib/package/name/R;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
Okay then, answering myself.
Tried running ProGuard manually on .aar, worked with latest version v5.2.1. Didn't work with default version v4.7 which came with Studio v1.2.2. Hope this helps.

From Eclipse to Android Studio: Why is proguard.cfg used when NOT building Release?

So, I have been trying to migrate the development of a multi-project (eclipse projects that is) app to Android Studio (v1.1).
Importing via File > Import Project was really easy and went without incident.
However, when I tried Make/Rebuild project I encountered numerous surprises. The first, not Proguard-related, had to do with source file encoding (originally in Windows-1252), but I quickly found a solution using this Windows-1252 > Reload > UTF-8 > Convert answer.
Then, "error: cannot find symbol class" for a com.comp.mod.X class of mine. I then solved it by going to the module (Android Studio module that is, previously an Eclipse project) that contains that class and added to its proguard.cfg the following:
-keep public class com.comp.mod.X
Which left me with an "error: cannot find symbol variable" for a const BB defined in the aforementioned class. I solved this by adding to the same proguard.cfg the following:
-keepclassmembers class com.comp.mod.X {
public static final boolean BB;
}
That left me with only one "error: cannot find symbol method getPreferenceX()". I solve this by adding to the same -keepclassmembers that method, so that the above now looks:
-keepclassmembers class com.comp.mod.X {
public static final boolean BB;
public static java.lang.String getPreferenceX();
}
I thought I was done, but to my dismay, when I attempted to rebuild the project, I received numerous additional errors from other modules in the project, all of the type to "error: cannot find symbol class/variable/method" and related to some proguard.cfg.
I could have continued butchering the proguard.cfg files for each and every module but at this point I am beginning to suspect that I am doing something fundamentally wrong, because those proguard.cfg files work perfectly under Eclipse. So,
Why all of a sudden these are no longer good for Android Studio?
If ProGuard runs only when you build your application in release mode, why is the Android Studio based build complaining about "cannot find symbol" due to Proguard's obfuscation?
What am I missing?
Note: I have not attempted "Generate Signed APK". All I do is "Rebuild Project".
Currently (as of 2015-04-24), minifyEnabled's default value of false for all build types is incorrect for multi-module projects, in which some modules (app included) are dependent on other modules. This is due to bug #52962 that causes build types to not propagate to libraries -- they are always built as RELEASE.
Suggestions to work around this bug or notifications of its fix are most welcome.

The hierarchy of the type MainActivity is inconsistent

I created a new Android project via eclipse, with a MainActivity.
I added ActionBarSherlock to the project (Properties > Android and clicked Add).
Then, when I replace "extends Activity" with "extends SherlockFragmentActivity" I get a compile error "The hierarchy of the type MainActivity is inconsistent".
I also tried to create a project without an Activity, then created a class "MainActivity" that inherits from SherlockFragmentActivity, and there are no compile time errors, but with a run-time error "ClassNotfoundException: com.NadavLitvak.nadavfragmentdemo.MainActivity"
Your project should also reference the android-support-v4.jar (which is included in ABS's libs folder.) So, Properties->Java Build Path->Add JARs... and select that jar from ActionBarSherlock->libs
Sometimes, when Eclipse gets retarded, it doesnt add the SDK jar in your project (the Android x.y folder missing in project) so then you need to add it Properties > Android > Project Build Target (usually nothing there is ticked)

Resources