logcat doesn't display logs from library project in my special case - android

I have three Android projects: BaseLibProject, MainLibProject, AppProject.
The relationship of these three Android projects is that:
The BaseLibProject is a library project used by MainLibProject.
The MainLibProject uses BaseLibProject as library project, and generates MainLib.jar(use maven build)
The generated MainLib.jar is added to AppProject's libs/ folder & also added to the build path of AppProject.
NEXT:
A simple class in BaseLibProject :
public class BaseLibClass {
public static String doBaseTask() {
Log.i("MyLog", "doBaseTask..."); //I can't see this log
return "Result from Base!";
}
}
A simple class in MainLibProject which defined a function invokes the function in BaseLibProject:
public class MainLibClass {
public static void doMainTask() {
Log.i("MyLog", "doMainTask..."); //I can see this log in logcat
String result = BaseLibClass.doBaseTask();
Log.i("MyLog", "result = " + result); //I can see the result log
}
}
Finally, in my AppProject I simply call above function of MainLibProject (remember I have MainLib.jar):
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MainLibClass.doMainTask();
}
}
I started logcat with command adb logcat -s MyLog:* . When I run my AppProject application, logcat displays
I/MyLog(2039): doMainTask...
I/MyLog(2039): result = Result from Base!
According to above log, the code in BaseLibProject is running (because I got result from BaseLibClass), but it doesn't display any logs from BaseLibProject, why???
(I have googled on internect, someone got similar issue fixed by restarting eclipse, but in my case, it doesn't help. Besides, I am checking logs from terminal NOT from eclipse.)

Ok, finally, I fixed the problem.
The reason is that in BaseLibProject pom.xml, I defined <packaging>apk</packaging>, while in MainLibProject pom.xml, when I define the dependency of BaseLibProject, I didn't specify the <type> of the artifact.
After I changed BaseLibProject's pom.xml to <packagin>apklib</packaging> and in MainLibProject's pom.xml specified the dependency of BaseLibProject with <type>apklib</type> . I am able to see the logs in BaseLibProject.

Related

Timber - Problem with creating a custom Timber Debug Tree class

I'm trying to create a custom Debug Tree class to get the same result as the following:
I have followed this Stackoverflow answer:
Log method name and line number in Timber
But using that answer gives me two problems:
Implementing the custom Debug Tree class does not log anything when I use more than one method.
public class MyDebugTree extends Timber.DebugTree {
#Override
protected String createStackElementTag(StackTraceElement element) {
return String.format("(%s:%s)#%s",
element.getFileName(),
element.getLineNumber(),
element.getMethodName());
}
}
public class BaseApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG) {
Timber.plant(new MyDebugTree);
}
}
}
The above causes it to not log at all.
If I use only return element.getFileName(); it successfully logs that one error.
The second problem I'm having is that using a custom DebugTree class does not give me the same results as using err.getStackTrace()[0].getLineNumber().
}, err -> {
Timber.e("Method name: " + err);
Timber.e("Method name: " + err.getStackTrace()[0].getMethodName());
}
The custom Debug Tree class does not display the name of the method I'm trying to log.
Why is it not logging when I use all three methods?
Also how can I get it to log like it would using
err.getStackTrace()[0].getMethodName()?
I'm using 'com.jakewharton.timber:timber:4.7.1'
You seem to be doing it right. I recreated your code in kotlin (programming language should not matter) and i was able to show my logs.
MyDebugTree.kt
class QueenlyDebugTree : Timber.DebugTree() {
override fun createStackElementTag(element: StackTraceElement): String {
return "(${element.fileName}:${element.lineNumber})#${element.methodName}"
}
}
force log using an actual exception:
try {
throw RuntimeException("Hello World")
} catch (e: Exception) {
Timber.e(e)
}
i got a log:
So, from what i saw from your code, its most probably because you have a compact logcat view. To update your logcat view, follow these steps:
1. Make sure you have standard view selected
2. Configure standard view
3. Make sure Show tags is selected and tag column is at max(35)

The "GenerateJavaStubs" task failed unexpectedly

I have created one new project in Visual studio and only che in .xml file (put two frame layouts) and when i debug the code i have one error, please told me the solution
Error 1 The "GenerateJavaStubs" task failed unexpectedly.
System.InvalidOperationException: Sequence contains no elements
at System.Linq.Enumerable.Max(IEnumerable`1 source)
at Xamarin.Android.Tools.TypeNameMapGenerator.WriteBinaryMapping(Stream o, Dictionary`2 mapping)
at Xamarin.Android.Tools.TypeNameMapGenerator.WriteJavaToManaged(Stream output)
at Xamarin.Android.Tasks.GenerateJavaStubs.UpdateWhenChanged(String path, Action`1 generator)
at Xamarin.Android.Tasks.GenerateJavaStubs.WriteTypeMappings(List`1 types)
at Xamarin.Android.Tasks.GenerateJavaStubs.Run()
at Xamarin.Android.Tasks.GenerateJavaStubs.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__20.MoveNext() TMS_TabletView.Droid
This still can happen if you use one of Xamarin Plugins that use CrossCurrentActivity, for example Plugins.Share and if you have Application class with [Application] tag. This is because on nuget installation it generates another Application class with same tag and that causes 'GenerateJavaStubs' failure.
Solution is simply delete one of Application classes / merge them into one.
For me this happened after installing the Xamarin Share Plugin (https://www.nuget.org/packages/Plugin.Share). I was setting the theme of my Android app in its assemblyinfo.cs with the line below:
[assembly: Application(Theme = "#style/AppStyle.myApp")]
Simply removing this line resolved the issue, and I then set the theme in MainActivity as you should really anyway.
I had the same issue and the error in detail was saying something like; "Path is too long. Bla bla name cannot exceed 248 characters and the other bla bla cannot exceed 260 characters".
And shortening the project name solved my issue.
If you have constructor in Activity class, please add default constructor.
Or you can remove constructors in class.
This is old source code.
[Activity(Label = "MyActivity", ScreenOrientation = ScreenOrientation.Portrait)]
public class MyActivity : BaseActivity
{
bool param;
protected override int LayoutResource
{
get
{
return Resource.Layout.myactivity;
}
}
public MyActivity(bool param = false)
{
this.param = param;
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
...
}
This is updated new source code.
[Activity(Label = "MyActivity", ScreenOrientation = ScreenOrientation.Portrait)]
public class MyActivity : BaseActivity
{
bool param;
protected override int LayoutResource
{
get
{
return Resource.Layout.myactivity;
}
}
public MyActivity()
{
}
public MyActivity(bool param = false)
{
this.param = param;
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
...
}
I experienced this problem, and got all the same error messages while creating my very first app on Visual Studio Professional 2015 (with Xamarin). No idea if this will be of any use to anyone else, but we stumbled on something that fixed the problem for us.
In MainActivity.cs, there is some default code when you first open up a "Blank App" project, but we had deleted some of this code, and copied/pasted over it. This is what it initially looks like:
namespace App3
{
[Activity(Label = "App3", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
}
}
}
To fix it: We tried putting back these lines into MainActivity.cs code:
[Activity(Label = "App3", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
And then we ran the code and the error went away. Probably just a dumb mistake, and won't always solve the issue, but it worked for us.
I had this issue because for some reason my Android Class library got changed into an Android Application type - so VS was looking for Application class in the project.
Solution was to create a blank android class library, and compare project files.
I removed any xml tags from the broken project that were "application" specific.
like [<]AndroidApplication>true[<]/AndroidApplication>
I had this issue because i put activity events under #region block. after removing #region block, everything is working fine in my case.
I had this issue because of copying an Android Library to a new project.
Visual Studio automatically made me a new resource file, which resulted in having ambiguity problems between the resource file of project 1 and the file of project 2. After solving this, I didn't have a Java Stub error anymore. I don't know if this was the fix, but I didn't do anything else, so it almost has to be although it is a bit weird.
None of these answers ended up working for my issue. Here is what did work:
In your Solution Explorer, go to the Android project, right click -> Properties -> Android Options -> Linker -> Linking and choose Sdk Assemblies Only.
Also make sure in the Android project -> Properties -> Android Options -> Advanced properties -> Java Max Heap Size is set to 1G.
I was getting this error on doing a Release build
The "GenerateJavaStubs" task failed unexpectedly.
Nullreference Exception
Restarted VS cleared it up.

Getting "Source not found" when debugging my own files on android

This problems occurs when I'm debugging any android application on eclipse. Let´s say I've got 2 classes, each on a different file but on the same package:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
<BREAKPOINT> DummyClass.doSomething();
}
...
}
----------------
public class DummyClass {
public static diSomething(){
Log.e("TAG","HELLO");
}
}
When the execution stops at the breakpoint, I want to step into, to get to the DummyClass. However, I get a "Source not found" error, saying that android.jar is not accessible. Why? I'm not even trying to access that code.
I would appreciate much any help you you give me.
Looks like there is a spelling mistake in your code.
You have DummyClass.doSomething();and
public class DummyClass {
public static *diSomething*(){
Log.e("TAG","HELLO");
}
}
Change it to 'doSomething()' instead of diSomething()

Android error: NoClassDefFound error while using android-json-rpc

I am using android-json-rpc library. I added the android-json-rpc-0.3.4.jar library to my build path. Just following some basic tutorial.
Here is my code:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.text_view);
tv.setText(testMethod());
}
private String testMethod() {
// Crashes here on this line
JSONRPCClient client = JSONRPCClient.create("10.1.2.3/json", JSONRPCParams.Versions.VERSION_2);
String string = "";
try {
string = client.callString("cf.test");
} catch (JSONRPCException e) {
Log.i("JSON-RPC Client", e.toString());
}
return string;
}
}
Error:
*AndroidRuntime(1528): java.lang.NoClassDefFoundError: org.alexd.jsonrpc.JSONRPCParams$Versions*
While going around this error, I found out that some guy has a blogpost about the same issue on a Mac Lion (same as mine) but worked fine on Ubuntu. http://www.1771.in/android-jsonrpc-not-working-on-mac.html
Could anyone help me with a workaround for this issue?
Thanks,
Dexter
There are 2 options I have in mind. 1. Have "libs" folder in root of the project, copy lib there, no need to add to the build path. This is a new requirement from android team. 2. Add import of used class.
Just got it compiled and working.

Android Eclipse jUnit does not see superclass

This may be a simple answer.
1) Create a jar file with this code:
package com.myCompany.base;
public class Dex1 {
public String getTerm1() {
return "Term 1";
}
}
This is compiled to Dex1.jar using NetBeans.
2) Created a 'Hello world' android application in Eclipse. Add the code for Dex2 that extends Dex1. Copy and add Dex1.jar to the java build path.
package com.myCompany;
import com.myCompany.base.Dex1;
public class Dex2 extends Dex1 {
public String getTerm2() {
return getTerm1() + " Term 2";
}
}
in my onCreate() call:
editText.setText(dex2.getTerm2());
Everything works Great! I get the correct string displayed on the android screen. Yea!
Now the problem starts:
3) Create a jUnit 3 test case using Eclipse command File -> New -> Project -> Android Text Project command and add the code:
package com.myCompany.test;
import junit.framework.TestCase;
import com.myCompany.Dex2;
public class Dex2Test extends TestCase {
protected void setUp() throws Exception {
super.setUp();
dex2 = new Dex2();
}
protected void tearDown() throws Exception {
super.tearDown();
}
Dex2 dex2;
public void testGetTerm2() {
/*line 21 */ assertEquals("Term 1 Term 2", dex2.getTerm2());
}
public void testGetTerm1() {
/* line 25 */ assertEquals("Term 1", dex2.getTerm1());
}
}
On Line 25 the compiler gives a 'method undefined' error for getTerm1(). I don't understand why this is an error?
I tried to add the Dex1.jar to the java Build path of the test project, it compiles but we receive a run time error 'NoClassDefFoundError'. Yuch!
More Information 16Mar2012
I set this up using Plan Java classes, same jar file, to remove Android and it worked. This makes me conclude there must be some anomaly in Android/DalvikVM (aka DavrosVM).
More Information 16Mar2012 End
Am I missing something?
Is the jar file built incorrectly (didn't think that was possible)?
Am I importing the jar file incorrectly?
Is it just crazy to expect to import and override a class in a jar file?
Thank you for reading, please reply.
Fish
Take a look at Android Testing: External libraries which I guess have the solution to your problem.
What you may be doing wrong is not exporting your library in Order and Export under Java Build Path.

Categories

Resources