如何使用Resource在Android Studio中创建库项目

时间:2016-02-05 12:49:20

标签: android android-library

我正在尝试创建库项目并将库代码和资源用于我的其他应用程序,我可以成功获取未使用任何资源的库活动或方法,但是当我尝试在活动或任何方法中使用资源时出错

这是我的代码。

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) {

    }
}

这是另一个使用资源的类

 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();
    }
}

** About.xml的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>

** Lib项目的清单**

<?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'
}

从该lib我可以成功访问我的Project上的AdditionActivity,但是当我尝试访问About时,得到java.lang.NoClassDefFoundError的错误:解析失败:Lcom / dhl / lib / R $ layout;

我犯了错误?我找不到那一点。

这是我的应用程序app build.gradle,它使用了库项目

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')
}

**申请主要活动类**

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");
            }
        });
    }
}

0 个答案:

没有答案