I have a library I want to add to my Android Application. But the problem is that the library is separated into .java files. Now I have tried to add a module to do the job but it does not work. My problem is I do not know how to add a bunch of .java files to my Android project to use throughout my app. I am new to Android dev and I have looked around but cannot find a clear explanation of how to add my type of library to an Android project.
I can see a couple of problems in the screenshot that you just posted above.
1) It seems that you're not telling Gradle (and therefore Android Studio) that you have Java files that need to be compiled. You can fix that by adding this to the build.gradle of the TMDB-Lib module:
sourceSets {
main {
java {
srcDir 'libs'
}
}
}
and then adding the module to your settings.gradle.
2) It seems that the Tmdb classes don't have a package (i.e. their first line isn't a package declaration) so the right way to import them into your Java code is by using just their class name:
import TmdbApi;
instead of:
import TMDB-lib.libs.TmdbApi;
Related
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.
I know there are a lot of questions that seem similar. I have also spent a few hours getting to grips with Gradle multiprojects. But I still don't understand what the best course of action is here. Incidentally I am using Groovy as my coding language, but explanations referencing Java would be just as good.
I have developed an Eclipse Gradle project, "ProjectA", which in particular has a class, IndexManager, which is responsible for creating and opening and querying Lucene indices.
Now I am developing a new Eclipse Gradle project, "ProjectB", which would like to use the IndexManager class from ProjectA.
This doesn't really mean that I would like both projects to be part of a multiproject. I don't want to compile the latest version of ProjectA each time I compile ProjectB - instead I would like ProjectB to be dependent on a specific version of ProjectA's IndexManager. With the option of upgrading to a new version at some future point. I.e. much as with the sorts of dependencies you get from Maven or JCenter...
Both projects have the application plugin, so ProjectA produces an executable .jar file whose name incorporates the version. But currently this contains only the .class files, the resource files, and a file called MANIFEST.MF containing the line "Manifest-Version: 1.0". Obviously it doesn't contain any of the dependencies (e.g. Lucene jar files) needed by the .class files.
The application plugin also lets you produce a runnable distribution: this consists of an executable file (2 in fact, one for *nix/Cygwin, one for Windows), but also all the .jar dependencies needed to run it.
Could someone explain how I might accomplish the task of packaging up this class, IndexManager (or alternatively all the classes in ProjectA possibly), and then including it in my dependencies clause of ProjectB's build.gradle... and then using it in a given file (Groovy or Java) of ProjectB?
Or point to some tutorial about the best course of action?
One possible answer to this which I seem to have found, but find a bit unsatisfactory, appears to be to take the class which is to be used by multiple projects, here IndexManager, and put it in a Gradle project which is specifically designed to be a Groovy library. To this end, you can kick it off by creating the project directory and then:
$ gradle init --type groovy-library
... possible to do from the Cygwin prompt, but not from within Eclipse as far as I know. So you then have to import it into Eclipse. build.gradle in this library project then has to include the dependencies needed by IndexManager, in this case:
compile 'org.apache.lucene:lucene-analyzers-common:6.+'
compile 'org.apache.lucene:lucene-queryparser:6.+'
compile 'org.apache.lucene:lucene-highlighter:6.+'
compile 'commons-io:commons-io:2.6'
compile 'org.apache.poi:poi-ooxml:4.0.0'
compile 'ch.qos.logback:logback-classic:1.2.1'
After this, I ran gradle jar to create the .jar which contains this IndexManager class, initially without any fancy stuff in the manifest (e.g. name, version). And I put this .jar file in a dedicated local directory.
Then I created another Gradle project to use this .jar file, the critical dependency here being
compile files('D:/My Documents/software projects/misc/localJars/XGradleLibExp.jar' )
The file to use this class looks like this:
package core
import XGradleLibExp.IndexManager
class Test {
public static void main( args ) {
println "hello xxx"
Printer printer = new Printer()
IndexManager im = new IndexManager( printer )
def result = im.makeIndexFromDbaseTable()
println "call result $result"
}
}
class Printer {
def outPS = new PrintStream(System.out, true, 'UTF-8' )
}
... I had designed IndexManager to use an auxiliary class, which had a property outPS. Groovy duck-typing means you just have to supply anything with such a property and hopefully things work.
The above arrangement didn't run: although you can do build and installdist without errors, the attempt to execute the distributed executable fails because the above 6 compile dependency lines are not present in build.gradle of the "consumer" project. When you put them in this "consumer" Gradle project's build.gradle, it works.
No doubt you can add the version to the generated .jar file, and thus keep older versions for use with "consumer" projects. What I don't understand is how you might harness the mechanism which makes the downloading and use of the dependencies needed by the .jar as automatic as we are used to for things obtained from "real repositories".
PS in the course of my struggles today I seem to have found that Gradle's "maven-publish" plugin is not compatible with Gradle 5.+ (which I'm using). This may or may not be relevant: some people have talked of using a "local Maven repository". I have no idea whether this is the answer to my problem... Await input from an über-Gradle-geek... :)
You should be able to update the Eclipse model to reflect this project-to-project dependency. It looks something like this (in ProjectB's build.gradle):
apply plugin: 'eclipse'
eclipse {
classpath.file.whenMerged {
entries << new org.gradle.plugins.ide.eclipse.model.ProjectDependency('/ProjectA')
}
project.file.whenMerged {
// add a project reference, which should show up in /ProjectB/.project's <projects> element
}
}
These changes may be to the running data model, so they may not actually alter the .classpath and .project files. More info can be found here: https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.eclipse.model.EclipseModel.html
This issue is discussed here: http://gradle.1045684.n5.nabble.com/Gradle-s-Eclipse-DSL-and-resolving-dependencies-to-workspace-projects-td4856525.html and a bug was opened but never resolved here: https://issues.gradle.org/browse/GRADLE-1014
I have to use PKCS10 class into my project. I tried to import sun.security.pkcs.PKCS10; into my activity code but studio shows "Cannot resolve symbol PKCS10" at both places where I have imported it and where I want to instantiat it.
Note: I haven't added any dependency or library (like .jar) to my project.
I want to know whether can it be automatically?
The class sun.security.pkcs10.PKCS10 was removed from the JDK. Using a class that was internal and already removed from the JDK is going a step beyond just using an internal API.
Put this into your build.gradle file.
compileJava {
options.forkOptions.javaHome = file(System.properties['java.home'])
}
And if this does not work, put this into you build.gradle(app) dependencies.
compile group: 'com.sun', name: 'rt', version: '1.5.0_06'
Ive got an Project an within it,I developed a bunch of classes wich is kept very abstract so I could use it in other projects. How should I outsource the package in a way so I can include it via gradle or by the IDE in the end?
Also the reusable packag-content is still in development so I want do work on it in paralelle.
Can anybody tell me how to solve this?
In your build.gradle use a custom build to collect only the package that you want
task customBuild(type: Jar) {
from ("src/main/java/abstract/"){
into "abstract"
}
version = ""
baseName = "myClasses"
}
it will build you a jar file inside build/libs/YouJarName
Now you can copy the jar to anywhere you want, and include it in another project in that way :
dependencies {
compile fileTree (dir : "your/jar/location", includes: ["myJar.jar"])
}
Right now I have got a Java library which has a test class. In that class I want to access some files located on my hard disk.
The build.gradle looks like this:
apply plugin: 'java'
dependencies {
testCompile 'junit:junit:4.11'
}
My file is under java_lib/src/test/assets/file.xml and the Java class is under java_lib/src/test/java/<package_name>.java
Therefore I execute
final InputStream resourceAsStream = this.getClass().getResourceAsStream("assets/file.xml");
Unfortunately I get null back. What am I doing wrong?
To get thing rolling you need to add the following to the gradle file:
task copyTestResources(type: Copy) {
from "${projectDir}/src/test/resources"
into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyTestResources
What it basically does is copying all the files in the src/test/resource directory to build/classes/test, since this.getClass().getClassLoader().getResourceAsStream(".") points to build/classes/test.
The issue is already known to Google and they want to fix it in Android Studio 1.2 (since they need IntelliJ14 for that and it seems like it will be included in Android Studio 1.2)
Try placing file.xml under src/test/resources and use this.getClass().getResourceAsStream("file.xml") (without the folder prefix)
The problem appears to be that the assets folder is not part of the test runtime classpath, hence this.getClass().getResourceAsStream("assets/file.xml") wouldn't be able to resolve the path as you expected.
By default, the test resources folder in a Gradle java project is src/test/resources (same as a Maven java project). You can override it to assets folder if you wish by adding this in the project's build.gradle file:
sourceSets.test {
resources.srcDirs = ["src/test/assets"]
}
In build.gradle, add this :
sourceSets.test {
resources.srcDirs = ["src/test"]
}
In your code, access your resource like this :
getClass().getClassLoader().getResourceAsStream("assets/file.xml"));
Works for me.
Thanks for pointing out the Google issue I've been looking all day for this...
In "Android Studio 1.1 RC 1" (gradle build tool 1.1.0-rc1) there is no need to add the work around to the gradle file, but your you have to execute the test from the gradle task menu (or command prompt)!
This worked for me (3 years later, gradle 4.10)
subprojects {
junitPlatformTest.dependsOn processTestResources
}