Android problems when import library project using IntelliJ - java

I am not sure why I cannot import the android.support.v7.app.ActionBarActivity class. I looked at and followed the steps described in this question to change the jar file of the project. However in the solution the first step was to delete the jar file that was already there, I however did not have that file. I am not using gradle.
This is code I got from the android tutorial.
package com.example.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActvity;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
}
The error that comes up first is
Error:(5, 30) java: package android.support.v7.app does not exist
In the manifest also wrote this line.
<uses-sdk android:minSdkVersion="7"/>
I assume this is okay because I would need to make sure that v7 stuff is supported. As mentioned in the title I am using IntelliJ.

If you are using IntelliJ IDEA and not Android Studio (gradle based), you
should add the library by going to File > Project Structure
then go to your module, tab Dependencies, and add the jar file from there.
Jar files in libs are not automatically added in IntelliJ, you have to manually add them as dependencies.

Add this in build.gradle in dependencies
compile 'com.android.support:appcompat-v7:21.0.0'

Related

Can't import class from jar file in IntelliJ

So I am trying to import this class from my jar file. IntelliJ recognizes access to all the folder structures but not the class.
Says: "Cannot resolve symbol 'Constants'.
Note that I have tried clicking "Add library 'premiumdue.main.jar' to classpath" and it still doesn't work.
I have no idea why it won't let me import the class.
Here is a minimal intellij project showing my issue: https://www.dropbox.com/s/xzvv2x1ca2lld26/jar_issues.zip?dl=0
For your sample Gradle project, the dependency jar has to be referenced in build.gradle file as described in this answer.
dependencies {
implementation files('../create_jar.jar')
}
Proof:

Cannot find the UnityPlayerActivity class inside com.unity3d.player

I'm trying to create an Android plugin for my Unity game. I have watched a lot of tutorials (most of them are outdated based on eclipse) and have read the documentation also. I'm using Unity 2019.3.0f6. I want to extend my main activity in Android Studio project with UnityPlayerActivity
I don't understand what does the ending lines mean "Locate the file, and add classes.jar to the classpath Unity uses to compile the new Activity. Compile your Activity source file and package it into a JAR or AAR package, then copy it into your Project folder"
I understand UnityPlayerActivity does not exist in the classes.jar at PlaybackEngines/AndroidPlayer/Variations/mono or il2cpp/Development or Release/Classes/ and so I can't import com.unity3d.player.UnityPlayerActivity; I can only import UnityPlayer and IUnityPlayerLifecycleEvents. I am also interested to understand what is the UnityPlayer class and IUnityPlayerLifecycleEvents in this context.
But the UnityPlayerActivity.java is available at C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\src\com\unity3d\player
How do I add it in the Unity3d library or classes.jar. Or even import it to extend my mainactivity. I don't understand what am I doing wrong here.
I am using Android Studio, although I am new to it.
Don't worry, make android plugins for unity could be a little bit messy at the start, even more if you don't have any experience with Android!
Some tips:
Project directory structure should be: Assets/Plugins/Android (it's
important, I've been struggling for this stupidity here)
Insert classes.jar in AndroidStudio project in app/libs.
Insert external dependencies (if you are using it) like
"support-v4-24.1.1" into Android/libs
To create plugins on AndroidStudio you need to create a library (this steps is to create it from an activity):
On graddle remove ID line
On the same file, change .implementation to .library
To recompile the plugin do the follow:
Rebuild AndroidStudio solution
Go to AndroidStudio solution...app\build\outputs\aar get the .aar
file
Copy and paste it, change the extension from .aar to .zip or .rar
Open the modified file and extract 2 items:
classes.jar (this is another classes.jar, not the same stored in app/libs in your AndroidStudio project)
AndroidManifest.xml
Copy those files into Unity project in Assets/Plugins/Android
(remember, project directory structure is important!)
You can download the classes.jar file (the first one) from my Utility_Repo or from the path you name it D:\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Development\Classes\classes.jar.
I've got into the same situation, and after some search I've found that you should take that class from elsewhere and simply copy it into your project. On my machine the class is in "...path-to-unity-installation...\Editor\Data\PlaybackEngines\AndroidPlayer\Source\com\unity3d\player".
I think that the new approach that sidesteps using UnityPlayerActivity is totally worth attention, though.
There is a really great tutorial and accompanying youtube video on how to make native Android plugins and incorporate in Unity 3D.
http://www.cwgtech.com/using-android-webview-to-display-a-webpage-on-top-of-the-unity-app-view/
Create a new Module, eg: name it "UnityActivity".
Add classes.jar which can be found in unity install folder as Dependences in the method of "CompileOnly"
Add the Source Code of "UnityPlayerActivity" in to module of UnityActivity.
Add the new Module as Dependences to "Your Module" in the method of "CompileOnly"
Now you can create your CustomActivity extends from UnityPlayerActivity. and build into *.aar.
The idea is to mock the dependency com.unity3d.player. In Android Studio:
Create a new project (MyUnityPlayerActivity) with "no activity".
From File/New/New Module, create new Module (Player) with package name com.unity3d.player.
Apply these for "both" modules (app and player):
Clean-up any non-library references (icons, themes,...etc.) from AndroidManifest.xml.
Delete everything under the folder res except res/values/strings.xml.
In build.gradle of the module:
Replace id 'com.android.application' with id 'com.android.library'.
Delete the line applicationId "...".
Delete all lines related with tests.
Delete all dependencies.
Add local Unity installation dependency as "compile only" (so that it is not included in build).
dependencies {
compileOnly files('C:/Program Files/Unity/Hub/Editor/2019.4.32f1/Editor/Data/PlaybackEngines/AndroidPlayer/Variations/mono/Release/Classes/classes.jar)
}
In module player:
Copy UnityPlayerActivity.java from C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\src\com\unity3d\player\ to /player/java/com.unity3d.player.
In module app:
In build.gradle of module MyUnityPlayerActivity add compile only dependency to the module player we just created.
dependencies {
compileOnly files('C:/Program Files/Unity/Hub/Editor/2019.4.32f1/Editor/Data/PlaybackEngines/AndroidPlayer/Variations/mono/Release/Classes/classes.jar)
compileOnly project(':player')
}
Create new class MyUnityPlayerActivity.
package com.mycompany.myapplication.player;
import android.os.Bundle;
import android.util.Log;
import com.unity3d.player.UnityPlayerActivity;
public class MyUnityPlayerActivity extends UnityPlayerActivity {
private static final String TAG = "Unity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Running MyUnityPlayerActivity.");
}
}
It should be good to go.

Android app with 'DB2SimpleDataSource Class not found' error

I have an Android app in which I wanted to use the DB2SimpleDataSource class contained in the db2jcc.jar / db2jcc4.jar file. I wanted to use this class to establish a connection to a DB2 Database, but I noticed a problem with that class.
So I wrote a very simple app which just instantiate a DB2SimpleDataSource class object,in this way:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.ibm.db2.jcc.DB2SimpleDataSource;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DB2SimpleDataSource dataSource = new DB2SimpleDataSource(); //--->Error!
}
Here's the problem:when I write this code,Eclipse shows no errors about this class,but when i run this app,it shows the following error in logcat:
Could not find class 'com.ibm.db2.jcc.DB2SimpleDataSource' referenced
from method 'com.example.provadb2.MainActivity.onCreate'
Here's the process I used to add the JARs:
Right click on project>Properties
From Java Build Path,in Libraries tab,i clicked "Add External JARs" and pointed to the db2jcc.jar file
In Order and Export tab,i checked db2jcc.jar
I also tried unchecking in the Order and Export tab but nothing happened.
I also tried to use db2jcc4.jar file,without results.
The "funny" thing is that,when I wrote the same code in Java instantiating a DB2SimpleDataSource class object,it worked without errors...Hope i can find some help,thanks.
Your problem is at runtime, not compile time. Create a lib folder, add your external jars. Now click on individual jars and add to build path.

Why can't I see android.support.v7 files?

I'm trying to do the demo for the new Cast SDK (for using Chromecast), but I'm having trouble. I downloaded a demo for Android, and I'm getting errors in my project when trying to import anything from android.support.v7 library.
If I try to import from android.support.v4 or android.support.v13, things show up, but nothing shows up when I try to import from android.support.v7. It seems as if I don't have these files, but when I open the Android SDK Manager, it seems that I have most things installed (Android 3.0 API 11 and above).
Does anyone know why I might be missing this, or how to get it up and running?
Here are the files that I need to import for my demo:
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.MediaRouteActionProvider;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.support.v7.media.MediaRouter.RouteInfo;
If you wants to integrate actionbar below android sdk 3.x use given link reference:
Android Demo:
http://developer.android.com/tools/support-library/features.html
"uses-sdk
android:minSdkVersion="7"
Support Library classes that provide support for android sdk 2.x and above.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("ActionBar Support Demo");
}
}
http://www.coderzheaven.com/tag/android-support-v7-appcompat-jar/
There are multiple v7 libraries: appcompat, mediarouter and gridlayout. Follow the steps here to import and set them up (use Android Libraries with Resources); you need to install appcompat first and then mediarouter and make sure the mediarouter library lists appcompat as part of its dependency.
If you have already downloaded libraries, it will be present in android-sdk
at following location.
android-sdk\extras\android\support\v7\mediarouter\libs\android-support-v7-mediarouter.jar
similarly include other jars as well from follwing folder.
android-sdk\extras\android\support\v7
add these jars in your android project inside libs folder.
I fixed the import of MediaRouter by adding this line into the dependencies inside the build.gradle file:
compile 'com.android.support:mediarouter-v7:21.0.3'
Android Studio imports that as a library so there won't be any troubles with the resources at all.
You need to download and install Android Support Repository using the SDK Manager in Android Studio.
After that add the dependencies inside the build.gradle file as shown below
dependencies {
'compile com.android.support:mediarouter-v7:25.0.0'
}

NoClassDefFoundError: ResponseParserRegistrar

I have put the rest-assured-2.2.0.jar in my Eclipse workspace (for an Android app) and added it to the buildpath of the project as referenced library. Then I imported:
import com.jayway.restassured.RestAssured;
import static com.jayway.restassured.RestAssured.*;
import static com.jayway.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
But still if I do RestAssured.given(); I get
Caused by: java.lang.NoClassDefFoundError: com.jayway.restassured.internal.ResponseParserRegistrar
If I look inside the jar file I can find a ResponseParserRegistrar.class file on the right place, so it does exist.
If I try to import it it does not complain:
import com.jayway.restassured.internal.ResponseParserRegistrar;
But it still gives the error.
What did I forget or do wrong?
EDIT
Now I've created a blank new Android app with the following MainActivity:
package com.example.testrestassured;
import com.example.testrestassured.R;
import android.app.Activity;
import android.os.Bundle;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.RestAssured.*;
import com.jayway.restassured.internal.ResponseParserRegistrar;
import com.jayway.restassured.matcher.RestAssuredMatchers.*;
public class MainActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
RestAssured.given();
}
}
The project has only one referenced library: rest-assured-2.2.0.jar (correct spelling).
Still I get the same problem
E/AndroidRuntime(13142): Caused by: java.lang.NoClassDefFoundError: com.jayway.restassured.internal.ResponseParserRegistrar
EDIT
Changing to rest-assured-2.3.0.jar (new version) and adding json-schema-validator-2.3.0.jar and it's big list of dependencies did not fix it either. I've tried to find the runtime classpath, but I can't find it in the menus of ADT.
Make sure that you don't have more than one version of the jar. This is very common in projects using Maven, when parent projects have older versions of the libraries.
If you tell us more about your configuration it would be easier to try and find the cause.
Look NoClassDefFoundError is thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
Now in above as you are having MAVEN project it might be you have not added dependency of your rest-assured-2.2.0.jar in pom.xml.
And if you're building either Eclipse RCP or Eclipse plugin.
If yes, you should put rest-assured-2.2.0.jar under plugin dependencies. Go to plugin.xml, Runtime and put rest-assured-2.2.0.jar in the classpath.
Moreover as a cross-check please check the spelling name you used everywhere is same.

Categories