How to use the generated .so library in another Android Project? - java

I’ve followed http://kn-gloryo.github.io/Build_NDK_AndroidStudio_detail/ and it works well! However, I want to use the generated .so library in a new Android application, and I simply don’t know how to do this... I’ve been struggling for days and if any step-by-step tutorial can be shared that would be helpful!
This is the Android Project MyApp which I used to generate the .so files:
MainActivity :
Java Class : MyNDK
header file: com_demo_ble_myapp_MyNDK.h
Cpp file: MyLibrary
And this is the structure of my new Android project useSOLib, I simply copy all the so files from MyApp\app\src\main\libs to useSoLib\app\src\main\jniLibs
And this is MainActivity in useSoLib:
I can Build-> Make Project successfully, but when I run it on the device, the app shows "Unfortunately, useSoLib has stopped." and crushed. I know I miss some steps here, but I'm new to Android Studio so I have no clue where I should start with... thank you in advance for any suggestions! :)

You should import MyNDK and use its instance. JNI library is related to Java class. Change the code:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MyTag", new MyNDK().getMyString());
}
}
You can either export the MyNDK class as a jar package or create an aar bundle. Then import the Java class into your new project with JNI library.
The post How to Build *.so Library Files into AAR Bundle in Android Studio may help you.

Related

How do I begin to use this Java library?

Teaching myself Java, still very new. Last night I learned how to import this library into my project in Android Studio. But now I'm confused as to how to actually start using it.
I know that Java works with classes and that supposedly a library is just a collection of classes (and probably others things too...) that you can begin using once you import it. But the author of this library told me to just use this:
BackgroundMail bm = new BackgroundMail(context);
bm.setGmailUserName("sendername#gmail.com");
bm.setGmailPassword("sender_email_password");
bm.setMailTo("receiver#gmail.com");
bm.setFormSubject("Subject");
bm.setFormBody("Body");
bm.send();
But when I try to put that into another one of my classes, I get red errors all over the place. So then I tried to create a Java class within my app's files and I still got red errors. Could someone kindly help me, a total beginner, get started here? I'd like to use this library to send an email in the background of my app.
To import the library:
I followed this answer: https://stackoverflow.com/a/35369267/5241266 and used Approach 2.
MainActivity.Java: this is where I put the import code.
package moviehelper.moviesfree;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.creativityapps.gmailbackgroundlibrary.BackgroundMail;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
For now, I have not added the block of code that the author told me to use (see above).
The Gradle Console error:
I get the following error when doing Build -> Make Project:
error: package com.creativityapps.gmailbackgroundlibrary does not exist
import com.creativityapps.gmailbackgroundlibrary.BackgroundMail;
My project tree: I think there might be some problem with this structure. It looks like the library was added as its own project? Although I'm not sure.
If you don't want to use the JitPack approach as documented in the instructions, then look at the settings.gradle file in the Github example.
It includes both modules (app and the library).
Then, once that is setup, you can compile project(:libraryName) in the app/build.gradle file's dependencies section. Again, see the Github sample for the syntax.
With those two steps (plus one to download the library), it should be importable within the app code.
Assuming you followed along with the other steps on the readme, at the top of your code you need to tell the source file to import the classes:
import com.creativityapps.gmailbackgroundlibrary.BackgroundMail;
public class MainActivity extends AppCompatActivity {
[your code]
}
I would also recommend looking through the sample code included in the github project
You are doing some wrong steps friend. You can follow this
Find your gradle file with name buid.gradle (Project: [ YOUR_PROJECT_NAME ] )
Then find and add this line
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
You are done 50% now. Now press SHIFT+CTRL+ALT+S and wait for a window.
On the Window, select DEPENDENCY TAB on top.
Click on + BUTTON at top right. Click on Library Dependency.
Now paste com.github.yesidlazaro:GmailBackground:1.2.0 there.
Click on OK and exit the window. Gradle recompiles.
Now simply put your code, If RED syntax error showing, Click at the code where error is showing and when a BLUE pop comes, Press ALT+ENTER.
Everything done..!

Android how to run one small app in another

I have recently developed an app in which I wanna use a small app I have developed before, let's say in one view like below:
public class MyMain extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showmysmallapp(); //how?
}
}
I have saved the small app as aar file and imported it to my new app and added in my build.gradle dependency also:
dependencies {
compile project(":mysmallapp")
...
}
How can I run the entire small app when a view of my main app starts pls?
or should I have added the library in another way?
Much thanks ahead.
I guess you want to run an app as a plugin.
You can use one of these libraries:
VirtualApp
VirtualApp is an open platform for Android that allows you to create a
Virtual Space, you can install and run apk inside. Beyond that,
VirtualApp is also a Plugin Framework, the plugins running on
VirtualApp does not require any constraints. VirtualApp does not
require root, it is running on the local process
DL: Apk (Readme in chinese).
Android PluginManager
PluginManager is used to manage android applications like eclipse
plugins. you can start an activity from an uninstalled apk placed in
sdcard,just like it has installed or registed in the application's
AndroidManifest.xml.

bugs in eclipse after installing LibGDX

i had no project in eclipse, and i just followed the steps in this toturial: https://www.youtube.com/watch?v=S9fs1PVTyUc&src_vid=6i78aqugqds&feature=iv&annotation_id=annotation_4141831685
and it opened for me a few projects named:
project,
project-android,
project-core,
project-desktop,
project-ios.
and the project-android had an error.
here is the code:
package com.ak01.projext.android;*
import android.os.Bundle;**
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.ak01.projext.Projec;
public class AndroidLauncher extends AndroidApplication {***
#Override
protected void onCreate (Bundle savedInstanceState) {****
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new
AndroidApplicationConfiguration();
initialize(new Project(), config);
}
}
but it has a few errors although i didn't touch it yet(the number of *'s matches to the line)
*The type android.os.Handler cannot be resolved. It is indirectly referenced from required .class files
**The import android.os.Bundle cannot be resolved
***Multiple markers at this line
-The type android.app.Activity cannot be resolved. It is indirectly referenced from
required .class files
-The hierarchy of the type AndroidLauncher is inconsistent
****Bundle cannot be resolved to a type
It looks like you have not installed the Android developer tools.
To test this, right click your project and press build path. On the side there should be an 'Android' tab, if there isn't you need the android plugin for eclipse.
You can try to use the advanced option to check eclipse as #Tuss said in a comment. Also note that you need the version 19 of Android SDK (it isn't valid the last version at the time of writing this, API 23).
When creating the Libgdx project:
In Libgdx Project Generator open Advanced options.
Check Eclipse.
Generate your project.
In Eclipse:
Open Android SDK Manager: Window -> Android SDK Manager.
Mark if it isn't installed the SDK Plataform for Android 4.4.2 (API 19) and install it.
Finally import the projects with File -> Import -> General -> Existing Projects into Workspace.

How to import MenuDrawer from SimonVT in Android Studio

I have been searching a lot for like 4 hours how do use this, and had to reset my project as i messed up with the dependencies. Could anyone please explain how do i simply import and solve all the dependencies as i had it once set up but there were errors, i needed to change some lines in gradle file, i also tried to import pom from gradle and resolve all dependencies but it failed in the end.
i would be very thankful to anyone who would post a noob friendly [how-to] use simonvt library in android studio, i bet it would be helpful to others too.
There may be a problem with my java or android studio, thats why i would like to check a way that works for sure, so that if it fails, i have some software problems.
Update
So i set up Android studio and updated all android packages, and checked everything. I found this tutorial on how to use another menu drawer library in android studio: http://androiddev.orkitra.com/?p=72290
but i couldn't get it done as i always get dependency errors.
I am sure it would help me and a lot of other people if someone would post a step by step tutorial on how to import any of these 2 libraries.
It's as simple as adding a single line to your build.gradle file:
dependencies {
compile 'net.simonvt.menudrawer:menudrawer:3.0.+#aar'
// all the other dependencies
}
You might want to copy some styles and drawables from the sample app. Or create your own drawer style.
If all you need is a menu drawer that opens and closes, you dont need all the configurations, and other stuff that come with the library, I would go with Android's DrawerLayout. Its easy to use and you don't have all the headaches and resource overhead that comes with setting up a library project to use in your project.
I have done the following in my code
added following line to build.gradle file
dependencies {
compile 'net.simonvt.menudrawer:menudrawer:3.0.+#aar'
}
in MainActivity.java
public MenuDrawer menuDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
menuDrawer = MenuDrawer.attach(this, MenuDrawer.Type.OVERLAY, Position.LEFT, MenuDrawer.MENU_DRAG_CONTENT);
menuDrawer.setDropShadowEnabled(false);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = inflater.inflate(R.layout.activity_menu_drawer, null);
menuDrawer.setContentView(R.layout.activity_home);
menuDrawer.setMenuView(convertView);
}

android.os.Bundle cannot be resolved in libgdx Android project

I've just started using Libgdx to practice making games and I used the project creation .jar provided on the site to create the initial projects. However an error shows up in the Android project which says:
android.os.Bundle cannot be resolved.
I am using Eclipse for Java IDE. If I put the cursor over AndroidApplication which is underlined in red, it suggests that I configure the build path. I believe I have the Android SDK installed, because it worked a while back on a different workspace on a simple example project. Does anyone know what I might have done wrong in this new workspace? How can I configure the build path for the AndroidApplication class?
BTW, I believe I'm using Java 1.6 as that's what JAVA_HOME points to, although I've also got Java 1.7 installed. That might not be relevant though...
package com.example.drop;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
public class MainActivity extends AndroidApplication {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
cfg.useAccelerometer = false;
cfg.useCompass = false;
initialize(new Drop(), cfg);
}
}
The error you are seeing is related to the Android SDK configuration, and is not specific to libGDX. The compiler does not know which Android libraries to compile against.
Use the LibGDX tutorials to setup your projects correctly. (See
https://github.com/libgdx/libgdx/wiki/Manual-project-setup#android-project-setup.
The first step in there does the Android-specific setup.)
If you do not want to create a new project but fix an existing project, the following steps should configure a project to build against Android:
right-click on the project in the Eclipse Package Explorer
select Properties and pick the Android section
Make sure exactly one of the "Project Build Targets" is selected.
That should add the required Android libraries to the build path as a side-effect.
Please, also check to have ADT installed on Eclipse.

Categories

Resources