1

I am trying to create library project and used library code and resource to my other application, I can successfully get library activity or method which was not using any resource, but getting error when I try to use resource in activity or any method.

Here is my code.

package com.dhl.lib;
import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class AdditionActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        initUI();
    }

    private void initUI() {
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        final EditText edtFirst = new EditText(this);
        edtFirst.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        edtFirst.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

        final EditText edtSecond = new EditText(this);
        edtFirst.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        edtFirst.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

        Button btn = new Button(this);
        btn.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        btn.setText("Add");

    btn.setBackground(getResources().getDrawable(R.mipmap.ic_launcher));
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(AdditionActivity.this, "==> " + (Integer.parseInt(edtFirst.getText().toString().trim()) + Integer.parseInt(edtSecond.getText().toString().trim())), Toast.LENGTH_SHORT).show();
            }
        });

        linearLayout.addView(edtFirst);
        linearLayout.addView(edtSecond);

        linearLayout.addView(btn);
        setContentView(linearLayout);
    }

    @Override
    public void onClick(View v) {

    }
}

Here another class which used resource

 package com.dhl.lib;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class About {
    public static void show(Activity activity, String aboutText,
                            String okButtonText) {
        String versionNumber = "unknown";
        Drawable icon = null;
        String appName = "unknown";

        View about;
        TextView tvAbout;
        try {
            LayoutInflater inflater = activity.getLayoutInflater();
            about = inflater.inflate(R.layout.about, null);
            tvAbout = (TextView) about.findViewById(R.id.ca_tutortutor_aboutText);
        } catch (InflateException e) {
            about = tvAbout = new TextView(activity);
        }
        tvAbout.setText(Html.fromHtml(aboutText));
        tvAbout.setMovementMethod(LinkMovementMethod.getInstance());
        new AlertDialog.Builder(activity)
                .setTitle(appName + " " + versionNumber)
                .setIcon(icon)
                .setPositiveButton(okButtonText, null)
                .setView(about)
                .show();
    }
}

** XML Resource of About.xml **

<?xml version="1.0" encoding="utf-8"?>
<scrollview xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ca_tutortutor_aboutView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.dhl.lib.About">

    <linearlayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:padding="5dp">

        <textview
            android:id="@+id/ca_tutortutor_aboutText"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"></textview>
    </linearlayout>
</scrollview>

** Manifest of Lib Project **

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dhl.lib">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".AdditionActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

** app build.gradle **

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
}

From that lib I can successfully get access of AdditionActivity on my Project but When I try to access About, getting error of java.lang.NoClassDefFoundError: Failed resolution of: Lcom/dhl/lib/R$layout;

Where I made mistake ? I can't found that point.

Here Is my Application app build.gradle which used library project

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.addtion.lib"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    provided files('libs/addition.jar')
}

** Application Main Activity Class **

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn_callAdd);
        btn.setText("Call Addition Activity");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, AdditionActivity.class));
            }
        });


        ((Button) findViewById(R.id.btn_callAlert)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                About.show(MainActivity.this, "Hello From Application", "Ok");
            }
        });
    }
}
6
  • 1
    what is wrong with this question: You didn't show us how you consume(did you add it as dependencies? how?) the library in your application project ... Commented Feb 5, 2016 at 12:56
  • oh.. sorry , I not mention that, let me include that Commented Feb 5, 2016 at 12:58
  • 1
    This is not a duplicate of given in the prev comment question ... the answer is obsolted as it is about using eclipse Commented Feb 5, 2016 at 12:58
  • ok, now it is obvious ... you just add dependency to the code only ... the library with resources project should produce the xxxxxxx.aar file ... an you should use compile not provide Commented Feb 5, 2016 at 13:11
  • @Selvin : thanks, Let me try Commented Feb 5, 2016 at 13:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.