For use OpenCV in my app with Android Studio I complete the following instructions to Step 6. But I don't know how and where include
static { System.loadLibrary("opencv_java"); }
?
In main activity, like this:
public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{
static {
if(!OpenCVLoader.initDebug())
Log.d(sLog, "OpenCv load fail!");
else
Log.d(sLog, "OpenCv succes.");
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
}
...
}
instead of using System.loadLibrary(...), you can use OpenCVLoader.initDebug() which is suggested by OpenCv
Related
Android Studio 3.4
I have the next activity:
public class CartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod()
}
private void someCommonMethod() {
// some code
}
}
Now I have 2 build types: debug and release.
In debug I add method someDebugMethod() to activity
public class CartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod()
someDebugMethod()
}
private void someCommonMethod() {
// some code
}
private void someDebugMethod() {
// some debug code
}
}
In release I add method someReleaseMethod() to activity
public class CartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod()
someReleaseMethod()
}
private void someCommonMethod() {
// some code
}
private void someReleaseMethod() {
// some debug code
}
}
I read official documentation
and now project's stucture is:
app\src\debug\java\com\myproject\CartActivity.java
app\src\release\java\com\myproject\CartActivity.java
I remove CartActivity.java from app\src\main\java.
So, as a result, I have two files CartActivity.java.
Nice.
And now when I start the app in debug build type then runCartActivity in debug folder.
And when I start the app in release build type then runCartActivity in release folder.
Nice. It's work fine.
But suppose now I need to update the common method someCommonMethod(). This method used in both build types.
As a result, I need to update TWICE this method. First in app\src\debug\java\com\myproject\CartActivity.java and
then update same method with same code in app\src\release\java\com\myproject\CartActivity.java
So I think this is not good.
Because this is duplicate code. I need to copy & paste EVERY time in TWO files when update method someCommonMethod().
It's really bad.
How to avoid this duplicate code?
The ideal approach is when in CartActivity.java has only delta.
In app\src\debug\java\com\myproject\CartActivity.java has ONLY method someDebugMethod()
In app\src\release\java\com\myproject\CartActivity.java has ONLY method someReleaseMethod()
and common code is in app\src\main\java\com\myproject\CartActivity.java
Is it possible?
P.S. Suppose I have 3 build types.
As result, I need to update same code in three files. It's really not good.
Just create a CommonCartActivity in
app\src\main\java\com\myproject\CommonCartActivity.java
Then extend the class overriding the methods in the flavor implementations.
public class CartActivity extends CommonCartActivity {
protected void someCommonMethod() {
// some code
}
}
In this way CartActivity just inherits from CommonCartActivity with nothing else, duplicated in two flavors.
I think you do not need to complicate the structure of the project. Just use the real-time check.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod();
if(BuildConfig.DEBUG){
someDebugMethod();
}else{
someReleaseMethod();
}
}
private void someCommonMethod() {
// some code
}
private void someDebugMethod() {
// some debug code
}
private void someReleaseMethod() {
// some release code
}
But, if you need to separate the code, try using one activity, but create a new class (for example, Fork) in two copies for release and debug. Create an instance of the class in the activity. In this class there will be a doMetnod() which will have the necessary code depending on the type of project. So you avoid duplication of the activity code.
I have to bind a (.jar) file which contains an Eventbus module. When I call it vs reported an exception: XXXActivity has no public methods called onEvent.
but in reality the activity had an onEvent method.
Can help me?
Here is my code!
[Activity(Label = "BaseActivity")]
public class BaseActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//EventBus.Default.Register(this);
try
{
JMessageClient.RegisterEventReceiver(this.Application.ApplicationContext);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(" ==ex==" + ex.Message);
}
// Create your application here
}
public void onEventMainThread(CN.Jpush.IM.Android.Api.Event.MessageEvent Event)
{
System.Diagnostics.Debug.WriteLine(" ==1==");
}
...
}
IMRecActivity
[Activity(Label = "IMRecActivity")]
public class IMRecActivity : BaseActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
}
public void onEvent(CN.Jpush.IM.Android.Api.Event.MessageEvent Event)
{
// CN.Jpush.IM.Android.Api.Event.MessageEvent me = (MessageEvent)Event;
CN.Jpush.IM.Android.Api.Model.Message msg = Event.Message;
switch (msg.ContentType.ToString())
{
case "text":
TextContent textContent = (TextContent)msg.Content;
System.Diagnostics.Debug.WriteLine(" ==JMessage OnEvent==" + textContent.Text);
break;
default:
System.Diagnostics.Debug.WriteLine(" ==JMessage OnEvent==" + msg.Content);
break;
}
System.Diagnostics.Debug.WriteLine(" ==JMessage OnEvent==");
}
}
JMessageClient.RegisterEventReceiver(this.Application.ApplicationContext); this used to register an EventBus in the .JAR.With the code,you can see that I have declare onEvent method,but it not working.
By the way,I already uesed a tool like jd-gui to see how it work.
Here is the way to download the .JAR file:
https://www.jpush.cn/downloads/sdk/android/
The Official website,which is a Chinese website.
https://www.jpush.cn/common/products
I would recommend investigating the respective .jar using a tool like jd-gui(http://jd.benow.ca/) to see what the expected method visibility is. You can then use respective Metadata fixes to resolve any issues you find there after the generated binding is created.
I've created a generic guide to help out with this task in which you can look into:
https://gist.github.com/JonDouglas/dda6d8ace7d071b0e8cb
I have a BaseActivity which is an abstract activity and isn't registered in AndroidManifest. BaseActivity will call getPresenter in activity's lifecycle.
public abstract class BaseActivity extends AppCompatActivity{
public abstract Presenter getPresenter;
public abstract int getLayout();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
getPresenter().attachView(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
getPresenter().detachView();
}
}
I use ActivityTestRule to launch the BaseActivity, but the following error is shown.
java.lang.RuntimeException: Could not launch activity
How to test the getPresenter().attachView(this) and getPresenter().detachView() are called in correct activity's lifecycle?
I don't have quite big experience with Android Testing, especially unit testing, but I've already found this post, which may be useful for you:
Is it possible to test an Abstract activity with Robolectric
Also on Github page of Robolectric I'd found this: https://github.com/robolectric/robolectric/issues/1441
So all I can say according to your question, that yes you can test your abstract class, at least with Robolectric.
Read also: https://gualtierotesta.wordpress.com/2015/01/28/tutorial-java-abstract-classes-testing/
EDIT: Nowadays, Robolectric doesn't support directly API 23, but you can "downgrade" it in configuration of test class, like below:
#RunWith(RobolectricGradleTestRunner.class)
#Config(constants = BuildConfig.class, sdk = 21)
public class MainActivityTest {
MainActivity_ activity = Robolectric.setupActivity(MainActivity.class);
}
The error is shown because the BaseActivity isn't registered in Android Manifest. It seems that there are some solutions to add an activity in test package.
However, I finally choose another solution, delegate the activity's lifecycle to others.
This idea is mentioned in Mosby playbook.
http://hannesdorfmann.com/android/mosby-playbook/
New BaseActivity:
public abstract class BaseActivity extends AppCompatActivity implements BaseMvpView, DelegateCallback{
private ActivityMvpDelegate activityDelegate;
protected ActivityMvpDelegate getActivityDelegate() {
if (activityDelegate == null) {
activityDelegate = createActivityDelegate();
}
return activityDelegate;
}
protected ActivityMvpDelegate createActivityDelegate() {
return new ActivityMvpDelegateImpl(this, this);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivityDelegate().onCreate(savedInstanceState);
}
#Override
protected void onDestroy() {
super.onDestroy();
getActivityDelegate().onDestroy();
}
}
Finally, I can test the delegate class without activity's lifecycle.
I want to use Instabug with older version of support library (appcompat-v7 / support-v4) because our application is not ready to use Material design. But when I use InstabugAppCompatActivity (or any other type) I get material design into my application because of linking newer support library with linking com.instabug.library.instabugsupport.
Any ideas how to do that? Thanks
You can use this include (or newer version of instabugbasic): compile 'com.instabug.library:instabugbasic:1.3.8'
Then you need to create your InstabugActivity extending your ActionBarActivity and override few methods.
public class BaseInstabugActivity extends ActionBarActivity {
InstabugActivityDelegate mDelegate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDelegate = new InstabugActivityDelegate(this);
}
#Override
protected void onResume() {
super.onResume();
mDelegate.onResume();
}
#Override
protected void onPause() {
super.onPause();
mDelegate.onPause();
}
#Override
protected void onDestroy() {
super.onDestroy();
mDelegate.onDestroy();
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
mDelegate.dispatchTouchEvent(ev);
return super.dispatchTouchEvent(ev);
} }
The rest of integration is the same as with official guide. So don't forget to inicialize Instabug in Application class and add InstabugFeedbackActivity into your manifest. Maybe you will need to use own android:theme with InstabugFeedbackActivity in manifest.
I want to create project that reacts to OnCreate() method.
So, for example, I have activity
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
//do something
}
}
And i want my AspectJ class to do something before and after OnCreate method call.
public aspect onCreate
{
pointcut captureOnCreate() : (execution(* onCreate(Bundle)));
before(): captureOnCreate()
{
System.out.println("Aspect BEFORE called");
}
after(): captureOnCreate()
{
System.out.println("Aspect AFTER called");
}
}
I tried to convert project to AspectJ and run it as Android application project, but it doesn't work. What is wrong?
SOLVED
Solved it myself.
In Eclipse AspectJ tools -> Inpath -> Add External JARs and link it to aspectjrt.jar file.
And executoin looks like that:
execution(* onCreate(*))&& !within(com.xxx.automation.onCreate);