Android libraries are essentially
fully featured Android apps
with the one restriction that they
can't be installed by themselves.
They have to be packaged into another
app to be installed on a device.
Android apps tend to consist of a whole
lot of files, so while you can totally
create an Android library by hand,
it's a lot easier to use the wizard.
Let's check out how to do that.
If you right-click on
the root of your project and
select New Module > Phone and
Tablet Applications > Android Library,
you can give your library a name and
an sdk, and then just like a normal app,
you can choose what kinds of
activities you want to add.
Let's add a blank
activity with a fragment.
We'll give it a name.
Now, a myLibrary folder has been
added to the root of your project
with all the familiar source and res and
even a manifest that you'd associate
with a normal Android app.
Also, the wizard is nice enough to add
your new library as a sub-project in
the multi-project build.
So, the last thing you need to do to be
able to use your new activity in your
base app is to head over to the
build.gradle file in the app directory.
And then we'll need to add
a compile time dependency
on the project myLibrary.
That's all there is to it.
Let's check out a working example.
So here's the demo from the course code,
which I strong suggest you go check out.
We've added this imageactivity
Android library to our project.
This library defines an activity with
its own layouts and with its own values,
like strings.
So here's the message that
the activity's going to display.
And this drawable of the Gradle logo
is also wrapped up inside our Android
library.
In settings.gradle,
imageactivity is added as a subproject,
and in the build.gradle
file of the app directory,
we have a compile time dependency
on the image activity project.
So that means in the main activity
of our main Android project,
we can import image activity and
set up an Intent to go launch it.
If we go ahead and
deploy that to our test device, here's
the activity from our app module and
our button which then launches
the activity from our Android library.
There we go.
We've made an activity that's easy
to reuse between applications.
تُعد مكتبات Android في الأساس
،تطبيقات ذات ميزات كاملة لـ Android
ولكن مع تقييد واحد
.وهو عدم إمكانية تثبيت نفسها
فيجب وضعها في حزمة داخل تطبيق آخر
.ليتم تثبيتها على الجهاز
تميل تطبيقات Android إلى أن تتألف من دفعة
كاملة من الملفات. إذًا، في حين أن بإمكانكم إنشاء
،مكتبة Android يدويًا
.فإنه من الأيسر كثيرًا استخدام المعالج
.لنرَ كيفية القيام بذلك
إذا نقرتم بزر الماوس الأيمن فوق
جذر المشروع
ثم قمتم بتحديد New Module > Phone and
،Tablet Applications > Android Library
يمكنكم إعطاء المكتبة اسمًا
،وsdk، وبالضبط مثل أي تطبيق عادي
يمكنكم اختيار أنواع الأنشطة
.المراد إضافتها
لنضِف Blank Activity with Fragment
.
.ونمنحه اسمًا
تم الآن إضافة مجلد myLibrary
إلى جذر المشروع
مع ملفات المصدر وres (الموارد) المألوفة
وحتى ملف Manifest (ملف البيانات) الذي يتم إقرانه
.مع تطبيق Android العادي
بالإضافة إلى ذلك، يتميز المعالج بسهولته بما يكفي
لإضافة المكتبة الجديدة كمشروع فرعي في
.البنية متعددة المشاريع
إذًا، آخر ما يلزم القيام به هو
إمكانية استخدام النشاط الجديد في
التطبيق الأساسي، عن طريق الانتقال إلى
.ملف build.gradle في مستودع التطبيق
وبعد ذلك، يلزم إضافة
تبعية وقت التجميع
.على المشروع myLibrary
.وهذا كل المطلوب عمله
.لنرَ مثالاً عمليًا
،ها هو العرض التوضيحي من التعليمات البرمجية للدورة
.والذي أقترح بشدة أن تطَّلعوا عليه
أضفنا مكتبة Android imageactivity
.إلى المشروع
هذه المكتبة من شأنها تعريف نشاط
بالتخطيطات والقيم الخاصة به
.مثل السلاسل
إذًا، تكمن هنا الرسالة وهي أن
.النشاط سيتم عرضه
علاوة على ذلك، يتم إنجاز هذا المورد المسؤول عن رسم شعار Gradle
داخل مكتبة
.Android
،في settings.gradle
،يتم إضافة imageactivity كمشروع فرعي
وفي ملف build.gradle
،الخاص بمستودع التطبيق
لدينا تبعية وقت التجميع
.على مشروع نشاط الصورة
مما يعني أنه في النشاط الأساسي
،لمشروع Android الرئيسي
يمكن استيراد مكتبة ImageActivity
.وإعداد هدف لتشغيلها
إذا تابعنا
وقمنا بنشر ذلك إلى جهاز الاختبار، فها هو
النشاط من الوحدة النمطية للتطبيق
وهذا هو الزر الذي سيشغِّل
.النشاط من مكتبة Android بعد ذلك
.تمت العملية بنجاح
لقد أنشأنا نشاطًا يَسهل
.إعادة استخدامه بين التطبيقات
As bibliotecas do Android são aplicativos do Android
inteiramente caracterizados
com a restrição de que não
podem ser instalados por si mesmos.
Para que sejam instaladas em um dispositivo,
elas precisam ser embaladas em outro aplicativo.
Aplicativos do Android em geral têm
muitos arquivos, por isso, mesmo que seja
possível criar uma biblioteca do Android manualmente,
é muito
mais fácil
usar o assistente.
Vejamos como fazer isso.
Com o botão direito do mouse, clique
na raiz do projeto,
selecione New Module >
Phone and Tablet Applications >
, selecione biblioteca do Android,
dê um nome e um SDK
à
sua biblioteca e depois, como faria em um aplicativo normal,
selecione
quais tipos de
atividades você deseja adicionar.
Vamos adicionar uma atividade em
branco com um fragmento.
Damos-lhe um nome.
A pasta myLibrary foi adicionada
à raiz do seu projeto,
com todas as fontes e res conhecidas e
até um manifesto que você pode associar
a um aplicativo do Android.
O assistente também adiciona
sua nova biblioteca como um subprojeto
na compilação de multiprojetos.
A última coisa que precisa fazer
para usar sua nova atividade no aplicativo base
é acessar o arquivo build.gradle no
diretório
de aplicativos.
Precisamos adicionar uma
dependência do tempo de compilação
na pasta myLibrary do projeto.
Isso é tudo que existe para ela.
Vejamos um exemplo prático.
Veja aqui a demonstração
do código do curso,
que recomendo enfaticamente
que você confira.
Adicionamos esta imageactivity
da biblioteca do Android ao nosso projeto.
Essa biblioteca define uma atividade com
os próprios layouts
e valores,
como strings.
Aqui está a mensagem que
a atividade vai mostrar.
E este desenhável do logotipo Gradle
também é embalado em nossa biblioteca do
Android.
Em settings.gradle,
a imageactivity é adicionada como um subprojeto
e, no arquivo build.gradle
do diretório do aplicativo,
temos uma dependência do tempo de compilação
no projeto de atividade de imagem.
O significado disso é que, na atividade principal
de nosso projeto Android principal,
nós podemos importar atividade de imagem e
configurar uma Intenção para lançá-la.
Se implementarmos isso em
nosso dispositivo de teste, eis
a atividade de nosso
módulo de aplicativo e
o botão que farão o lançamento
da atividade de nossa biblioteca do Android.
Vamos lá.
Nós criamos uma atividade de fácil
reutilização entre aplicativos.
Android 库实际上是一个功能全面的 Android 应用
但它有一个限制 即不能自行安装
它需要打包到其他应用中才能安装在设备上
Android 应用通常包括大量文件 所以
尽管可以完全手动创建一个 Android 库 但使用向导会简单得多
让我们看看如何执行此操作
如果你右键单击项目的根目录
然后选择 “新模块” > “手机和平板电脑应用程序” > “Android 库”
你可以为库提供一个名称和一个 sdk 然后
就像正常应用一样 可以选择要添加的 activity 类型
我们添加一个具有 Fragment 的空白 activity
我们为它提供一个名称
现在 myLibrary 文件夹已添加至项目的根目录
其中包含所有熟悉的源和资源
以及你已与正常 Android 应用关联的清单
此外 向导也非常实用
可将你的新库添加为多项目构建中的子项目
因此 只需要继续前往应用目录中的 build.gradle 文件
即可在基本应用中使用新 activity
然后 我们将需要在项目 myLibrary 上
添加一个编译时间依赖性
这就是全部要讲的内容
我们来看看工作示例
这是课程代码中的演示 强烈建议大家看一看
我们已将此 imageactivity Android 库添加到项目中
此库定义了一个具有自己的布局和值
(如字符串) 的 activity
这就是 activity 将要显示的消息
而且 Gradle 徽标的这一绘制图也封装在
Android 库内部
在 settings.gradle 中 imageactivity 作为子项目
添加到应用目录的 build.gradle 文件中
我们具有大量关于图像 activity 项目的时间依赖性
所以 这意味着在主 Android 项目的主 activity 中
可以导出图像 activity 并设置一个 Intent 来启动它
如果继续并
将其部署到测试设备 这将是应用模块中的 activity 以及
从 Android 库中启动 activity 的按钮
现在开始
我们创建了一个易于在应用程序之间重复使用的 activity